async function importContactsFromArray(contacts, listId) {
const BATCH_SIZE = 100; // Process in batches
for (let i = 0; i < contacts.length; i += BATCH_SIZE) {
const batch = contacts.slice(i, i + BATCH_SIZE);
const response = await fetch(
`{{api.baseUrl}}/api/campaigns/contact-lists/${listId}/contacts`,
{
method: 'POST',
headers: {
'x-api-key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
contacts: batch.map(contact => ({
mailId: contact.email,
firstName: contact.firstName,
lastName: contact.lastName,
companyName: contact.company,
jobTitle: contact.title
}))
})
}
);
if (!response.ok) {
const error = await response.json();
console.error(`Batch ${i / BATCH_SIZE + 1} failed:`, error);
// Handle error (retry, skip, etc.)
}
// Rate limiting: wait between batches
await new Promise(resolve => setTimeout(resolve, 1000));
}
}