SendGrid Webhook事件能否包含personalizations中的custom_args?
SendGrid Webhook不返回收件人专属custom_args的解决办法
SendGrid默认只会把全局层级的custom_args返回在Webhook事件里,personalizations.to里的收件人专属custom_args不会自动携带。要让事件返回个性化参数,有几个直接可行的方案:
方案1:把专属custom_args移到personalizations层级
把每个收件人的专属参数放到personalizations对象的custom_args中,而不是嵌套在to数组的对象里。SendGrid会自动将这个层级的custom_args和全局的合并,一起返回在Webhook事件中。
修改后的请求体示例:
{ "from": { "email": "sender@example.com", "name": "Example Sender" }, "personalizations": [ { "to": [ { "email": "recipient1@example.com" } ], "custom_args": { "userId": "63r20acm4xfv" }, "dynamic_template_data": { "subject": "New Article", "description": "Read the lastest news on...", "documentId": "624dxnpvhw8g", "host": "https://localhost:8000", "userId": "63r20acm4xfv" } } ], "custom_args": { "documentId": "624dxnpvhw8g" } }
这样处理后,Webhook事件里就会同时出现全局的documentId和个性化的userId。
方案2:用自定义Header传递专属参数
在personalizations里添加headers字段,把专属参数做成自定义Header,比如X-User-Id。SendGrid会在Webhook的headers字段中返回这些自定义Header,你可以从中提取需要的参数。
请求体片段示例:
"personalizations": [ { "to": [{"email": "recipient1@example.com"}], "headers": { "X-User-Id": "63r20acm4xfv" }, "dynamic_template_data": { // ... 模板数据 } } ]
后续接收Webhook时,从事件对象的headers属性里读取X-User-Id即可。
注意事项
- 不要把专属参数放在
to数组的对象里,这个位置的custom_args不会被带入Webhook事件; - 方案1是最直接的,推荐优先使用,因为参数会直接出现在事件的根层级,无需额外解析。
内容的提问来源于stack exchange,提问作者MattP
相关产品推荐
相关产品推荐

