27

Sendgrid webhook has sg_message_id But the response of Web API v2 when sending mail doesn't have sg_message_id. In fact, there is only message showing only success or failed.

So, how do i associate between sent mail and the webhook callback?

I have check the past questions in SO but it doesn't provide be the solution for this.

Thank you.

dmulter
  • 2,608
  • 3
  • 15
  • 24
chaintng
  • 1,325
  • 3
  • 14
  • 26

3 Answers3

37

You can use unique arguments. Take your unique ID (like primary key) and give it to sendgrid API during sending email. You will get it back in the event webhook.

https://docs.sendgrid.com/for-developers/sending-email/unique-arguments

When you want to use "unique arguments" through SMTP you should be able to do so using the X-SMTPAPI message header.

https://docs.sendgrid.com/for-developers/sending-email/building-an-x-smtpapi-header

Koen.
  • 25,449
  • 7
  • 83
  • 78
Dmitry Polyakovsky
  • 1,535
  • 11
  • 31
  • This must be accepted as answer. Thanks Dmitry! Actually this is something that must be added to the Sendgrid docs!!! – Auresco82 Oct 13 '16 at 13:54
  • 1
    Is there something analogous for the v3 SendGrid API? – ktingle Feb 22 '18 at 03:16
  • 6
    @ktingle yes, for the v3 API, use `custom_args` – KayakinKoder Nov 15 '18 at 05:24
  • Note: if your hook fails and you need retrospect data you will need to pay undisclosed amount for [Email Activity API](https://sendgrid.api-docs.io/v3.0/email-activity/filter-all-messages). (Price is not shown for free users for some reason). – int_ua Jul 09 '19 at 16:03
  • [Here's the v3 SendGrid documentation](https://docs.sendgrid.com/for-developers/tracking-events/event#unique-arguments-and-custom-arguments) for `custom_args` / `unique_args`. – James Hooper Nov 24 '22 at 15:42
22

EDIT: The best practice seems to be using custom arguments (see documentation). Which is not apparent from the documentation (as of today).


In Web API v3, the response includes a special header X-Message-Id (see https://sendgrid.com/docs/Glossary/x_message_id.html).

This can be later matched with sg_message_id in webhook: https://sendgrid.com/docs/Glossary/message_id.html

The documentation does not make it clear, but the X-Message-Id header is actually not equal to sg_message_id, but it is a prefix of sg_message_id.

For example:

  • X-Message-Id = "MUvGg3V1ThOu3oe8eRqFrA"
  • sg_message_id = "MUvGg3V1ThOu3oe8eRqFrA.filter0001p2iad2-21183-5AF0BD9B-E.0"
Marek Lisý
  • 3,302
  • 2
  • 20
  • 28
  • I would advise against this; it is entirely possible that this type of matching does not work 100% of the time. As you noted in your edit a custom argument avoids this possibility – KayakinKoder Jan 23 '19 at 21:53
  • Me too, actually. At the time, the documentation told everywhere people should use those X-Message-Id, however the implementation was all kind of weird... – Marek Lisý Jan 25 '19 at 08:56
  • 1
    After those 30 days - if user will do any activity with that message (like open it again or click) after i.e. 40 days (so beyond 30 days limit) will that message come back to email activity history? – jean d'arme Sep 16 '19 at 19:15
  • I would not use it. This pattern can be changed by sendgrid any day any time. Don't depend on it. – Tyagi Akhilesh Oct 11 '20 at 14:27
1

The Web APIv2 call that sends out the email only responds with a {message :"success"} answer indeed, but the sg_message_id is not generated at that point. The API call answer simply acknowledges that the email was sent to SendGrid for processing.

The email is sent to SendGrid, and there it receives the sg_message_id value that is provided through the Event Webhook posts.

VictorA
  • 41
  • 2