# Batch

```http
POST https://api.qelka.com/v1/batch
```

Runs up to 100 checks of any kind in one request and returns all the results at once. Costs the sum of its items: 1 per basic phone check, 3 per live phone lookup, 1 per email and 1 per IP. Items that fail are not billed.

Use it when you have a handful of values to check together, such as the email, phone number and IP address of one signup, or when you process a list in chunks. For files of thousands of rows or more, a [bulk job](/docs/api-reference/bulk) runs in the background instead.

## Request

```json
{
  "items": [
    { "type": "phone", "phone": "+14155550123" },
    { "type": "email", "email": "jane@example.com" },
    { "type": "ip", "ip": "8.8.8.8" },
    { "type": "ip", "ip": "10.0.0.1" }
  ]
}
```

| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| `items` | array | Yes | none | From 1 to 100 items. Each item is an object with a `type` and the fields of the matching endpoint. |
| `items[].type` | string | Yes | none | `phone`, `email` or `ip`. |

Each item takes the same fields, with the same defaults, as the request body of its endpoint:

| `type` | Fields | Reference |
|---|---|---|
| `phone` | `phone` (required), `country`, `level` (default `basic`), `fresh` (default `false`) | [Phone validation](/docs/api-reference/phone) |
| `email` | `email` (required), `mode` (default `full`), `fresh` (default `false`) | [Email verification](/docs/api-reference/email) |
| `ip` | `ip` (required), `fresh` (default `false`) | [IP lookup](/docs/api-reference/ip) |

You can mix types and levels freely: a live phone lookup next to a fast email check is fine.

## Response

```json
{
  "object": "list",
  "data": [
    {
      "id": "lkp_01M84BQ5RF9S2J873867GN3HJW",
      "object": "phone",
      "input": "+14155550123",
      "valid": true,
      "status": "valid",
      "e164": "+14155550123",
      "national": "(415) 555-0123",
      "country": { "code": "US", "name": "United States", "calling_code": "1" },
      "region": "California",
      "timezones": ["America/Los_Angeles"],
      "line_type": "fixed_line_or_mobile",
      "carrier": { "name": null, "mcc": null, "mnc": null, "source": "numbering_plan" },
      "ported": null,
      "credits_used": 1,
      "cached": false
    },
    {
      "id": "lkp_01M84BQ7EJ57QRMTCB4C7FG6YV",
      "object": "email",
      "input": "jane@example.com",
      "status": "deliverable",
      "reason": "accepted_email",
      "did_you_mean": null,
      "checks": {
        "syntax": true,
        "mx": true,
        "smtp": true,
        "catch_all": false,
        "disposable": false,
        "role": false,
        "free_provider": false
      },
      "domain": { "name": "example.com", "spf": true, "dmarc": true },
      "score": 100,
      "credits_used": 1,
      "cached": true
    },
    {
      "id": "lkp_01M84BQ94N3HXZ5NMDN5DKVCNC",
      "object": "ip",
      "input": "8.8.8.8",
      "version": 4,
      "country": { "code": "US", "name": "United States" },
      "region": null,
      "city": null,
      "asn": { "number": 15169, "org": "Google LLC", "type": "hosting" },
      "flags": { "tor": false, "vpn": false, "proxy": false, "datacenter": true },
      "risk": { "score": 35, "level": "medium" },
      "credits_used": 1,
      "cached": false
    },
    {
      "object": "error",
      "index": 3,
      "error": {
        "type": "unprocessable_input",
        "message": "10.0.0.1 is in Private-Use (RFC 1918) and has no public location."
      }
    }
  ],
  "credits_used": 3
}
```

Values in the examples are illustrative.

| Field | Type | Description |
|---|---|---|
| `object` | string | Always `list`. |
| `data` | array | One entry per item, in the order of `items`. Each entry is a `phone`, `email` or `ip` result, exactly as the single endpoints return it, or an error object. |
| `credits_used` | integer | Total credits debited for the batch: the sum of the `credits_used` of its results. Error items count for nothing. |

