-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
integrate mailchimp with an api function
- Loading branch information
1 parent
8246cb8
commit f5e8c44
Showing
6 changed files
with
232 additions
and
20 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# grants list | ||
GOOGLE_GRANTS_SHEET_API_URL= | ||
|
||
# project grants application | ||
PROJECT_GRANTS_DOWNLOAD_FILE_URL= | ||
|
||
# mailchimp | ||
MAILCHIMP_APIKEY= | ||
MAILCHIMP_SERVER= | ||
MAILCHIMP_LIST_ID= |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import mailchimp from '@mailchimp/mailchimp_marketing'; | ||
|
||
mailchimp.setConfig({ | ||
apiKey: process.env.MAILCHIMP_APIKEY, | ||
server: process.env.MAILCHIMP_SERVER | ||
}); | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const { body, method } = req; | ||
|
||
if (method !== 'POST') { | ||
return res.status(405).end(`Method ${method} Not Allowed`); | ||
} | ||
|
||
try { | ||
await mailchimp.lists.addListMember(process.env.MAILCHIMP_LIST_ID!, { | ||
email_address: body, | ||
status: 'subscribed' | ||
}); | ||
|
||
res.status(200).end(); | ||
} catch (err: any) { | ||
console.error(err); | ||
|
||
if (err.response) { | ||
// return the mailchimp error code & message | ||
// docs: https://mailchimp.com/developer/marketing/docs/errors/ | ||
const body = err.response.body; | ||
res.status(body.status).end(body.detail); | ||
} else { | ||
res.status(500).end(); | ||
} | ||
} | ||
} |
Oops, something went wrong.