MattP
MattP

Reputation: 31

Can SendGrid webhook events include custom_args from personalizations?

I'm using a dynamic template in SendGrid to send transactional emails that typically go to multiple recipients in the personalizations field. The message body includes custom_args that are shared among all recipients, and in personalizations, both custom_args and dynamic_template_data specific to each recipient. Webhook events return the shared custom_args, but none of the recipient-specific custom_args. Is there a way to get events together with the personalized custom_args? Here's an example request body where we add a userId custom_arg for each recipient:

{
  "from": {
    "email": "[email protected]",
    "name": "Example Sender"
  },
  "personalizations": [
    {
      "to": [
        {
          "email": "[email protected]",
          "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"
  }
}

And the webhook events we receive (the "documentId" custom arg is reported, but not the "userID" custom_arg from personalizations):

[
  {
    "documentId": "624dxnpvhw8g",
    "email": "[email protected]",
    "event": "processed",
    "sg_event_id": "cHJvY2Vzc2VkLTI3MTk3NzMzLUtTUEJOaVpGUXpXb05KblctVDBNRkEtMA",
    "sg_message_id": "KSPBNiZFQzWoNJnW-T0MFA.filterdrecv-58c58fcb9f-44vrp-1-6312139C-1F6.0",
    "sg_template_id": "d-1ebc6b474b7041c0b439d1bdb17650e4",
    "sg_template_name": "DEFAULT TEMPLATE",
    "smtp-id": "<redacted>",
    "timestamp": 1662128000
  },
  {
    "documentId": "624dxnpvhw8g",
    "email": "[email protected]",
    "event": "delivered",
    "ip": "<redacted>",
    "response": "250 Message received",
    "sg_event_id": "ZGVsaXZlcmVkLTAtMjcxOTc3MzMtS1NQQk5pWkZReldvTkpuVy1UME1GQS0w",
    "sg_message_id": "KSPBNiZFQzWoNJnW-T0MFA.filterdrecv-58c58fcb9f-44vrp-1-6312139C-1F6.0",
    "sg_template_id": "d-1ebc6b474b7041c0b439d1bdb17650e4",
    "sg_template_name": "DEFAULT TEMPLATE",
    "smtp-id": "<redacted>",
    "timestamp": 1662128002,
    "tls": 1
  }
]

Upvotes: 1

Views: 1636

Answers (1)

MattP
MattP

Reputation: 31

Ugh - the custom_args in personalizations was at the wrong level. After moving the custom_args up to the same level as the dynamic_template_data, it worked fine and the values are included in the webhook events.

The example from the question was changed to:

{
  "from": {
    "email": "[email protected]",
    "name": "Example Sender"
  },
  "personalizations": [
    {
      "to": [
        {
          "email": "[email protected]",
        }
      ],
      "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"
  }
}

Upvotes: 2

Related Questions