Skip to main content

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​

  1. Create companies, parents first, and remember each humanId.
  2. Create people and remember their humanId.
  3. Add employments.
  4. 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 → humanId state file (the test data script does this) and use update for 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 status of every call. A validation error does not throw, it resolves with status: 400.

Validation errors you will meet​

MessageFix
name too shortCompany names need at least 3 characters
unknown legalFormUse a catalog key such as gmbh, not "GmbH"
birthDate invalidUse YYYY-MM-DD
website invalidMust be a full URL including https://
address rejectedSet hasAddress: true in the same update