This directory holds the code for the Mailchimp Integration Tutorial.
You can either:
- install and use it as a Medusa application;
- or copy its source files into an existing Medusa application.
- Node.js v20+
- Git CLI
- PostgreSQL
- Mailchimp account with an API key and an audience list.
- Clone the repository and change to the
mailchimp-integrationdirectory:
git clone https://github.com/medusajs/examples.git
cd examples/mailchimp-integration2. Rename the .env.template file to .env.
3. If necessary, change the PostgreSQL username, password, and host in the DATABASE_URL environment variable.
4. Set the Mailchimp environment variables:
MAILCHIMP_API_KEY=
MAILCHIMP_SERVER=
MAILCHIMP_LIST_ID=
MAILCHIMP_NEW_PRODUCTS_SUBJECT_LINE=
MAILCHIMP_NEW_PRODUCTS_STOREFRONT_URL=Where:
MAILCHIMP_API_KEYis your Mailchimp API key.MAILCHIMP_SERVERis your Mailchimp server prefix. For example,us5.MAILCHIMP_LIST_IDis the ID of the audience list.MAILCHIMP_NEW_PRODUCTS_SUBJECT_LINEis an optional variable for the subject line of the products newsletter.MAILCHIMP_NEW_PRODUCTS_STOREFRONT_URLis an optional storefront URL to be used in the products newsletter.
Learn more about retrieving these variables in the tutorial
5. Install dependencies:
yarn # or npm install6. Setup and seed the database:
npx medusa db:setup
yarn seed # or npm run seed7. Start the Medusa application:
yarn dev # or npm run devYou can then use the /store/newsletter API route to subscribe an email.
If you have an existing Medusa application, copy the following directories and files into your project:
src/api/storesrc/api/middlewares.tssrc/modules/mailchimpsrc/subscriberssrc/workflows
Then, add the Mailchimp Module Provider to medusa-config.ts:
module.exports = defineConfig({
// ...
modules: [
{
resolve: "@medusajs/medusa/notification",
options: {
providers: [
{
resolve: "./src/modules/mailchimp",
id: "mailchimp",
options: {
channels: ["email"],
apiKey: process.env.MAILCHIMP_API_KEY!,
server: process.env.MAILCHIMP_SERVER!,
listId: process.env.MAILCHIMP_LIST_ID!,
templates: {
new_products: {
subject_line: process.env.MAILCHIMP_NEW_PRODUCTS_SUBJECT_LINE!,
storefront_url: process.env.MAILCHIMP_NEW_PRODUCTS_STOREFRONT_URL!,
}
}
},
},
],
},
},
]
})MAILCHIMP_API_KEY=
MAILCHIMP_SERVER=
MAILCHIMP_LIST_ID=
MAILCHIMP_NEW_PRODUCTS_SUBJECT_LINE=
MAILCHIMP_NEW_PRODUCTS_STOREFRONT_URL=Where:
MAILCHIMP_API_KEYis your Mailchimp API key.MAILCHIMP_SERVERis your Mailchimp server prefix. For example,us5.MAILCHIMP_LIST_IDis the ID of the audience list.MAILCHIMP_NEW_PRODUCTS_SUBJECT_LINEis an optional variable for the subject line of the products newsletter.MAILCHIMP_NEW_PRODUCTS_STOREFRONT_URLis an optional storefront URL to be used in the products newsletter.
Learn more about retrieving these variables in the tutorial
After that, install the Mailchimp SDK:
yarn add @mailchimp/mailchimp_marketing # or npm install @mailchimp/mailchimp_marketing
yarn add -D @types/mailchimp__mailchimp_marketing # or npm install @types/mailchimp__mailchimp_marketing --save-devYou can then use the /store/newsletter API route to subscribe an email.