# Files and batches API

## GET /api/v1/admin/batches

`GET /api/v1/admin/batches` · operation `listBatchesAdmin`

Requires batches and batches:read; team roles see their teams. Each batch carries mode (gateway or provider, T215) and, for a provider batch, provider_id, phase (staging, submitting, submitted, collecting), provider_batch_id, provider_state, submitted_at and polls.

**Authentication:** `adminSession`

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Batches | `application/json`: object |
| `404` | Feature disabled | — |

Response `200` fields:

- `application/json`: object
  - `data` (array of object)
  - `provider_batches` ([ProviderBatchesConfig](/docs/api/schemas/p/#providerbatchesconfig))


## POST /api/v1/admin/batches/{id}/cancel

`POST /api/v1/admin/batches/{id}/cancel` · operation `cancelBatchAdmin`

Requires batches:manage (admin, or team_admin of the batch team).

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Cancelled | — |
| `404` | Feature disabled or not visible | — |


## GET /v1/files

`GET /v1/files` · operation `listFiles`

Files of the key team; purpose filters. Requires batches and key scope batches:write; objects belong to the key team.

**Authentication:** `bearerAuth`

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | OK | — |
| `400` | Invalid input | — |
| `404` | Feature disabled or not visible to the key team | — |


## POST /v1/files

`POST /v1/files` · operation `uploadFile`

Multipart file and purpose (batch, evals, user_data, assistants and, with fine_tuning, fine-tune), at most 50 MiB; UTF-8 except PDF and images with assistants or user_data, which a vector store indexes through vector_stores.ocr_model. Batch files are validated line by line at upload: unique custom_id, POST, one supported url, a model the key may use, no streaming. fine-tune files must be JSONL of objects. Requires batches and key scope batches:write; objects belong to the key team. With streaming_file_uploads (T168) the file part is stored in 1 MiB chunks as it arrives, every check runs on the stream and the 50 MiB limit answers 413 as soon as it is passed; a single file part is accepted, fine-tune lines are limited to 4 MiB and each replica streams at most 4 uploads at a time (503 uploads_busy with Retry-After otherwise).

**Authentication:** `bearerAuth`

**Request body** (required)

- `multipart/form-data`: object
  - `file` (string (binary), required)
  - `purpose` (string, required, one of `"batch"`, `"evals"`, `"user_data"`, `"assistants"`, `"fine-tune"`)

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | OK | — |
| `400` | Invalid input | — |
| `404` | Feature disabled or not visible to the key team | — |


## GET /v1/files/{id}

`GET /v1/files/{id}` · operation `getFile`

File object. Requires batches and key scope batches:write; objects belong to the key team.

**Authentication:** `bearerAuth`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | OK | — |
| `400` | Invalid input | — |
| `404` | Feature disabled or not visible to the key team | — |


## DELETE /v1/files/{id}

`DELETE /v1/files/{id}` · operation `deleteFile`

Deletes the file content. Requires batches and key scope batches:write; objects belong to the key team.

**Authentication:** `bearerAuth`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | OK | — |
| `400` | Invalid input | — |
| `404` | Feature disabled or not visible to the key team | — |


## GET /v1/files/{id}/content

`GET /v1/files/{id}/content` · operation `getFileContent`

Raw content (JSONL for batch files and outputs). Requires batches and key scope batches:write; objects belong to the key team.

**Authentication:** `bearerAuth`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | OK | — |
| `400` | Invalid input | — |
| `404` | Feature disabled or not visible to the key team | — |


## GET /v1/batches

`GET /v1/batches` · operation `listBatches`

Batches of the key team. Requires batches and key scope batches:write; objects belong to the key team.

**Authentication:** `bearerAuth`

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | OK | — |
| `400` | Invalid input | — |
| `404` | Feature disabled or not visible to the key team | — |


## POST /v1/batches

`POST /v1/batches` · operation `createBatch`

input_file_id, endpoint (/v1/chat/completions, /v1/embeddings or /v1/responses), completion_window 24h and metadata. The gateway runs each line through its own pipeline as the key, so any provider works and quotas, guardrails and the ledger apply per line; a revoked key stops its batch. With customer_portal, a key trusted to assert customer identity may send X-Gateway-Customer-Org, -Team and -Member (resolved as for a client request, T158.006): every line is charged to that customer and recorded with it, and a line is refused when the key loses its trust or the customer is deleted or disabled while the batch runs; a key with require_identity must name one (422 customer_identity_required). Requires batches and key scope batches:write; objects belong to the key team. T215, flag provider_batches: when every line names an alias with batch provider of one provider, the file goes to that provider's own batch API instead (OpenAI, Anthropic, Gemini API, Vertex AI, Bedrock): each line is staged through the pipeline (guardrails, a blocked line never sent; its worst case held on the budget at the batch tier), the staged lines are uploaded once, a leased job polls the provider, and each result is replayed through the pipeline and valued and settled at the batch tier; a file over the remaining budget ends failed before upload. A file mixing such aliases with others, or two providers, is refused with 400.

**Authentication:** `bearerAuth`

**Request body** (required)

- `application/json`: object
  - `input_file_id` (string, required)
  - `endpoint` (string, required)
  - `completion_window` (string, required, one of `"24h"`)
  - `metadata` (object)

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | OK | — |
| `400` | Invalid input | — |
| `404` | Feature disabled or not visible to the key team | — |


## GET /v1/batches/{id}

`GET /v1/batches/{id}` · operation `getBatch`

Batch with request_counts and, when done, output_file_id and error_file_id in the OpenAI line format. Requires batches and key scope batches:write; objects belong to the key team.

**Authentication:** `bearerAuth`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | OK | — |
| `400` | Invalid input | — |
| `404` | Feature disabled or not visible to the key team | — |


## POST /v1/batches/{id}/cancel

`POST /v1/batches/{id}/cancel` · operation `cancelBatch`

Stops a batch; finished lines keep their results. A provider batch (T215) answers status cancelling: the gateway cancels it at the provider, collects the lines that ran, releases the holds of the rest and ends it cancelled. Requires batches and key scope batches:write; objects belong to the key team.

**Authentication:** `bearerAuth`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | OK | — |
| `400` | Invalid input | — |
| `404` | Feature disabled or not visible to the key team | — |


## GET /v1/messages/batches

`GET /v1/messages/batches` · operation `listMessageBatches`

T168: the key team's message batches, newest first. Message batches are listed only here, never by GET /v1/batches.

**Authentication:** `bearerAuth`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `limit` | query | integer, default `20`, min 1, max 1000 |  |
| `after_id` | query | string | Batches older than this one |
| `before_id` | query | string | Batches newer than this one |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Page of message batches | `application/json`: object |
| `400` | Invalid limit or both cursors | — |
| `404` | Feature disabled | — |

Response `200` fields:

- `application/json`: object
  - `data` (array of [MessageBatch](/docs/api/schemas/m/#messagebatch), required)
  - `has_more` (boolean, required)
  - `first_id` (string or null, required)
  - `last_id` (string or null, required)


## POST /v1/messages/batches

`POST /v1/messages/batches` · operation `createMessageBatch`

T168, flag message_batches (requires batches and anthropic_messages): the Anthropic Message Batches API shape on the gateway batch engine. Each request runs later through /v1/messages as the key that created the batch, for any provider behind the alias, with budgets, quotas, guardrails, the customer and the usage ledger per request; no provider-native discount. Up to 10000 requests and 50 MiB; custom_id is 1-64 letters, digits, _ or -, unique; params is a Messages request with model, max_tokens and messages, no stream, on a model the key may use with capability messages. Requires key scopes batches:write and route:messages; batches belong to the key team. Window 24 hours, then the remaining requests expire.

**Authentication:** `bearerAuth`

**Request body** (required)

- `application/json`: object
  - `requests` (array of object, required, min items 1, max items 10000)
    - `custom_id` (string, required, pattern `^[a-zA-Z0-9_-]{1,64}$`)
    - `params` (object, required) — A Messages request body without stream.

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | message_batch object | `application/json`: [MessageBatch](/docs/api/schemas/m/#messagebatch) |
| `400` | Invalid request, custom_id, model or params | — |
| `403` | scope_forbidden (no route:messages), team_required or entitlement_forbidden | — |
| `404` | Feature disabled | — |


## GET /v1/messages/batches/{id}

`GET /v1/messages/batches/{id}` · operation `getMessageBatch`

T168: one message batch of the key team. A batch of another team, or an OpenAI-style batch, is 404.

**Authentication:** `bearerAuth`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | message_batch object | `application/json`: [MessageBatch](/docs/api/schemas/m/#messagebatch) |
| `404` | Feature disabled or not visible to the key team | — |


## DELETE /v1/messages/batches/{id}

`DELETE /v1/messages/batches/{id}` · operation `deleteMessageBatch`

T168: deletes an ended message batch with its requests and results and deletes its input file. Usage events and ledger rows of its requests stay.

**Authentication:** `bearerAuth`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Deleted | `application/json`: object |
| `404` | Feature disabled or not visible to the key team | — |
| `409` | batch_not_ended: cancel it or wait until it ends | — |

Response `200` fields:

- `application/json`: object
  - `id` (string, required)
  - `type` (`"message_batch_deleted"`, required)


## POST /v1/messages/batches/{id}/cancel

`POST /v1/messages/batches/{id}/cancel` · operation `cancelMessageBatch`

T168: stops a message batch. Requests already answered keep their results; the others come back canceled. The batch ends at once (processing_status ended), except a provider batch (T215), which reads canceling until the provider batch is cancelled and its answered requests are collected.

**Authentication:** `bearerAuth`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | message_batch object | `application/json`: [MessageBatch](/docs/api/schemas/m/#messagebatch) |
| `404` | Feature disabled or not visible to the key team | — |


## GET /v1/messages/batches/{id}/results

`GET /v1/messages/batches/{id}/results` · operation `messageBatchResults`

T168: the results of an ended message batch as JSON Lines, in request order, streamed a page at a time. Each line is {custom_id, result} where result is {type: succeeded, message} (the Messages response), {type: errored, error} (an Anthropic error object, {type: error, error: {type, message}}), {type: canceled} or {type: expired}. Kept 29 days after creation.

**Authentication:** `bearerAuth`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | JSON Lines of results | `application/x-jsonl`: string |
| `404` | Feature disabled, not visible to the key team, or results_expired after 29 days | — |
| `409` | batch_not_ended | — |

