Created
June 15, 2022 04:25
-
-
Save jdretz/d7e666e7a62adc5fc740c06acab27f65 to your computer and use it in GitHub Desktop.
Add a new contact to a SendGrid contact list using the SendGrid API via a Netlify Typescript Function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Handler } from "@netlify/functions" | |
import client from "@sendgrid/client" | |
import { ClientRequest } from "@sendgrid/client/src/request" | |
const blogListId = `0350949f-5c47-4bb3-b8fb-83e848f9d8ae` | |
const handler: Handler = async (event, context) => { | |
if (event.httpMethod !== `POST`) { | |
return { | |
statusCode: 405, | |
body: `Method Not Allowed`, | |
} | |
} | |
if (typeof process.env.SENDGRID_TOKEN !== `string`) { | |
return { | |
statusCode: 500, | |
body: `Could not set Sendgrid API Key`, | |
} | |
} | |
client.setApiKey(process.env.SENDGRID_TOKEN) | |
let reqBody | |
// Check values from frontend | |
if (event?.body) { | |
reqBody = JSON.parse(event.body) | |
} | |
if (!reqBody) { | |
return { | |
statusCode: 400, | |
body: `No response body found.`, | |
} | |
} | |
const { firstName, lastName, email } = reqBody | |
if (!firstName || !email) { | |
return { | |
statusCode: 400, | |
body: `Please provide an email address and first name`, | |
} | |
} | |
// Fetch marketing mailing list | |
const query = { | |
query: `email = '${email}' AND CONTAINS(list_ids, '${blogListId}')`, | |
} | |
const request: ClientRequest = { | |
url: `/v3/marketing/contacts/search`, | |
method: `POST`, | |
body: query, | |
} | |
let recipients | |
try { | |
recipients = await client.request(request) | |
} catch (e) { | |
return { | |
statusCode: 500, | |
body: `Failed to fetch current mailing list.`, | |
} | |
} | |
// Check if email is already in mailing list | |
// If they are in the list, return an error | |
const [response, body] = recipients | |
if (!body) { | |
return { | |
statusCode: 500, | |
body: `Could not retreive contact list.`, | |
} | |
} | |
const { contact_count: contactCount, result } = body | |
if (contactCount > 0 && result.map((cont: { email: string }) => cont.email).includes(email)) { | |
return { | |
statusCode: 409, | |
body: `User is already subscribed.`, | |
} | |
} | |
// If user is not subscribed, add the new user | |
// as a subscriber | |
const data = { | |
list_ids: [blogListId], | |
contacts: [ | |
{ | |
first_name: firstName, | |
last_name: lastName || ``, | |
email, | |
}, | |
], | |
} | |
const signupReqConfig: ClientRequest = { | |
method: `put`, | |
url: `/v3/marketing/contacts`, | |
body: data, | |
} | |
try { | |
await client.request(signupReqConfig) | |
// Return success status code | |
return { | |
statusCode: 200, | |
} | |
} catch (e) { | |
return { | |
statusCode: 500, | |
body: `Server error.`, | |
} | |
} | |
} | |
// eslint-disable-next-line import/prefer-default-export | |
export { handler } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment