Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readReactNode

Subscribe to Mailchimp from Next.js

Build a Next.js Pages Router endpoint that sends newsletter signups to Mailchimp without exposing your API key.

On this site, I have created a Newsletter Subscribing system. This is the Pages Router implementation I used to send the form’s email address to Mailchimp from the server.

Get the Mailchimp settings

Create a Mailchimp API key and find your audience ID. Copy the server prefix from the part of your API key after the last hyphen, such as us6.

Create .env.local in the project root and restart your Next.js server after setting these values. Keep the key in the server environment and out of Git.

MAILCHIMP_API_KEY=your-api-key
MAILCHIMP_LIST_ID=your-audience-id
MAILCHIMP_SERVER_PREFIX=us6

Create the subscribe route

Run npm install axios and save this as pages/api/subscribe.js. Next.js maps that file to /api/subscribe and parses JSON request bodies. Mailchimp accepts HTTP Basic authentication with the API key as the password.

The form posts the email to /api/subscribe; the serverless route adds the API key from .env.local and calls Mailchimp, so the key never reaches the browser.
import axios from 'axios'

export default async function subscribe(req, res) {
  if (req.method !== 'POST') {
    res.setHeader('Allow', 'POST')
    return res.status(405).json({ error: 'Use POST.' })
  }

  const email = typeof req.body?.email === 'string' ? req.body.email.trim() : ''
  if (!email) return res.status(400).json({ error: 'Enter your email address.' })

  const {
    MAILCHIMP_API_KEY: key,
    MAILCHIMP_LIST_ID: listId,
    MAILCHIMP_SERVER_PREFIX: server,
  } = process.env
  if (!key || !listId || !server) {
    return res.status(500).json({ error: 'Newsletter signup is unavailable.' })
  }

  try {
    await axios.post(
      `https://${server}.api.mailchimp.com/3.0/lists/${listId}/members`,
      { email_address: email, status: 'pending' },
      { auth: { username: 'newsletter', password: key }, timeout: 5000 },
    )
    return res.status(201).json({ message: 'Check your email to confirm.' })
  } catch (error) {
    if (error.response?.status === 400) {
      return res.status(400).json({ error: 'Mailchimp could not add this address.' })
    }
    return res.status(502).json({ error: 'Newsletter signup is unavailable.' })
  }
}

The add-member endpoint uses pending to send a confirmation email before subscribing the address.

Send the email from the form

With the Next.js server running, test with an address you control.

curl -i http://localhost:3000/api/subscribe \
  -H 'Content-Type: application/json' \
  --data '{"email":"replace-with-your-email@example.com"}'

A successful request returns 201. Have your form send the same JSON in a POST and display the confirmation message. Handle failed responses and rate-limit the route before putting it on a public page.

Update, November 2026. This example uses Pages Router API Routes. In a project using the App Router, define the POST endpoint in an App Router Route Handler.