Import contacts from a spreadsheet
Create new contacts from a CSV, update the ones already on file, and tag them all.
The script reads each row, creates the contact, and when Flito answers that the email is already on file, updates that contact instead. Every row is then tagged, so you can find the import later. Running it twice changes nothing new.
contacts.csv
import.mjs
import.py
email,firstName,lastName,jobTitle
ada@acme.com,Ada,Lovelace,Head of Operations
grace@example.com,Grace,Hopper,CTOThe key needs contacts:read, contacts:write and tags:write: a read and write key made by a member or above has them.
// node import.mjs contacts.csv (Node 22 or later, no packages needed)
import { readFileSync } from 'node:fs'
const { FLITO_URL, FLITO_API_KEY } = process.env
const TAG = 'import-2026-11'
async function call(capability, input, { idempotencyKey } = {}) {
const res = await fetch(`${FLITO_URL}/api/cap/${capability}`, {
method: 'POST',
headers: {
Authorization: `Bearer ${FLITO_API_KEY}`,
'Content-Type': 'application/json',
...(idempotencyKey ? { 'Idempotency-Key': idempotencyKey } : {}),
},
body: JSON.stringify({ input }),
})
const body = await res.json()
if (!res.ok) throw Object.assign(new Error(body.message), { status: res.status, code: body.code })
return body.result
}
// A small CSV reader: a header row, commas, and "quoted, values" (one row per line).
function splitLine(line) {
const fields = []
let field = ''
let quoted = false
for (let i = 0; i < line.length; i++) {
const ch = line[i]
if (quoted && ch === '"' && line[i + 1] === '"') {
field += '"'
i++
} else if (ch === '"') {
quoted = !quoted
} else if (ch === ',' && !quoted) {
fields.push(field.trim())
field = ''
} else {
field += ch
}
}
fields.push(field.trim())
return fields
}
function readCsv(path) {
const [header, ...lines] = readFileSync(path, 'utf8').trim().split(/\r?\n/)
const keys = splitLine(header)
return lines.map((line) => Object.fromEntries(splitLine(line).map((v, i) => [keys[i], v])))
}
async function upsert(row) {
const fields = { firstName: row.firstName, lastName: row.lastName, jobTitle: row.jobTitle }
try {
const created = await call(
'contact.create',
{ email: row.email, ...fields },
{ idempotencyKey: `${TAG}:${row.email}` },
)
return { id: created.id, action: 'created' }
} catch (err) {
if (err.code !== 'CONFLICT') throw err
// The email is on file: update that contact instead.
const { rows } = await call('contact.list', { search: row.email, limit: 5 })
const existing = rows.find((c) => c.email?.toLowerCase() === row.email.toLowerCase())
if (!existing) throw err
await call('contact.update', { id: existing.id, ...fields })
return { id: existing.id, action: 'updated' }
}
}
for (const row of readCsv(process.argv[2] ?? 'contacts.csv')) {
try {
const { id, action } = await upsert(row)
await call('tag.apply', { objectType: 'contact', objectId: id, tags: [TAG], source: 'import' })
console.log(`${action} ${row.email} (${id})`)
} catch (err) {
console.error(`failed ${row.email}: ${err.status} ${err.code} ${err.message}`)
}
}What it shows
- Duplicates: a create whose email is on file answers
409 CONFLICT, so the script updates the contact on file. See Records. - Safe retries: the idempotency key is the tag and the email, so re-running after a network error never creates a second contact. See safe retries.
- Finding the import: the tag marks every contact the run touched. In the app, filter contacts by it; over the API, use a filter on interest tags.
For thousands of rows, the app's CSV import (Settings, Import and export) maps columns for you and can undo a whole run.
Was this page useful?