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.

6 Comments

  1. Hey, this is awesome. If I wanted to put the emails into a specific Contacts List in SendGrid what do I do?
    Thanks in advance

    • Hey William! Sorry for the delay. You need to get the list ID from Sendgrid and then include it here in the custom script–specifically in the array called "list_ids". You should replace string (optional)with the list ID you want to use:

      const data = {
        "list_ids": [
          "string (optional)"
        ],
        "contacts": [
          {
            "email": inputData.email
          }
        ]
      }

      Let me know if you have other questions!

  2. Hello! Thank you. That’s exactly i looking for.
    But Zapier shows “SyntaxError: Unexpected token ]”
    Can you help me to reach right request?

    • Hey Vitaliy! Sorry for the delay. It sounds like there is an extra ] in your script. I just confirmed that the code in this article is currently live and working in an active Zap, so it should work if you swap out the LIST_ID placeholder and the SENDGRID_API_KEY. Let me know if you are still running into issues!

  3. Hi Joel,

    I am getting the following error when trying to set this up. Any ideas?

    INFO Success: { errors: [ { field: ‘contacts[0].email’, message: ’email is required’ } ] }

    • Hey Parker! Hmmm, could you copy and paste the script here so I can look at it? There’s a chance the API has changed but hard to say without seeing your code.

Leave a Reply

Your email address will not be published. Required fields are marked *