fbpx

How to add a SendGrid Contact with Zapier Code Snippets

If you’re trying to connect a Squarespace newsletter signup form to SendGrid, this is the article for you.

Recently, I needed to connect a Squarespace newsletter form to SendGrid. There’s not a direct integration, so using Zapier to store the Squarespace newsletter signups in SendGrid was the best option.

I spent a while googling for “how to connect Squarespace to SendGrid”. It was easy to learn how to send an email after someone signed up; however, annoyingly SendGrid did not automatically add this person to my contacts database.

If you’re trying to figure out how to use Zapier’s code snippets to add a contact to SendGrid using the API endpoints, then this should help!

Here’s the final code snippet used in Zapier. (Note: You’ll need to add your own SendGrid API Key.)

const data = {
  "list_ids": [
    "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" // LIST_ID
  ],
  "contacts": [
    {
      "email": inputData.email,
      "first_name": inputData.firstName,
      "last_name": inputData.lastName
    }
  ]
}

const res = await fetch('https://api.sendgrid.com/v3/marketing/contacts', {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json',
    'authorization': 'Bearer SG.xxxxxxxxxx' // SENDGRID_API_KEY
  },
  body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
  console.log('Success:', data);
})
.catch((error) => {
  console.error('Error:', error);
});

return { res: res };

Despite a rather popular Github issue discussing the difficulties in shaping the request, there’s surprisingly little information on this. This is partially because SendGrid supports a legacy API that uses a different endpoint, api.sendgrid.com/v3/contactdb/recipients/.

I’ll also point out that the inputData object is injected by Zapier. You can choose a variable from a previous step in your Zap to inject with whatever variable name you decide.

Lastly, make sure you ensure your Zap is turned on! Annoyingly, Zapier seems to turn the Zap off anytime the code produces an error. This is problematic if you have previous steps that you want to function while trying to test the code snippet. In my case, this meant newsletter signups were not receiving the welcome email because the Zap was automatically turned off and I didn’t realize it!

You can see the “On” toggle in the top-right below.

I hope this helps you store your Squarespace newsletter signups in SendGrid!


Note: I believe that API access requires a paid SendGrid account, so you may run into unforeseen issues if you’re trying to do this on the free account.