← Guides
Sync Contacts with ERP
Keep your CRM contacts in sync with your ERP system using pagination and incremental sync.
Prerequisites
- A Service Account (Client ID + Client Secret)
- ERP system with a contacts API or database
- A scheduled job or cron to run the sync periodically
Step 1: Authenticate
Obtain an access token using client credentials:
cURL
curl -X POST https://magnetcustomer.net/realms/platform/protocol/openid-connect/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=YOUR_ID&client_secret=YOUR_SECRET"Step 2: Fetch contacts with pagination
Use page and limit parameters to iterate through all contacts:
cURL
curl "https://minha-empresa.platform-api.magnetcustomer.com/api/contacts?page=1&limit=100" \
-H "Authorization: Bearer YOUR_TOKEN"Response 200
{
"docs": [
{
"_id": "507f1f77bcf86cd799439011",
"lifeCycle": "customer",
"fullname": "Joao Silva",
"email": "joao@empresa.com.br",
"phones": [{ "number": "11988880000", "typePhone": "cellphone", "ddi": "+55" }],
"updatedAt": "2026-01-15T14:30:00Z"
}
],
"total": 1147,
"pages": 12,
"page": 1,
"limit": 100
}Step 3: Map fields to ERP format
Transform the CRM contact structure to match your ERP schema:
Node.js
function mapCrmToErp(contact: CrmContact): ErpContact {
return {
erpId: contact._id,
name: contact.fullname,
email: contact.email,
phone: contact.phones?.[0]?.number ?? null,
// Map custom fields
taxId: contact.customFields?.find(f => f.k === 'cpf')?.v ?? null,
updatedAt: contact.updatedAt,
};
}Step 4: Incremental sync using updatedAt
The contact list has no date filter. Sort by the most recent change and stop when you reach a contact older than the last sync:
cURL
# Most recently changed first: stop when you reach a contact older than the last sync
curl "https://minha-empresa.platform-api.magnetcustomer.com/api/contacts?page=1&limit=100&sortBy=updatedAt&sortType=desc" \
-H "Authorization: Bearer YOUR_TOKEN"Full sync loop (Node.js)
Node.js
import { MagnetCustomer } from '@magnetcustomer/sdk';
const crm = new MagnetCustomer({
account: 'minha-empresa',
clientId: process.env.MAGNET_CLIENT_ID,
clientSecret: process.env.MAGNET_CLIENT_SECRET,
});
async function syncContactsToErp(lastSyncAt?: string) {
const startedAt = new Date().toISOString();
for (let page = 1; ; page++) {
const { data } = await crm.contacts.listContacts({
page,
limit: 100,
sortBy: 'updatedAt',
sortType: 'desc',
});
for (const contact of data.docs ?? []) {
// Sorted by most recent change: everything from here on was already synced
if (lastSyncAt && contact.updatedAt <= lastSyncAt) return finish(startedAt);
await erp.contacts.upsert(mapCrmToErp(contact));
}
if (page >= (data.pages ?? 0)) return finish(startedAt);
}
}
// Persist sync timestamp for next run
async function finish(startedAt: string) {
await db.set('lastContactSync', startedAt);
}Next: Learn how to setup webhooks to receive real-time contact updates instead of polling.