Learn how to seamlessly integrate Lovable with Campaign Monitor using our step-by-step guide to boost your email marketing strategy and efficiency.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
CampaignMonitorService.ts in your project's src/services directory. This file will contain the code to interact with Campaign Monitor's API.
export interface Subscriber {
email: string;
name?: string;
}
export class CampaignMonitorService {
private apiKey: string;
private listId: string;
private baseUrl: string;
constructor(apiKey: string, listId: string) {
this.apiKey = apiKey;
this.listId = listId;
// Base URL for Campaign Monitor API v3.2
this.baseUrl = 'https://api.createsend.com/api/v3.2';
}
public async addSubscriber(subscriber: Subscriber): Promise {
const url = ${this.baseUrl}/subscribers/${this.listId}.json;
const body = {
EmailAddress: subscriber.email,
Name: subscriber.name || '',
Resubscribe: true // Ensures that existing subscribers are updated
};
// Authorization using Basic Auth (Campaign Monitor requires an API key)
// The API key is encoded with a dummy password parameter ':x'
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(this.apiKey + ':x'),
},
body: JSON.stringify(body),
});
if (!response.ok) {
const errorData = await response.text();
throw new Error(Error adding subscriber: ${errorData});
}
return response.json();
}
}
YOURCAMPAIGNMONITORAPIKEY and YOURCAMPAIGNMONITORLISTID with your actual Campaign Monitor credentials.
subscribe.ts in your project's src directory. This file will handle user subscriptions by using the service you just created.
import { CampaignMonitorService, Subscriber } from './services/CampaignMonitorService';
// Replace with your actual Campaign Monitor API key and list ID
const apiKey = 'YOURCAMPAIGNMONITORAPIKEY';
const listId = 'YOURCAMPAIGNMONITORLISTID';
const campaignMonitor = new CampaignMonitorService(apiKey, listId);
// Function to subscribe a user to your Campaign Monitor list
export async function subscribeUser(email: string, name?: string) {
const subscriber: Subscriber = { email, name };
try {
const result = await campaignMonitor.addSubscriber(subscriber);
console.log('Subscriber added successfully:', result);
} catch (error) {
console.error('Failed to add subscriber:', error);
}
}
subscribeUser function into that file.subscribeUser function when a user submits their email and name.
import { subscribeUser } from './subscribe';
// Assuming your subscription form has the ID "subscription-form"
const form = document.getElementById('subscription-form');
if (form) {
form.addEventListener('submit', async (event) => {
event.preventDefault();
// Assuming input fields with IDs "email" and "name"
const emailInput = document.getElementById('email') as HTMLInputElement;
const nameInput = document.getElementById('name') as HTMLInputElement;
const email = emailInput.value;
const name = nameInput.value;
await subscribeUser(email, name);
});
}
fetch API, which is supported in most browsers.index.html within the <head> section:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/fetch.umd.js"></script>
This prompt helps an AI assistant understand your setup and guide you through the fix step by step, without assuming technical knowledge.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.