emailsendgridsubscriptionnewsletter

Is there a way to build a complete sendgrid subscribtion form using only sendgrid's API?


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.


Solution

  • 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);