# Client onboarding

Your website's sign-up form creates the customer in CarFleet and files each KYC scan straight onto
their card — and your site never holds a document number.

**Server to server only.** Call these endpoints from your website's backend, never from the
browser: the key would be readable by anyone who opens the page. The browser only ever sees the
one-file `uploadUrl` your backend hands it.

## The key

**Settings → API & MCP → Create key**, access **Client onboarding**. It may find, create and update
customers, add their drivers, signatories and beneficial owners, upload their documents and read
where each document stands. It reads no booking, invoice or payment, and no document number or file.
It works with this REST API only — the MCP endpoint refuses it.

**What never leaves.** No endpoint accepts an Emirates ID, passport or licence number — they reach
the card only by reading the uploaded scan — and none returns one, nor a file or a file URL. Answers
are ids, types, statuses, check codes and dates. Bank details sent on create or update are stored
and never returned.

Base URL `https://carfleet.ae/api`. Every call: `Authorization: Bearer cf_live_sk_…`. Every POST and
PATCH: `Idempotency-Key: <unique per request>` and `Content-Type: application/json`.

## The flow

1. **Find or create the customer** — `POST /v1/customers`. A person is matched by phone, then
   e-mail, among individuals; a company by trade licence, then TRN, among companies.

   **Verify before you match.** CarFleet cannot tell whether the person typing a phone number owns
   it. A match is honoured only when you send `"identityVerified": true` — meaning YOU confirmed the
   person controls that phone or e-mail (an OTP they typed back, or the personal link you sent to
   that number). Without it, a match answers `409` with `error.reason` `identity_unverified` and
   nothing is touched; verify, then send again with a new Idempotency-Key. Never set the flag for
   details you have not verified: it is what stops a stranger who knows a client's number from
   attaching a form — and scans — to that client's card. The same goes for `GET …/lookup`: use its
   answer only for an identifier you verified.

   On a verified match nothing is renamed or overwritten — only the existing customer's empty fields
   are filled — and you get `200 { id, created: false, matchedOn, ignored }`; `ignored` names the
   fields you sent that were not applied (already filled, or `bankDetails` — see below). Otherwise
   `201 { id, created: true }`. Store the `id`. `GET /v1/customers/lookup?phone=&email=&tradeLicenseNo=&trn=`
   answers the same question without creating anything (exact matches; URL-encode `+` as `%2B`).

   ```bash
   curl https://carfleet.ae/api/v1/customers \
     -H "Authorization: Bearer $CARFLEET_KEY" \
     -H "Idempotency-Key: 7d0c6f1e-signup-42" \
     -H "Content-Type: application/json" \
     -d '{ "type": "individual", "fullName": "Sara Ahmed", "phone": "+971 50 123 4567",
           "email": "sara@example.com", "nationality": "AE", "residencyStatus": "resident",
           "identityVerified": true }'
   ```

   Fields: `type` (`individual` | `corporate`), `fullName`, `fullNameAr`, `phone`, `email`,
   `nationality`, `dob` (YYYY-MM-DD), `address`, `emirate`, `residencyStatus` (`resident` |
   `tourist` | `gcc_national`); a company also `companyName` (required), `companyNameAr`,
   `tradeLicenseNo`, `tradeLicenseExpiry`, `trn`, `legalAddress`, `companyContact { name, title,
   phone, email }`; anyone `bankDetails { bankName, accountName, iban, swift }` (IBAN checked);
   `identityVerified` (create only). An empty string in an optional field means "not given". Unknown
   keys are refused.

   `PATCH /v1/customers/{id}` takes the same fields minus `type` and answers `{ id, ignored }`. On a
   customer this API created it applies as sent (`null` or `""` clears a field; a name cannot be
   cleared). On a customer your form did NOT create — one the operator already had, found by a
   verified match — it only fills EMPTY fields: what the operator's staff recorded is never changed
   or cleared from a website. **Bank details** (where refunds and cheques are paid) are written only
   on a customer this API created; on any other card they are ignored and the operator enters them.

