Overview
The Contact Management API provides full CRUD operations for managing CRM contacts, including subscribers, leads, and members.
Methods
getAll
Retrieve all contacts in the CRM.
db.crm.getAll(): Contact[]
Example:
const contacts = db.crm.getAll();
console.log(`Total contacts: ${contacts.length}`);
getById
Find a specific contact by ID.
db.crm.getById(id: string): Contact | undefined
The contact object if found, undefined otherwise
Example:
const contact = db.crm.getById('c_12345');
if (contact) {
console.log('Contact:', contact.name, contact.email);
}
add
Create a new contact.
db.crm.add(contact: Omit<Contact, 'id'>): Contact
contact
Omit<Contact, 'id'>
required
Contact data without ID (auto-generated)
The newly created contact with generated ID
Example:
const newContact = db.crm.add({
name: 'Juan Pérez',
email: '[email protected]',
phone: '+56912345678',
role: 'Member',
status: 'Subscribed',
lastContact: '2024-03-15',
tags: ['Newsletter', 'Meditation']
});
update
Update an existing contact.
db.crm.update(contact: Contact): Contact[]
Complete contact object with updates
Updated array of all contacts
Example:
const contact = db.crm.getById('c_12345');
if (contact) {
contact.status = 'Subscribed';
contact.tags = [...contact.tags, 'VIP'];
db.crm.update(contact);
}
addLead
Quickly add a lead from a subscription form.
db.crm.addLead(lead: {
email: string;
source: string;
date: string;
status: string;
}): Promise<Contact>
Lead source (e.g., ‘Footer’, ‘Landing Page’)
Date captured (ISO format)
The created contact record
Example:
const lead = await db.crm.addLead({
email: '[email protected]',
source: 'Footer Newsletter',
date: new Date().toISOString(),
status: 'Subscribed'
});
addMultiple
Bulk import multiple contacts.
db.crm.addMultiple(contacts: Omit<Contact, 'id'>[]): Contact[]
contacts
Omit<Contact, 'id'>[]
required
Array of contacts to import
Array of newly created contacts with IDs
Example:
const imported = db.crm.addMultiple([
{ name: 'Ana García', email: '[email protected]', phone: '', role: 'Lead', status: 'Pending', lastContact: '2024-03-15', tags: [] },
{ name: 'Carlos López', email: '[email protected]', phone: '', role: 'Lead', status: 'Pending', lastContact: '2024-03-15', tags: [] }
]);
console.log(`Imported ${imported.length} contacts`);
interface Contact {
id: string;
name: string;
firstName?: string;
lastName?: string;
email: string;
phone: string;
role: string;
status: 'Subscribed' | 'Unsubscribed' | 'Bounced' | 'Pending' | 'new';
lastContact: string;
tags: string[];
notes?: string;
engagementScore?: number; // 0-100
mailrelayId?: string;
city?: string;
country?: string;
createdAt?: string;
listIds?: string[]; // List memberships
}
Integration with Mailrelay
Sync contacts with Mailrelay service.
db.mailrelay.syncContact(contact: Contact): Promise<Contact>
Contact to sync with Mailrelay
Updated contact with mailrelayId
Example:
const synced = await db.mailrelay.syncContact(contact);
console.log('Mailrelay ID:', synced.mailrelayId);