Overview
The Campaigns API allows you to create targeted email campaigns, send test emails, and launch mass communications to segmented audiences.
Campaign Operations
getAll
Retrieve all campaigns.
db.campaigns.getAll(): Campaign[]
Example:
const campaigns = db.campaigns.getAll();
console.log(`Total campaigns: ${campaigns.length}`);
getById
Get a specific campaign by ID.
db.campaigns.getById(id: string): Campaign | undefined
The campaign object if found
Example:
const campaign = db.campaigns.getById('camp_123');
if (campaign) {
console.log('Campaign:', campaign.name);
}
create
Create a new campaign.
db.campaigns.create(
data: Omit<Campaign, 'id' | 'createdAt' | 'metrics'>
): Campaign
Campaign status: ‘Draft’ | ‘Scheduled’ | ‘Sent’ | ‘Testing’
Target audience: ‘all’ | ‘subscribed’ | ‘list’
Required if recipientType is ‘list’
Initial recipient count (set to 0)
The created campaign with ID and timestamps
Example:
const campaign = db.campaigns.create({
name: 'March Newsletter 2024',
subject: 'Novedades de Marzo - CAFH',
content: '<h1>Boletín de Marzo</h1><p>Contenido...</p>',
status: 'Draft',
recipientType: 'subscribed',
recipientCount: 0
});
save
Update an existing campaign.
db.campaigns.save(campaign: Campaign): Campaign
Complete campaign object with updates
Example:
const campaign = db.campaigns.getById('camp_123');
if (campaign) {
campaign.subject = 'Updated Subject';
campaign.status = 'Testing';
db.campaigns.save(campaign);
}
delete
Delete a campaign.
db.campaigns.delete(id: string): void
Example:
db.campaigns.delete('camp_123');
Testing & Launch
sendTest
Send a test email before launching.
db.campaigns.sendTest(
campaignId: string,
testEmail: string
): Promise<void>
ID of the campaign to test
Email address to send test to
Example:
await db.campaigns.sendTest('camp_123', '[email protected]');
console.log('Test email sent');
Test emails are sent with “[TEST]” prefix in the subject line.
launch
Launch a campaign to all targeted recipients.
db.campaigns.launch(
campaignId: string
): Promise<{ recipientCount: number }>
ID of the campaign to launch
Object containing the number of recipients
Example:
const result = await db.campaigns.launch('camp_123');
console.log(`Campaign launched to ${result.recipientCount} recipients`);
How It Works:
-
Determines recipients based on
recipientType:
'all' → All contacts in CRM
'subscribed' → Only contacts with status ‘Subscribed’
'list' → Contacts in the specified list
-
Creates email log entries for each recipient
-
Queues emails via backend API
-
Updates campaign status to ‘Sent’ with metrics
Campaign Type
interface Campaign {
id: string;
name: string; // Internal name
subject: string; // Email subject
content: string; // HTML content
status: 'Draft' | 'Scheduled' | 'Sent' | 'Testing';
recipientType: 'all' | 'subscribed' | 'list';
listId?: string; // If recipientType === 'list'
recipientCount: number; // Snapshot at send time
createdAt: string;
sentAt?: string;
scheduledAt?: string;
testEmail?: string; // Last test recipient
metrics: {
sent: number;
opened: number;
clicked: number;
bounced: number;
};
}
Recipient Targeting
db.campaigns.create({
name: 'Announcement',
subject: 'Important Update',
content: '<p>...</p>',
status: 'Draft',
recipientType: 'all',
recipientCount: 0
});
Subscribed Only
db.campaigns.create({
name: 'Newsletter',
subject: 'Monthly News',
content: '<p>...</p>',
status: 'Draft',
recipientType: 'subscribed',
recipientCount: 0
});
Specific List
db.campaigns.create({
name: 'VIP Event Invite',
subject: 'Exclusive Invitation',
content: '<p>...</p>',
status: 'Draft',
recipientType: 'list',
listId: 'list_vip_members',
recipientCount: 0
});
Workflow Example
// 1. Create campaign
const campaign = db.campaigns.create({
name: 'Spring Campaign 2024',
subject: 'New Season, New Beginnings',
content: '<h1>Spring Newsletter</h1>',
status: 'Draft',
recipientType: 'subscribed',
recipientCount: 0
});
// 2. Send test
await db.campaigns.sendTest(campaign.id, '[email protected]');
// 3. Review and update
campaign.content = '<h1>Updated Content</h1>';
db.campaigns.save(campaign);
// 4. Launch to all recipients
const result = await db.campaigns.launch(campaign.id);
console.log(`Sent to ${result.recipientCount} people`);
// 5. Check metrics
const updated = db.campaigns.getById(campaign.id);
console.log('Metrics:', updated?.metrics);
Best Practices
Always Test First
Send test emails before launching to catch formatting issues
Segment Audiences
Use lists to target specific groups instead of blasting everyone
Monitor Metrics
Track open and click rates to improve future campaigns
Rate Limits
Remember the queue respects SMTP rate limits (80/hour default)