Sync contacts from your app
PreviewKeep Luso8 contacts up to date from your product's signups, with upserts, bulk imports and list enrollment.
Preview
This guide shows how to push people from your own product into Luso8 as they sign up or change their details, and how to add them to a list that a campaign can use. You will build an idempotent upsert: find the contact by email, update it if it exists, create it if it does not.
What you need
- A Luso8 workspace where you are an Owner or Administrator.
- An API key with the
contacts:writeandlists:writescopes. - The ID of the list you want new signups in. Open the list in Data›Lists and copy the ID from the URL, or call
GET /api/v1/lists.
Store the key as LUSO8_API_KEY in your server environment. Never send it to the browser.
1. Find a contact by email
search matches names, emails and phone numbers, so filter the results for an exact email match:
const BASE = 'https://api.luso8.rexplore.ai/api/v1';
const headers = {
Authorization: `Bearer ${process.env.LUSO8_API_KEY}`,
'Content-Type': 'application/json',
};
async function findContactByEmail(email) {
const url = new URL(`${BASE}/contacts`);
url.searchParams.set('search', email);
url.searchParams.set('limit', '10');
const res = await fetch(url, { headers });
if (!res.ok) throw new Error(`Search failed: ${res.status} ${await res.text()}`);
const contacts = await res.json();
return contacts.find((c) => c.email?.toLowerCase() === email.toLowerCase()) ?? null;
}2. Create or update it
Map your user fields to Luso8 contact fields. All fields are optional; send the ones you have. PUT only changes the fields you send.
| Luso8 field | Example |
|---|---|
email | ada@example.com |
first_name, last_name | Ada, Lovelace |
phone, mobile_phone | +265991234567 (E.164 recommended) |
company_name | Analytical Engines Ltd |
job_title | Head of Operations |
country, town | Malawi, Lilongwe |
source | website-signup |
notes | Free text |
async function upsertContact(user) {
const body = {
email: user.email,
first_name: user.firstName,
last_name: user.lastName,
phone: user.phone,
company_name: user.company,
job_title: user.jobTitle,
country: user.country,
source: 'website-signup',
};
const existing = await findContactByEmail(user.email);
const res = existing
? await fetch(`${BASE}/contacts/${existing.id}`, { method: 'PUT', headers, body: JSON.stringify(body) })
: await fetch(`${BASE}/contacts`, { method: 'POST', headers, body: JSON.stringify(body) });
if (!res.ok) throw new Error(`Upsert failed: ${res.status} ${await res.text()}`);
return res.json(); // the saved contact, including its id
}3. Add the contact to a list
The body takes one or more contact IDs. Contacts already in the list are left as they are.
async function addToList(listId, contactIds) {
const res = await fetch(`${BASE}/lists/${listId}/contacts`, {
method: 'POST',
headers,
body: JSON.stringify({ contact_ids: contactIds }),
});
if (!res.ok) throw new Error(`Add to list failed: ${res.status} ${await res.text()}`);
return res.json();
}
// In your signup handler:
const contact = await upsertContact(user);
await addToList(process.env.LUSO8_SIGNUPS_LIST_ID, [contact.id]);If a campaign is enrolling that list, new members can be added to the campaign from the campaign's Contacts tab, or with POST /api/v1/campaigns/{campaign_id}/contacts/from-lists (scope campaigns:write).
4. Backfill existing users in bulk
For a first import, send batches of contacts in one request instead of one call per user. The body is an array of the same contact objects you send to POST /api/v1/contacts.
async function backfill(users, batchSize = 200) {
for (let i = 0; i < users.length; i += batchSize) {
const batch = users.slice(i, i + batchSize).map((u) => ({
email: u.email,
first_name: u.firstName,
last_name: u.lastName,
company_name: u.company,
source: 'backfill',
}));
const res = await fetch(`${BASE}/contacts/bulk`, { method: 'POST', headers, body: JSON.stringify(batch) });
if (!res.ok) throw new Error(`Batch ${i / batchSize} failed: ${res.status} ${await res.text()}`);
}
}Run the backfill before you switch on real-time sync, and use upserts from then on so repeated events never create duplicates.
5. Handle rate limits and failures
Each key can make 600 requests per minute. A signup handler makes two or three calls per user, which is plenty for most products, but backfills and bursts can hit the limit:
- On
429, wait for theRetry-Afterheader and retry. See Rate limits for a ready-made helper. - On
5xxor network errors, retry with exponential backoff. The upsert is safe to repeat. - On
422, log thedetailarray: it names the invalid field. See Errors. - Don't block your own signup on Luso8. Run the sync in a background job or queue, so a slow request never delays your user.
Next steps
Last updated on