3

I'm trying to build my own sendgrid subscribtion form using their API. Unfortunately this https://www.npmjs.com/package/@sendgrid/subscription-widget is the only solution I could find which requires a Heroku account which I don't need. I just want to find out what the API request should look like to subscribe to a mailing list whithout using third party apps.

BonisTech
  • 342
  • 3
  • 15
  • Keep in mind, the API can only be used from a server endpoint. CORS will be blocked if the request is made from a browser (https://stackoverflow.com/questions/61067224/nodejs-sendgrid-cors-request-blocked) – BonisTech Oct 29 '21 at 08:53

1 Answers1

4

Twilio SendGrid developer evangelist here.

Yes, you can create your own subscription form. To create a new contact in a list, you can use the create contacts API.

In JavaScript, using the API would look like:

const { Client } = require("@sendgrid/client");
const client = new Client();
client.setApiKey(process.env.SENDGRID_API_KEY);

const request = {
  method: "PUT",
  url: "/v3/marketing/contacts",
  body: {
    contacts: [{ email: "test@example.com", first_name: "Test" }],
  },
};
client.request(request)
  .then(console.log)
  .catch(console.error);
philnash
  • 70,667
  • 10
  • 60
  • 88