# Errors

Errors use HTTP status codes and a JSON body with a stable machine-readable `code` and a human `message` (the message may change; match on `code`):

```json
{ "error": { "code": "insufficient_credits", "message": "Your credit balance is 0. Add credits in the dashboard to keep verifying." } }
```

## Status codes

| Status | Code | What to do |
|---|---|---|
| 200 | | Done |
| 202 | | Accepted: still checking (`state: "pending"`) or processing in the background; poll |
| 401 | `unauthorized` | Send `Authorization: Bearer mf_live_...` with a live key |
| 402 | `insufficient_credits` | Add credits; nothing was charged |
| 403 | `missing_scope` | Use a key with the needed scope |
| 403 | `email_not_verified` | Confirm the account email address |
| 403 | `ip_not_allowed` | Call from an address in the key's IP allowlist, or edit the list in the dashboard |
| 404 | `not_found` | Wrong id, or it belongs to another account |
| 409 | | The list is not finished yet (download) |
| 413 | `too_large` | Body or file over the limit; upload a file or split it |
| 415 | `unsupported_media_type` | Send JSON or multipart as documented |
| 422 | `invalid_request` | Fix the request; the message says what is wrong |
| 429 | `rate_limited`, `limit_reached`, `too_many_open_lists` | Wait `Retry-After` seconds (when sent) and retry |
| 500 | `internal` | Retry with backoff; safe with an `Idempotency-Key` |
| 503 | `busy`, `not_configured` | Temporarily unavailable; retry later |

Other endpoint-specific codes (for example `already_monitored`, `monitor_limit`, `too_many_hooks`) are listed per endpoint in the [API reference](/docs/api).

## Retries

- Retry 429, 500, 502, 503 and 504 with exponential backoff, honouring `Retry-After`.
- Send an `Idempotency-Key` on `POST /v1/verify` and `/v1/verify/batch` so a retry never charges twice.
- Do not retry 4xx errors other than 429 without changing the request.

## Tools endpoints

`/v1/tools/*` answer errors as `{"error": "message"}` with the same status codes (429 also carries `retry_after` in the body).

## Handling errors in code

```bash
curl -sS -w '\nHTTP %{http_code}\n' https://api.mails.fit/v1/verify \
  -H "Authorization: Bearer $MAILSFIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"email": "jane@example.com"}'
```

```javascript
const res = await fetch("https://api.mails.fit/v1/verify", { method: "POST", headers, body });
if (!res.ok && res.status !== 202) {
  const { error } = await res.json();
  if (res.status === 429) await sleep(Number(res.headers.get("Retry-After") ?? 1) * 1000);
  throw new Error(`${res.status} ${error.code}: ${error.message}`);
}
```

```python
res = requests.post(url, headers=headers, json=body, timeout=30)
if res.status_code == 429:
    time.sleep(int(res.headers.get("Retry-After", "1")))
elif res.status_code >= 400:
    err = res.json()["error"]
    raise RuntimeError(f"{res.status_code} {err['code']}: {err['message']}")
```

```go
if res.StatusCode >= 400 {
	var e struct {
		Error struct{ Code, Message string } `json:"error"`
	}
	json.NewDecoder(res.Body).Decode(&e)
	return fmt.Errorf("%d %s: %s", res.StatusCode, e.Error.Code, e.Error.Message)
}
```

```php
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($status >= 400) {
    $err = json_decode($body, true)["error"];
    throw new RuntimeException("$status {$err['code']}: {$err['message']}");
}
```

```rust
let res = client.post(url).bearer_auth(&key).json(&body).send().await?;
if res.status().is_client_error() || res.status().is_server_error() {
    let status = res.status();
    let err: serde_json::Value = res.json().await?;
    anyhow::bail!("{status} {}: {}", err["error"]["code"], err["error"]["message"]);
}
```