`data[i]` always answers `items[i]`. Read the `object` field of each entry to know whether it is a result or an error.

### Error items

An item that fails does not fail the batch. It takes its place in `data` as an error object:

| Field | Type | Description |
|---|---|---|
| `object` | string | Always `error`. |
| `index` | integer | Position of the item in `items`, starting at 0. |
| `error.type` | string | One of the [error types](/docs/errors), such as `unprocessable_input` for a private IP address, or `upstream_unavailable` for a live phone lookup whose provider failed. |
| `error.message` | string | A sentence for humans that explains the failure. |

Error items are not billed.

## Validation of the whole request

The request is checked as a whole before any item runs. If the body does not match the schema, the API answers `400 invalid_request` for the batch and nothing is billed. That covers:

- an empty `items` array, or more than 100 items;
- an item without `type`, or with a `type` other than `phone`, `email` or `ip`;
- an item with a field its endpoint does not accept, such as `mode` on a phone item;
- an item with a missing or out-of-range value, such as an empty `email`.

Values that are well formed but cannot be checked, such as a private IP address, do not fail the batch: they become error items. So do items that a restricted key may not run: with a key limited to `/email` and `/batch`, phone and IP items come back as `forbidden` error items while the email items run.

## Timing

A batch is synchronous: the response arrives when every item is done. Items run in parallel, and the whole batch counts as one request against your [rate limits](/docs/rate-limits). Email items in `full` mode wait for mail servers, so the slowest mailbox check sets the pace of the whole batch. When speed matters, send email items with `"mode": "fast"`, or split slow work into its own request. Set a client timeout that leaves room for the mailbox checks.

## Examples

```bash
curl https://api.qelka.com/v1/batch \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"items": [{"type": "phone", "phone": "+14155550123"}, {"type": "email", "email": "jane@example.com"}, {"type": "ip", "ip": "8.8.8.8"}, {"type": "ip", "ip": "10.0.0.1"}]}'
```

```javascript
const items = [
  { type: 'phone', phone: '+14155550123' },
  { type: 'email', email: 'jane@example.com' },
  { type: 'ip', ip: '8.8.8.8' },
  { type: 'ip', ip: '10.0.0.1' },
]

const response = await fetch('https://api.qelka.com/v1/batch', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ items }),
  signal: AbortSignal.timeout(30000),
})
const { data, credits_used } = await response.json()

data.forEach((entry, index) => {
  if (entry.object === 'error') {
    console.warn(`Item ${entry.index} failed: ${entry.error.type}`)
    return
  }
  console.log(items[index].type, entry.status ?? entry.risk.level)
})
console.log(`Credits used: ${credits_used}`)
```

The response is the one shown above.

## Errors

These errors concern the whole request and use the regular [error envelope](/docs/errors):

| Status | Type | When |
|---|---|---|
| 400 | [invalid_request](/docs/errors#invalid_request) | The body does not match the schema, as described above |
| 401 | [invalid_api_key](/docs/errors#invalid_api_key) | The key is missing, invalid or revoked |
| 402 | [insufficient_credits](/docs/errors#insufficient_credits) | Your balance cannot cover the items of the batch. Credits for every item are reserved up front, and the unused part is returned with the response. |
| 403 | [forbidden](/docs/errors#forbidden) | The key is restricted and does not include `/batch` |
| 429 | [rate_limited](/docs/errors#rate_limited) | Too many requests; wait `Retry-After` seconds |
| 500 | [internal_error](/docs/errors#internal_error) | Unexpected error on our side; retry with backoff |

None of these responses is billed.

## Related

- [Validate signup forms](/docs/guides/validate-signup-forms) checks one signup with a single batch call.
- [Clean an email list](/docs/guides/clean-an-email-list) processes a list in chunks of 100.