2. **Add the people on the card** — `POST /v1/customers/{id}/drivers` (relationship `spouse`,
   `family`, `friend`, `employee`, `hired_driver` or `other`) → `{ driverId }`. For a company:
   `POST …/signatories` → `{ signatoryId }` and `POST …/beneficial-owners` → `{ id }`
   (`ownershipPercent` 0–100, two decimals, at most 100% in total).

   A signatory is the person who signs for the company, or its general manager —
   `"generalManager": true` (stored as the title `General Manager`; a `title` that names another
   role is a 422 — a "Managing Director" is another role). The primary signatory is the one who signs: send `"isPrimary": true` for that
   person (it takes over from the previous primary; the first signatory is primary anyway). Add the
   owners and the signatories BEFORE you register the company's papers — each paper is checked
   against the people on the card when it is registered. See [What a company sends](#what-a-company-sends).

3. **Upload each scan — two calls and a PUT.**

   ```bash
   # a) ask for an upload URL
   curl https://carfleet.ae/api/v1/customers/0b6c…/documents/uploads \
     -H "Authorization: Bearer $CARFLEET_KEY" -H "Idempotency-Key: 7d0c6f1e-eid-front" \
     -H "Content-Type: application/json" \
     -d '{ "holder": { "kind": "customer" }, "type": "emirates_id", "side": "front",
           "filename": "eid-front.jpg", "contentType": "image/jpeg", "sizeBytes": 482113 }'
   # → 201 { "path": "…", "uploadUrl": "https://…", "expiresAt": "…" }   (URL valid 2 h, one file)

   # b) send the bytes straight to storage — no CarFleet auth header on this one
   curl -X PUT "$UPLOAD_URL" -H "Content-Type: image/jpeg" --data-binary @eid-front.jpg

   # c) register it (reads the scan before answering — use a client timeout of 60 s or more)
   curl https://carfleet.ae/api/v1/customers/0b6c…/documents --max-time 90 \
     -H "Authorization: Bearer $CARFLEET_KEY" -H "Idempotency-Key: 7d0c6f1e-eid-front-register" \
     -H "Content-Type: application/json" \
     -d '{ "holder": { "kind": "customer" }, "path": "…", "type": "emirates_id", "side": "front" }'
   # → 201 { "documentId": "9a21…", "status": "verified", "verificationSource": "auto", "reasons": [] }
   ```

   `holder` is `{ "kind": "customer" }`, `{ "kind": "driver", "id": … }` or
   `{ "kind": "signatory", "id": … }`. Optional on both calls: `subtype`, `issuedDate`, `expiryDate`
   (YYYY-MM-DD). PUT the file with the SAME `Content-Type` you declared — a file stored as any other
   type is refused and deleted. Registering the same path again (also two registrations racing)
   answers 200 with the same document. An upload you never register is deleted about three hours
   after it was minted; while 60 of one customer's uploads (1,000 for the workspace) wait to be
   registered, new upload URLs are refused with `429` `pending_uploads`.

4. **Tell the client what to fix** — `GET /v1/customers/{id}/documents/status`:

   ```json
   { "customerId": "0b6c…",
     "documents": [ { "documentId": "9a21…", "holder": { "kind": "customer", "id": "0b6c…" },
                      "type": "emirates_id", "subtype": null, "side": "front", "status": "verified",
                      "verificationSource": "auto", "reasons": [], "rejectionReason": null,
                      "expiryDate": "2028-03-14",
                      "createdAt": "2026-11-22T10:02:11.402Z" } ],
     "missing": [ { "holder": { "kind": "customer", "id": "0b6c…" }, "type": "emirates_id", "code": "missing_back" } ] }
   ```

   `missing` names what a holder still owes: a card side (`missing_front` / `missing_back`) —
   and, for a company, its general manager (`missing_general_manager`, on the company itself, type
   `emirates_id`, until a signatory with `generalManager: true` is on the card) and the Emirates ID
   of the general manager and of each person who signs (`missing_document`, on that signatory, until
   one is on file that is not rejected).

   Or subscribe to the webhooks below and never poll.

## What a company sends

| When | Document | `holder` | Checked against |
|---|---|---|---|
| Always | `trade_license` | the company | its name and licence number |
| Always | `moa_aoa` | the company | its name and one of the people on the card — its manager counts |
| Always | `shareholder_register` — a share certificate or the shareholders register: ONE file listing the shareholders (send it even when the MOA lists them) | the company | its name, that it is a paper of who owns the company, and EVERY beneficial owner you added, each by his whole name |
| Always | `emirates_id` of the general manager | his signatory | his name |
| VAT-registered | `vat_certificate` | the company | its name |
| The signatory is not on the trade licence | `power_of_attorney` (or a board resolution) | the company | its name and someone on the card who is not its general manager — he grants it, so his name on it proves nothing |
| No longer asked | `ubo_declaration` | — | still accepted, for papers already on file |

**The person who signs.** Ask whether he is the general manager.
- **Yes** — one signatory, `{ "fullName": …, "generalManager": true, "isPrimary": true }`, and his
  Emirates ID: it is the general manager's too.
- **No** — the signer, `{ "fullName": …, "title": …, "isPrimary": true }`, and his Emirates ID (he
  signs); and the general manager, `{ "fullName": …, "generalManager": true }`, and HIS Emirates ID.

A signatory's passport and visa are not asked for — unless he drives. **Drivers** keep the full
package: passport, visa (not for a UAE national), Emirates ID and UAE driving licence. A signatory
who drives sends it on his own signatory record (the contract prints the primary signatory's
licence as the company's driver — with no primary, the oldest signatory who signs, never a general
manager who is not the primary); anyone else who drives is a driver (`POST …/drivers`).

A shareholders register that names the company and every owner is verified automatically. Each
owner must appear by his whole name in one entry of the paper — any order, at most one more word
in it (a "bin") — never pieced together from two other people's names or read out of the
company's own name; an owner whose name is one word is always compared by a person. One that
leaves an owner out (or spells one so differently it cannot be matched) goes to a person with
reason `shareholder_mismatch` — it is never rejected for a name; with no owner on the card yet,
with `manual_legal_check`. So does a paper that does not read as one of who owns the company — a
share certificate, a register of shareholders, members or partners, a certificate of incumbency,
the memorandum or articles of association: a trade licence or a power of attorney filed as the
register goes to a person (`manual_legal_check`), whoever it names. Adding an owner, or renaming
one, after a register was verified automatically sends that register back to a person
(`needs_review` / `shareholder_mismatch`, with a status webhook): it named the owners as they were.

## What a scan may be

| Rule | Detail |
|---|---|
| Files | `image/jpeg`, `image/png`, `image/webp`, `image/heic`, `image/heif`, `application/pdf`; at most 15 MiB. Checked against the file's own bytes at registration — a file that is not what it claims is deleted. |
| Types | `passport`, `emirates_id`, `drivers_license`, `intl_license`, `visa`, `residence_permit`, `proof_of_address`, `trade_license`, `vat_certificate` (the TRN certificate), `moa_aoa`, `power_of_attorney` (or a board resolution), `ubo_declaration` (no longer asked for — kept for papers on file), `shareholder_register` (a share certificate or the shareholders register), `other`. |
| Sides | `front`, `back` or `full`. Only an Emirates ID or a driving licence has a `back` (anything else sent as `back` is a 422). An Emirates ID and a UAE driving licence need both sides — two scans, or one `full` PDF; the status listing names a missing side. A single-sided foreign licence: send it as `full`. A `full` Emirates ID (the UAE PASS PDF holds both faces) counts as both sides when the reading sees both faces — the front with the photo and the back with the machine-readable zone. One face alone sent as `full` waits with reason `missing_back` (the front alone) or `missing_front` (the back alone), and the listing asks for that face; once it is uploaded the `full` scan is decided on its own checks and a status webhook follows. Unsure which faces it shows: reason `low_confidence`, a person looks (nothing to ask the client). |
| Proof of address | `subtype` is required: `ejari` (a current tenancy contract — `expiryDate` = contract end, today or later), `title_deed` (no age limit), `dewa` or `utility_bill` (any other utility — `issuedDate` = bill date, no older than 3 calendar months, Dubai time). A failure is 422 with `error.code` `proof_of_address_missing_subtype`, `…_missing_date`, `…_too_old`, `…_expired` or `…_invalid_date`; nothing is filed, and the uploaded file is KEPT — correct the fields and register the same `path` again (new Idempotency-Key), within 30 minutes of the upload URL's expiry. |

## Statuses and what to tell the client

Checks run automatically after the scan is read: is it the declared document, is it legible, has it
expired, is it this person's (or this company's), does it match what the card already holds. A
person looks only at what the checks could not decide.

