Add Emails to SendGrid Contacts in React Native & Node JS

Add Emails to SendGrid Contacts in React Native & Node JS

Helpful links:

Get list IDs

Add or update a contact

sengrid nodejs

SendGrid error response

request npm

After you create an account on SendGrid go ahead and install request: npm i request


I put my http call to SendGrid in a firebase function. This is what is looks like

exports.addToSendGrid = functions.https.onCall(async (data) => {
const e = data.email
const f = data.first
const l = data.last
var request = require("request");
var options = { method: 'PUT',
url: 'https://api.sendgrid.com/v3/marketing/contacts',
headers:
{ 'content-type': 'application/json',
authorization: 'Bearer ' + SEND_GRID_KEY},
body: { list_ids: [ 'eda0bc01-b098-4366-ad58-8bab03ec9b33' ],
contacts: [
{
  "email": e,
  "first_name": f,
  "last_name": l
}
]},
json: true };
request(options, function (error, response, body) {
 if (error) throw new Error(error);
 console.log(body);
});

You can get the list ID from your SendGrid list url.
A successful response will look like this:

job_id: '2bae9f9a-201c-4644-b364-08a27e2e720e'

Related Posts