Import from a file
Load companies and deals from a file with the API, in a way that is safe to run again after a failure.
On this page
The API has no file upload. Your code reads the file and calls the operations, one row at a time. Two things make it safe to run again: each row has a fixed operationId and an externalId.
Prepare the rows
- Give every row the number your own system uses for it, such as a customer number. It becomes the
externalId. - Name the
ownerof each company with the identifier of a person of your account. An unknown owner is refused withowner_not_found. - Define the custom fields first, if the file has columns for them.
- Import companies before their deals and persons: a deal and a person name the
idof their company, which you read byexternalId.
One row, one operation
Derive the operationId from the kind of record and the row number, so that the same row always sends the same request. A row that was imported before then gets its first outcome back, and nothing is created twice.
import {createHash} from 'node:crypto';
import {readFileSync} from 'node:fs';
// A UUID that depends only on its text: the same row always gives the same operationId.
const uuidOf = text => {
const hex = createHash('sha1').update(text).digest('hex');
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-4${hex.slice(13, 16)}-8${hex.slice(17, 20)}-${hex.slice(20, 32)}`;
};
const call = async (method, path, body) => {
const response = await fetch(`${process.env.RDC_BASE_URL}/crm/v1${path}`, {
method,
headers: {Authorization: `Bearer ${process.env.RDC_TOKEN}`, 'Content-Type': 'application/json'},
body: body && JSON.stringify(body),
});
return {status: response.status, body: await response.json()};
};
const rows = readFileSync('companies.csv', 'utf8').trim().split('\n').slice(1).map(line => line.split(','));
for (const [number, name, cif, owner, town, county] of rows) {
const answer = await call('POST', '/companies', {
operationId: uuidOf(`import:company:${number}`), externalId: number, name, cif, owner, town, county,
});
if (answer.status !== 202) console.log(number, answer.status, answer.body.code);
} Run it again
| Situation | Answer |
|---|---|
| The row is unchanged and was imported: the same request again. | 202 |
The row was edited after it was imported: the same operationId with a different request. | 409 operation_reused |
| The row is new but its number is already used by another record. | 409 external_id_taken |
Too many requests: the row was not processed, so the same request is sent again after the seconds in Retry-After. | 429 rate_limited |
The owner is not a person of your account. | 404 owner_not_found |
For an edited row, read the company by externalId and send Update a company with its revision.
Check the result
Read the companies back with List companies and check them: follow nextCursor to the end, as in Pagination. Every row that answered 202 is in the list, found by its externalId.