**`verificationSource` says who decided.** `auto` is the automatic checker. It compares the scan
with the card — and with what your form itself sent (the name, say) — so an automatic `verified`
means "the checks found nothing wrong", not "a person accepted it". `staff` is a person at the
operator; `null`, nobody yet. If your process needs a human's acceptance before KYC counts as done,
wait for `verified` with `verificationSource: "staff"`.

| `status` | Meaning |
|---|---|
| `received` | Stored; checks still running (legal papers are read in the background). The final status arrives as a webhook. |
| `verified` | Every check passed (`auto`), or staff accepted it (`staff`). |
| `needs_review` | A person will decide — nothing for the client to do yet. A document no automatic rule covers (a residence permit, `other`) goes here at once with reason `manual_check`; so does an identity card (passport, Emirates ID, driving licence, IDP) the card check was not sure about, once its other checks pass — only a confident answer counts. The card check reads photos and PDFs alike: an identity PDF is drawn on our servers to a picture of each page (up to four), and only those pictures are read — what the pages visibly show, never text a viewer would not show. So is every other PDF the checks decide from: a visa, a trade licence, a proof of address, a legal paper — and one of those with more than four pages comes here too (`manual_check`, or `manual_legal_check` for a legal paper): only its first four pages were read. A scan the check saw no identity card on at all comes here with `wrong_document` (a person looks; it is not rejected for that alone), and so does a PDF that holds several documents (a passport and a licence, two people's cards) or has more than four pages — send each card on its own. A PDF that needs a password to open comes here at once with `encrypted_pdf` — send one without a password; a PDF that only restricts printing or editing is read like any other. A PDF that cannot be drawn (damaged, or too heavy to draw in time) is tried again and then comes here with `ocr_unavailable`. |
| `rejected` | Ask for a new scan. `reasons` says why; `rejectionReason` is a person's own words, meant for the client. |

| Reason code | Say to the client |
|---|---|
| `expired` | The document has expired — upload a valid one. |
| `too_old` | The bill is older than 3 months — upload a recent one. |
| `unreadable` | We could not read the photo — upload a sharper one. |
| `low_confidence` | Parts were hard to read; a person is checking. |
| `wrong_document` | This is not the document asked for. |
| `name_mismatch` | The name does not match; a person is checking. |
| `data_mismatch` | It differs from what we already have; a person is checking. |
| `missing_front` / `missing_back` | Upload the other side of the card. |
| `shareholder_mismatch` | The shareholders on the paper are not the owners you entered; a person is checking. |
| `manual_legal_check` | A legal document a person reads. |
| `manual_check` | No automatic check applies to this document (or, for an identity card the card check was unsure of, none could confirm it is that card); a person reads it. |
| `ocr_unavailable` | Automatic reading was unavailable — the scan could not be read after several attempts, or the reading service was down for more than two hours; a person is checking. |
| `encrypted_pdf` | The PDF needs a password to open: send it again without one (a copy without a password). |

## Retries, errors and limits

Every POST and PATCH needs an `Idempotency-Key` unique to that request. Retry with the same key after
a timeout or a **5xx** — a 5xx is never stored, so the retry runs again. A completed answer (2xx, or
a 4xx other than 429) is replayed for 24 h to the same key: after fixing a 4xx (say, a 422 because the file had not
finished uploading), send the corrected request with a NEW key.

Errors have one shape: `{ "error": { "code", "message", "reason"? } }`.

| Status | When |
|---|---|
| 400 | No Idempotency-Key, or a body that is not JSON. |
| 401 / 403 | The key is wrong, revoked or expired / its role does not allow this, or the workspace is read-only (`reason: read_only`) or frozen. |
| 404 | No such customer — or that driver / signatory is not this customer's. |
| 409 | The key was used for a different request, or the first one is still running — or (`reason: identity_unverified`) a customer matches but `identityVerified` was not sent. |
| 422 | The body breaks a rule. `error.reason` lists the fields, or names the rule: `content_type`, `too_large`, `empty`, `content_mismatch` (the file was deleted — upload again), `missing_upload`, `upload_expired`, `proof_of_address_…` (the file was kept — fix and register again). |
| 429 | Too many calls for this key this minute — wait `Retry-After` seconds; `reason` names the bucket. Or `reason: pending_uploads` — register the files already uploaded first. A 429 is never stored: retry with the same key. |

| Bucket | Per key, per minute |
|---|---|
| `lookup` | 60 — `GET /v1/customers/lookup` |
| `upload` | 30 — `POST …/documents/uploads` |
| `write` | 120 — every other POST / PATCH |
| `read` | 600 — every other GET |

Every answer carries `x-ratelimit-limit` and `x-ratelimit-remaining`.

## Webhooks

Register an endpoint in Settings → API & MCP (see [/docs/webhooks.md](/docs/webhooks.md)). Customer
events carry ids, types, statuses, codes and field NAMES only — never a name, phone, e-mail, number,
file or free text. Read a person's rejection reason through the status endpoint.

| Event | `data` |
|---|---|
| `customer.created` | `customerId, customerType, source` |
| `customer.updated` | `customerId, fields` (the names of the fields that changed) |
| `customer.document_added` | `customerId, documentId, holder { kind, id }, documentType, subtype, side, status, source, previousDocumentId?` |
| `customer.document_status_changed` | `customerId, documentId, holder { kind, id }, documentType, status, previousStatus, verificationSource, reasons` |

A document moved to another person on the card arrives as a new `document_added` with a new
`documentId`; `previousDocumentId` names the old one.

Every field of every endpoint is in the API reference (`/docs/api`, tags **Customers** and
**Customer documents**) and in `GET /api/v1/openapi.json`.
