> ## Documentation Index
> Fetch the complete documentation index at: https://docs.zerodrift.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Audit Historical Messages

> Check thousands of past messages at once with the batch API: upload a file, follow the batch, and download the results.

A batch checks many messages in one job. Use it to audit a back catalog, such as last quarter's outbound email, an export from your archive, or a CRM's message history. You upload one file, ZeroDrift checks every message against your rule packs, and you download the results as JSONL, a spreadsheet, an audit workbook and a PDF report.

Batches are for messages that have already been sent. To check a message before it goes out, use [Enforce Content](https://docs.zerodrift.com/api-reference/validate/validate-content.md).

| | Enforce Content | Batch |
| - | - | - |
| **Use it for** | One message, before it is sent | Up to 100,000 messages that were already sent |
| **Answer** | Seconds (`sync`) or a job to poll (`async`) | A batch to poll; minutes to hours, depending on size |
| **Rewrites** | Yes, on Flagship | No. Detection and verification only |
| **Custom rules** | Yes | Not yet. Managed rule packs only |
| **Output** | One verdict | JSONL, spreadsheet, audit workbook and PDF report |

> [!NOTE]
>
> You can also start an audit without writing code, from **Audits** in ZeroDrift Command or by [running an audit from Slack](https://docs.zerodrift.com/guides/slack-app/run-an-audit.md).

## Before you start

* A **full-access** API key. Read-only keys can follow and download batches but can't create them.
* A billing account with a card on file, under [Settings > Billing](https://app.zerodrift.ai/settings/billing). Without one, a batch is accepted and then fails with `pricing_unavailable`.
* At least one active rule pack. A batch checks against the rule packs active for your account, as [Enforce Content](https://docs.zerodrift.com/api-reference/validate/validate-content.md) does by default.

Check all three in one call with [Get Batch Limits](https://docs.zerodrift.com/api-reference/batches/get-batch-limits.md):

```bash theme={null}
curl -s https://api.zerodrift.ai/api/v3/batches/limits \
  -H "x-api-key: $ZERODRIFT_API_KEY"
```

```json theme={null}
{
  "api_version": "v3",
  "limits": {
    "max_active_batches": 8,
    "max_in_flight_items": 500000,
    "max_items": 100000,
    "max_upload_bytes": 268435456
  },
  "current": { "active_batches": 0, "in_flight_items": 0 },
  "credit": {
    "available": { "amount": "12.34", "currency": "USD" },
    "rate_per_unit": { "amount": "0.01", "currency": "USD" }
  },
  "billing": { "enforced": true, "has_account": true }
}
```

If `billing.has_account` is `false`, set up billing before you go on.

## 1. Write the file

Put one message per line in a [JSONL](https://jsonlines.org) file. Each line is a JSON object:

| Field | Required | Rules |
| - | - | - |
| `custom_id` | Yes | 1–64 letters, digits, `_` or `-`. Unique in the file. You use it to match results to your records. |
| `content` | Yes | The message text. Not empty, at most 1 MB. |
| `metadata` | No | Any JSON object, such as the sender, date or channel. It comes back with the item's result and as columns in the spreadsheet. |

```jsonl emails.jsonl theme={null}
{"custom_id":"email-001","content":"Hi Dana, attached is the agenda for Thursday's quarterly review.","metadata":{"sender":"advisor@example.com"}}
{"custom_id":"email-002","content":"Our fund will beat the market this year. Returns of 12-14% are guaranteed."}
{"custom_id":"email-003","content":"Heads up before tomorrow's announcement: Acme is being acquired, so buy now.","metadata":{"sender":"trader@example.com"}}
```

A file holds up to 100,000 lines and 256 MB.

## 2. Upload the file

Ask for an upload URL with [Create a Batch Upload URL](https://docs.zerodrift.com/api-reference/batches/create-upload-url.md), then `PUT` the file to it with every header in `required_headers`:

```bash theme={null}
UPLOAD=$(curl -s -X POST https://api.zerodrift.ai/api/v3/batches/uploads \
  -H "x-api-key: $ZERODRIFT_API_KEY" \
  -H "Content-Type: application/json" -d '{}')

UPLOAD_ID=$(jq -r .upload_id <<<"$UPLOAD")
UPLOAD_URL=$(jq -r .upload_url <<<"$UPLOAD")
HEADERS=()
while IFS=$'\t' read -r k v; do HEADERS+=(-H "$k: $v"); done \
  < <(jq -r '.required_headers | to_entries[] | "\(.key)\t\(.value)"' <<<"$UPLOAD")

curl -s -X PUT "$UPLOAD_URL" "${HEADERS[@]}" --data-binary @emails.jsonl
```

> [!WARNING]
>
> Send every header in `required_headers` exactly as given. The URL is signed with them, so a missing or changed header makes the upload fail. The URL expires at `expires_at`, 15 minutes after you ask for it.

> [!TIP]
>
> For a few thousand short messages you can skip the upload. Send them inline as `items` on [Create a Batch](https://docs.zerodrift.com/api-reference/batches/create-batch.md), up to about 5 MB in one request.

## 3. Check the file (optional)

[Check a Batch Upload](https://docs.zerodrift.com/api-reference/batches/check-upload.md) reads the whole file with the same rules a batch uses, without creating a batch or billing anything. Start the check, then poll [Get a Batch Upload Check](https://docs.zerodrift.com/api-reference/batches/get-upload-check.md) until `status` is `done`:

```bash theme={null}
curl -s -X POST https://api.zerodrift.ai/api/v3/batches/uploads/$UPLOAD_ID/inspect \
  -H "x-api-key: $ZERODRIFT_API_KEY"

curl -s https://api.zerodrift.ai/api/v3/batches/uploads/$UPLOAD_ID/inspect \
  -H "x-api-key: $ZERODRIFT_API_KEY"
```

```json theme={null}
{
  "api_version": "v3",
  "upload_id": "703c343c018b42ad88bdd56cc8175c0b",
  "status": "done",
  "kind": "jsonl",
  "row_count": 4,
  "row_count_exact": true,
  "sample_rows": [
    { "custom_id": "email-001", "content": "Hi Dana, attached is the agenda for Thursday's quarterly review." },
    { "custom_id": "email-002", "content": "Our fund will beat the market this year. Returns of 12-14% are guaranteed." }
  ],
  "problems": [
    { "line": 3, "code": "duplicate_id", "message": "custom_id 'email-002' already used on line 2" },
    { "line": 4, "code": "empty_text", "message": "content must be a non-empty string" }
  ],
  "problem_count": 2,
  "file_errors": [],
  "checked_at": "2026-12-02T19:46:33Z"
}
```

Fix the lines it lists, or create the batch with `skip_problem_rows: true` to drop them. Skipped lines are not billed.

## 4. Create the batch

> [!NOTE]
>
> **A batch is an audit.** There's no separate endpoint to create or start an audit. Creating a batch starts one, and every batch gets an audit report (the workbook and PDF) when it ends. Audits started in Command are batches too. Set `metadata.name` to give the batch its name in Command's Audits list.

Pass the `upload_id` to [Create a Batch](https://docs.zerodrift.com/api-reference/batches/create-batch.md). Set `metadata.name` so the batch is easy to find in Command:

```bash theme={null}
curl -s -X POST https://api.zerodrift.ai/api/v3/batches \
  -H "x-api-key: $ZERODRIFT_API_KEY" \
  -H "Content-Type: application/json" \
  -d "{\"upload_id\": \"$UPLOAD_ID\", \"skip_problem_rows\": true, \"metadata\": {\"name\": \"Q4 outbound review\"}}"
```

```json 202 Accepted theme={null}
{
  "api_version": "v3",
  "batch_id": "batch_703c343c018b42ad88bdd56cc8175c0b",
  "status": "validating",
  "created_at": "2026-12-02T19:46:37Z",
  "expires_at": "2026-12-03T19:46:37Z",
  "poll": { "method": "GET", "url": "/api/v3/batches/batch_703c343c018b42ad88bdd56cc8175c0b" }
}
```

To check against particular rule packs or rules rather than all your active ones, send `validation_scope`, as on [Enforce Content](https://docs.zerodrift.com/api-reference/validate/validate-content.md).

## 5. Follow the batch

Poll [Get a Batch](https://docs.zerodrift.com/api-reference/batches/get-batch.md) until `status` is `ended` or `failed`. Every minute is often enough.

```bash theme={null}
curl -s https://api.zerodrift.ai/api/v3/batches/$BATCH_ID \
  -H "x-api-key: $ZERODRIFT_API_KEY"
```

A batch moves through these statuses:

| Status | Meaning |
| - | - |
| `validating` | The file is being read and checked. |
| `in_progress` | Messages are being checked. `request_counts` and `verdict_counts` go up as they finish. |
| `canceling` | You canceled it. Messages not yet started are dropped. |
| `finalizing` | Every message is done. The results are being compiled. |
| `ended` | Done. The results are ready to download. |
| `failed` | The batch could not run. `failure.reason` says why, and nothing was billed. |

Read progress from three fields:

* `request_counts`: messages by outcome (`processing`, `succeeded`, `errored`, `canceled`, `expired`).
* `verdict_counts`: checked messages by verdict (`approved`, `needs_changes`, `do_not_send`, and `flagged`, the last two combined). `other` appears only when some messages got another verdict, such as `send with caution`.
* `cost`: `estimate` once the file is read, and `actual`, what was billed, once the batch ends.

```json Example: an ended batch theme={null}
{
  "api_version": "v3",
  "batch_id": "batch_3f2b8c1e9d0a4b7f8e6d5c4b3a291807",
  "status": "ended",
  "request_counts": { "processing": 0, "succeeded": 998, "errored": 2, "canceled": 0, "expired": 0 },
  "total_items": 1000,
  "skipped_items": 0,
  "verdict_counts": { "approved": 941, "needs_changes": 44, "do_not_send": 13, "flagged": 57 },
  "cost": {
    "currency": "USD",
    "estimate": { "units": 1000, "amount": "10.00" },
    "actual": { "units": 998, "amount": "9.98" }
  },
  "model_engine": "anchor_3_0",
  "metadata": { "name": "Q4 outbound review" },
  "created_at": "2026-10-05T12:01:00Z",
  "ended_at": "2026-10-05T12:40:00Z",
  "files_deleted_at": "2026-10-19T12:40:00Z",
  "report_status": "ready",
  "failure": null,
  "results_url": "https://example-bucket.s3.amazonaws.com/batch-results/batch_3f2b8c1e9d0a4b7f8e6d5c4b3a291807/results.jsonl?X-Amz-Algorithm=...",
  "files": {
    "jsonl": { "url": "https://example-bucket.s3.amazonaws.com/...?...", "expires_at": "2026-10-05T13:45:00Z" },
    "xlsx": { "url": "https://example-bucket.s3.amazonaws.com/...?...", "expires_at": "2026-10-05T13:45:00Z" },
    "workbook": { "url": "https://example-bucket.s3.amazonaws.com/...?...", "expires_at": "2026-10-05T13:45:00Z" },
    "pdf": { "url": "https://example-bucket.s3.amazonaws.com/...?...", "expires_at": "2026-10-05T13:45:00Z" }
  }
}
```

To stop a running batch, use [Cancel a Batch](https://docs.zerodrift.com/api-reference/batches/cancel-batch.md). Messages already checked keep their results and are billed. The rest are not.

## 6. Download the results

When the batch has `ended`, its `files` hold four downloads:

| File | What's in it |
| - | - |
| `jsonl` | One line per message, in the shape below. Use it to load the results into your own systems. |
| `xlsx` | The same results as a spreadsheet: `custom_id`, a column per `metadata` key, then `status`, `overall_status`, `flagged`, `rules`, `quotes` and `reason`. |
| `workbook` | The audit workbook: a cover sheet, the rules triggered, recommended actions, and the evidence behind each finding. Set once `report_status` is `ready`. |
| `pdf` | The audit report as a PDF. Set once `report_status` is `ready`. If the PDF can't be built, the other files are still available. |

Each link is valid for an hour. For a fresh link, get the batch again or call [Download a Batch File](https://docs.zerodrift.com/api-reference/batches/download-file.md).

Each line of the `jsonl` file has the message's `custom_id`, its `metadata` if it had any, and a `result`:

```jsonl theme={null}
{"custom_id": "email-002", "result": {"type": "succeeded", "validation": {"overall_status": "do not send", "units": 1, "cost": {"currency": "USD", "amount": "0.01"}, "...": "..."}}}
{"custom_id": "email-009", "result": {"type": "errored", "error": {"type": "...", "code": "...", "message": "..."}}}
```

`result.type` is `succeeded`, `errored`, `canceled` or `expired`. A `succeeded` result carries the verdict in `validation`, in the same shape as [Get Verdict](https://docs.zerodrift.com/api-reference/validate/get-results.md), plus what that message cost.

> [!WARNING]
>
> Download the files within 14 days. At `files_deleted_at` the input, results and reports are deleted and the links become `null`. The batch record and its counts stay until you [delete the batch](https://docs.zerodrift.com/api-reference/batches/delete-batch.md).

## Cost

Batches bill at the Anchor 3.0 (Flagship) validation price, whatever model tier your workspace uses: **\$0.01 per message** of up to 2,000 input tokens. There's no batch discount and no audit fee.

* A longer message is billed in units of 2,000 input tokens, rounded up. A 3,000-token email is 2 units, \$0.02.
* Your rate per unit is `credit.rate_per_unit` on [Get Batch Limits](https://docs.zerodrift.com/api-reference/batches/get-batch-limits.md).
* A 10,000-message audit of ordinary emails costs about **\$100**.
* `cost.estimate` is set when the file is read. `cost.actual` is what was billed, set when the batch ends.
* You're billed for messages that were checked. Skipped lines, canceled and expired messages, and batches that fail are not billed.

See [Pricing](https://docs.zerodrift.com/pricing.md) for the rates.

## Limits

| Limit | Value |
| - | - |
| Messages per batch | 100,000 |
| File size | 256 MB |
| Message size | 1 MB |
| Time to finish | 24 hours from creation (`expires_at`). Messages not done by then end as `expired` and are not billed. |
| Files kept | 14 days after the batch ends |
| Batches running at once, and messages in flight across them | Set by your plan. See `limits` on [Get Batch Limits](https://docs.zerodrift.com/api-reference/batches/get-batch-limits.md). |

Going over your plan's running-batch limits returns `429 batch_quota_exceeded`. There is no `Retry-After` header: wait for a batch to end, or cancel one, then try again. Batch limits are separate from the API's request rate limits.

## When a batch fails

A failed batch has `status: "failed"` and a `failure.reason`. Nothing is billed.

| `failure.reason` | Cause | Fix |
| - | - | - |
| `invalid_input` | A line broke the file rules. `failure.errors` lists up to 100, with line numbers. | Fix the lines, or create the batch with `skip_problem_rows: true`. |
| `pricing_unavailable` | Your account has no billing set up. | Add a card under [Settings > Billing](https://app.zerodrift.ai/settings/billing). |
| `quota_exceeded` | The batch would push you past your messages-in-flight limit. | Wait for a running batch to end, or split the file. |

Requests that are refused outright never create a batch:

| Response | Cause |
| - | - |
| `402` | No credit left. |
| `409` | That `upload_id` has already been used for a batch. |
| `422 batch_scope_unsupported` | The `validation_scope` asks for custom rules or a trained adapter, which batches can't run yet, or resolves to no rules at all. |
| `429 batch_quota_exceeded` | You're at your plan's limit of running batches or messages in flight. |

## Related

* [Run an Audit from Slack](https://docs.zerodrift.com/guides/slack-app/run-an-audit.md): audit a Slack channel without code.
* [Read the Results](https://docs.zerodrift.com/guides/slack-app/read-results.md): the audit page, workbook and report in Command.
* [Batches API reference](https://docs.zerodrift.com/api-reference/batches/create-batch.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.