Errors
Every error is JSON with a message. Validation errors add an errors object.
There is no error code field, so branch on the status.
{ "message": "The url field must be a valid URL." }
Every status this API returns
| Status | Means | What to do |
|---|---|---|
401 |
Unauthenticated. No token, malformed token, or a revoked one |
Stop. Retrying will not help |
402 |
Not enough credits. Nothing was queued or charged. Carries credits_required and credits_available |
Top up by the difference, or shrink the request. See Credits |
403 |
Either Invalid ability provided. (read-only token on a write call) or This endpoint is not available to API tokens. (not part of the API at all) |
Stop. The first needs a different token, the second needs the dashboard |
404 |
Not found. The record does not exist, or belongs to another account |
Treat as gone. The two cases are deliberately indistinguishable |
409 |
A request with this Idempotency-Key is still in progress. |
Wait and retry the same key, or reconcile by reading. Never re-send under a new key |
422 |
Validation failed, or the action is not valid in the record's current state | Read message, and errors when present. Fix the request |
429 |
Rate limited. See Rate limits | Back off, honour Retry-After |
500 |
Our fault | Retry once with backoff, then alert |
A 422 covers two different things. Field validation returns errors:
{
"message": "The url field must be a valid URL.",
"errors": { "url": ["The url field must be a valid URL."] }
}
A state conflict does not. POST /searches for a workspace with no business
profile returns 422 with a sentence and no errors key. Use the presence of
errors to tell the two apart.
Money is never a 422. If a run is refused because the account cannot pay for
it, that is a 402 with the numbers on it, the same as every other metered
call.
What is safe to retry
| Safe | Why | |
|---|---|---|
Any GET |
Yes | Nothing changes |
401, 403, 404, 422 |
No | The same request will fail the same way |
429, 500, a timeout on a GET |
Yes, with backoff | Transient |
POST that spends credits, after a timeout |
Yes, with Idempotency-Key |
See below |
Retrying a credit-spending POST
Send an Idempotency-Key header: any string you generate, unique per
logical operation. A UUID per attempt-group is the usual choice.
curl -X POST "$BASE/website-analyses" \
-H "Authorization: Bearer $LEADCUBS_TOKEN" \
-H "Idempotency-Key: 7c3f1b2e-..." \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com"}'
Retry with the same key and you get the first attempt's response back,
with Idempotent-Replay: true on it. The work runs once and you are charged
once, however many times the request is delivered.
Rules worth knowing:
| Situation | What you get |
|---|---|
| Same key, same body, first call finished | The original status and body, plus Idempotent-Replay: true |
| Same key, same body, first call still running | 409. Wait and retry, or reconcile by reading |
| Same key, different body | 422. The key identifies the request, not just the attempt |
| Same key, different account | Nothing shared. Keys are scoped per account |
First call returned 5xx |
Not stored. Retry normally |
Keys expire after 24 hours. After that the same string is a new request.
Without the header everything behaves as it always did, so a quick curl stays
a one-liner. Anything automated should send one.
Two things are safe to repeat anyway
Independently of the header: enriching a lead that is already being enriched
returns 202 without charging again, and analysing a URL that is already being
analysed returns the run in progress. Those protect you even when no key is
sent, and they are why a double-click costs nothing.
A finished analysis of the same URL is a genuine re-run and does charge. Sites change, and that is the thing being paid for.
Debug output
In production, error bodies are just message and, where relevant, errors.
A local or staging instance with APP_DEBUG=on also returns exception,
file, line and a stack trace. Never parse those fields; they are not part
of the contract and are absent in production.