all repos — caroster @ 1b27f87d317fe2a90cceb3da3943ea4b18ff9676

[Octree] Group carpool to your event https://caroster.io

backend/src/lib/sendgrid.ts (view raw)

 1import sendgrid from "@sendgrid/client";
 2
 3const { SENDGRID_API_KEY, SENDGRID_CONTACTLISTID } = process.env;
 4
 5sendgrid.setApiKey(SENDGRID_API_KEY);
 6
 7export default {
 8  // https://sendgrid.api-docs.io/v3.0/contacts/add-or-update-a-contact
 9  subscribe: async (email) => {
10    if (!SENDGRID_CONTACTLISTID) {
11      strapi.log.error(
12        "No Sendgrid contact list ID provided (SENDGRID_DEFAULT_CONTACTLISTID)"
13      );
14      return null;
15    }
16
17    try {
18      strapi.log.info(`Save ${email} to Sendgrid contact list.`);
19
20      await sendgrid.request({
21        method: "PUT",
22        url: "/v3/marketing/contacts",
23        body: { list_ids: [SENDGRID_CONTACTLISTID], contacts: [{ email }] },
24      });
25    } catch (error) {
26      console.error(error.response.body.errors);
27      strapi.log.error(
28        `Impossible to save email ${email} tp Sengrid contact list. Error: ${JSON.stringify(
29          error
30        )}`
31      );
32    }
33  },
34};