Discover how to integrate v0 with Mailchimp effortlessly. Our step-by-step guide simplifies connecting your accounts for automated marketing success.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
mailchimp.ts. This file will contain the code for sending subscription requests to Mailchimp.mailchimp.ts. This code defines a function that uses Mailchimp’s API to add a subscriber. Be sure to replace YOURMAILCHIMPAPIKEY and YOURMAILCHIMPLISTID with your actual Mailchimp API key and List ID.
// mailchimp.ts
export interface SubscribeData {
email: string;
firstName?: string;
lastName?: string;
}
export async function subscribeUser(data: SubscribeData): Promise {
// Replace with your Mailchimp API key and List ID
const apiKey = 'YOURMAILCHIMPAPI_KEY';
const listId = 'YOURMAILCHIMPLIST_ID';
// Extract the data center from the API key (e.g., "us1" if key ends with "-us1")
const dc = apiKey.split('-')[1];
const url = https://${dc}.api.mailchimp.com/3.0/lists/${listId}/members;
// Prepare your subscription data
const payload = {
email_address: data.email,
status: "subscribed",
merge_fields: {
FNAME: data.firstName || '',
LNAME: data.lastName || ''
}
};
// Mailchimp API uses Basic Authentication. The username can be any string.
const auth = 'anystring:' + apiKey;
const encodedAuth = typeof btoa === 'function'
? btoa(auth)
: Buffer.from(auth).toString('base64'); // For environments without btoa
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + encodedAuth
},
body: JSON.stringify(payload)
});
if (!response.ok) {
const errorDetail = await response.json();
throw new Error('Error subscribing user: ' + JSON.stringify(errorDetail));
}
return await response.json();
}
signup.ts or an API handler file).subscribeUser function from mailchimp.ts and uses it within a function that processes incoming subscription data.
// signup.ts
import { subscribeUser } from './mailchimp';
// Example: a function that handles a subscription request
export async function handleSignup(req: any, res: any): Promise {
// Assume req.body contains the email, firstName, and lastName fields from a signup form
const { email, firstName, lastName } = req.body;
try {
const result = await subscribeUser({ email, firstName, lastName });
res.status(200).json({ message: 'Subscription successful', data: result });
} catch (error) {
res.status(500).json({ error: error.message });
}
}
fetch and btoa.fetch (for example, in some older Node.js versions), you will need to integrate a polyfill.node-fetch from a CDN or local copy if your project setup allows it.
// If you need a fetch polyfill and your environment does not support fetch, insert this code at the top of your main TypeScript file.
// (Ensure you have a way to load external scripts or include the polyfill code in your project)
// Example using an import from a CDN is not possible directly in TypeScript without bundling,
// so instead, consider embedding a fetch polyfill code if your platform supports it.
MAILCHIMPAPIKEY and MAILCHIMPLISTID.mailchimp.ts to read these values from the environment configuration instead of hardcoding them.
// In mailchimp.ts (modified to read from environment configuration)
const apiKey = process.env.MAILCHIMPAPIKEY || 'YOURMAILCHIMPAPI_KEY';
const listId = process.env.MAILCHIMPLISTID || 'YOURMAILCHIMPLIST_ID';
handleSignup.When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.