Import customers from CSV
A use case almost every integration starts with: you have companies and people in a spreadsheet or another system and want them in Scenid. The Customers app has no CSV import, so you script it with the SDK. The test data is a working example.
The recipe
- Create companies, parents first, and remember each
humanId. - Create people and remember their
humanId. - Add employments.
- Create groups and add members.
Every step needs the humanId of an earlier one, so keep a map from your own key (ref) to the humanId.
Core of the script
import-customers.mjs (excerpt)
import sdk from '@scenid/cloud-sdk'
const customers = sdk.asAdmin().Customers()
const ids = {}
const expectOk = ({ status, result }, label) => {
if (status >= 200 && status < 300) return result
throw new Error(`${label}: ${status} ${JSON.stringify(result)}`)
}
for (const row of companies) {
const created = expectOk(await customers.OU.create({
data: { ouType: row.ouType, name: row.name, legalForm: row.legalForm },
parent: ids[row.parentRef]
}, { return: true }), `create ${row.name}`)
ids[row.ref] = created.humanId
expectOk(await customers.OU.update(created.humanId, {
data: { hasAddress: true, street: row.street, houseNo: row.houseNo, zipCode: row.zipCode, city: row.city, countryCode: row.countryCode }
}), `update ${row.name}`)
}
The complete, runnable version (CSV parsing, contacts, employments, groups, cleanup) is in the test data downloads.
Avoid duplicates on re-run
Creation is not idempotent. To make imports repeatable:
- Look up before you create: search by a natural key such as the email or the company name.
- Or keep your own
ref → humanIdstate file (the test data script does this) and useupdatefor known rows.
const { result } = await customers.Entities.search({
entityType: 'ou',
filterModel: { logicOperator: 'and', items: [{ field: 'data.name', operator: 'equals', value: row.name }] },
idsOnly: true
})
const existing = result[0]?.humanId
Performance and limits
- Calls run one after another. For thousands of rows, run a few in parallel (for example 5 at a time). There is no bulk create endpoint for entities.
- The SDK has no retries or timeouts. Wrap calls in your own retry for
sdk/fetch-failed. - Check the
statusof every call. A validation error does not throw, it resolves withstatus: 400.
Validation errors you will meet
| Message | Fix |
|---|---|
name too short | Company names need at least 3 characters |
unknown legalForm | Use a catalog key such as gmbh, not "GmbH" |
birthDate invalid | Use YYYY-MM-DD |
website invalid | Must be a full URL including https:// |
| address rejected | Set hasAddress: true in the same update |