sendgridsendgrid-api-v3unsubscribe

How can I configure the SendGrid unsubscribe link to not add the email to any unsubscribe lists?


My app sends email newsletters. I'm using SendGrid. I want to use SendGrid's subscription tracking to provide the user-facing unsubscribe interface, because it authenticates the unsubscribe URL, and it sets a List-Unsubscribe header which includes the mailto: method.

I want to keep track of unsubscriptions in my database. I do this with SendGrid's unsubscribe webhook event: when a user clicks the unsubscribe URL, or sends to the List-Unsubscribe address, SendGrid triggers an unsubscribe webhook for me.

Here's my code so far, which works:

// Sending a newsletter

import * as sgMail from '@sendgrid/mail';
sgMail.send({
  customArgs: {
    newsletter_id: 'cats',
  },
  to: 'foo@bar.com',
  from: 'me@app.com',
  trackingSettings: {
    subscription_tracking: {
      enable: true,
      substitution_tag: '--unsubscribe--',
    },
  },
  html: '<a href="--unsubscribe--">Click here to unsubscribe</a>'
});

// Then, in SendGrid webhook endpoint:

const webhookEvents = await extractSGWebhookBody(req);
for (const ev of webhookEvents) {
  if (ev.event === 'unsubscribe') {
    const newsletterId = ev['newsletter_id'];
    const accountId = ev[EMAIL_TAG_ACCOUNT_ID];
    dbUnsubscribe(ev.email, newsletterId);
  }
}

However, as well as triggering a webhook, SendGrid also adds the email address to its own global unsubscribe/suppression list. This is undesirable, because I then have two places where unsubscribes are tracked. They will get out of sync, for many reasons (e.g. if the user re-subscribes within the app, SendGrid won't know about it).

So, I want to disable SendGrid's unsubscribe lists. I just want to use its unsubscribe feature as a proxy: user clicks unsubscribe, then SendGrid sends me a webhook.

How do I stop SendGrid's unsubscribe URL from adding the user's email address to its own unsubscribe list?


Solution

  • It's best to let SendGrid be the source of truth for unsubscribes, that way you will never accidentally send an email to a user who has unsubscribed to that group. However, you can keep your application in sync with SendGrid.

    To stop your application going out of sync with SendGrid there are Event Webhooks for resubscribes. So if a user resubscribes through SendGrid, your application can know about it.

    And if a user resubscribes through your application, your application needs to delete the suppression (unsubscribe record) so that SendGrid will send to that user again.


    To my knowledge, there is no way to stop SendGrid from keeping the information about unsubscribes, when you use the SendGrid methods like the generated unsubscribe link and the List-Unsubscribe header. To keep all that data in your application, you should implement unsubscribes yourself.