David Kassa
David Kassa

Reputation: 754

SendGrid 403 Response for /v3/contactdb/recipients - Permissons needed

What permissions do I need or what am I doing wrong with this SendGrid API call?

I'm trying to post a new recipient ( /contactdb/recipients ) to Send Grid but keep getting a 403 response:

I get this even when calling the API from the SendGrid Explorer

Contacts API - Recipients

{
  "errors": [
    {
      "field": null,
      "message": "access forbidden"
    }
  ]
}

This makes me think that my API Key doesn't have enough permissions but it has Full Access.

Here is my client code as well.

require("dotenv").config();
const client = require("@sendgrid/client");

exports.handler = function(event, context, callback) {
  const body = JSON.parse(event.body);
  const email = body.email;

  if (!process.env.SENDGRID_API_KEY) {
    callback("No API Key");
  }

  client.setApiKey(process.env.SENDGRID_API_KEY);
  const request = {
    method: "POST",
    url: "/v3/contactdb/recipients",
    body: JSON.stringify([{ email }])
  };

  client
    .request(request)
    .then(([response, body]) => {
      // console.log(response.statusCode);
      // console.log(body);
      callback(null, response, body);
    })
    .catch(error => {
      // console.log(JSON.stringify(error.response.body.errors));
      callback(error);
    });

};

Upvotes: 9

Views: 4111

Answers (2)

Lunatic
Lunatic

Reputation: 1926

  • Update

Previous answer does not coordinating the issue directly, sendgrid recently changed their internal api's base url, creating list of recipients which internally are static collections of Marketing Campaigns contacts. This API allows you to interact with the list objects themselves. To add contacts to a list, you must use the Contacts API.

Legacy api :

/v3/contactsdb/lists

New functional variant

/v3/marketing/lists

Upvotes: 1

David Kassa
David Kassa

Reputation: 754

Per support:

We have just very recently released a "New Marketing Campaigns" experience and the endpoints have changed from our "Legacy Marketing Campaigns".

Try this endpoint:

https://api.sendgrid.com/v3/marketing/contacts

I pulled it from our documentation here:

https://sendgrid.api-docs.io/v3.0/contacts/add-or-update-a-contact

Upvotes: 19

Related Questions