# Batch jobs

Run thousands of API calls from one JSONL file and download the results.

A batch job runs one endpoint once for every line of a JSONL file you upload. Use it when you would otherwise send thousands of requests: creating the job takes one slot of your create-links [rate limit](/docs/partners-api/rate-limits.mdx), and its lines share that limit. A line that hits it waits and runs again instead of failing, so a big job paces itself.

All routes are under `https://www.ninjabuy.com/api/v1` and take your [bearer token](/docs/partners-api/authentication.mdx). Creating or cancelling a job needs the `links:create` permission; reading one needs `jobs:read` (see [API keys](/docs/partners-api/api-keys.mdx#permissions-and-limits)).

## Which endpoints

| Endpoint             | Max lines |
| -------------------- | --------- |
| `POST /api/v1/links` | 10,000    |

Not offered: `GET` endpoints (batch jobs only write), uploads (`POST /api/v1/uploads`, whose reservation expires in 15 minutes), and batch jobs inside batch jobs.

## 1. Create the job

```bash
curl -sS https://www.ninjabuy.com/api/v1/batch_jobs \
  -H "Authorization: Bearer $NINJABUY_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  --data '{"endpoint":{"path":"/api/v1/links","http_method":"post"},"skip_validation":false,"metadata":{"batch":"spring-haul"}}'
```

| Field                  | Rules                                                                                                                                         |
| ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `endpoint.path`        | Required. A path from the table above, such as `/api/v1/links`.                                                                               |
| `endpoint.http_method` | Required: `post` or `delete`.                                                                                                                 |
| `skip_validation`      | Required boolean. `false` checks the whole file before any line runs; `true` starts at once and answers a bad line with its own `400` result. |
| `metadata`             | Optional. Up to 20 string keys of at most 40 characters, with string values of at most 500.                                                   |

The answer is `201` with the job in `ready_for_upload`:

```json
{
  "id": "k57d2m9qx8",
  "object": "batch_job",
  "created": "2026-11-24T12:00:00.000Z",
  "endpoint": { "path": "/api/v1/links", "http_method": "post" },
  "skip_validation": false,
  "metadata": { "batch": "spring-haul" },
  "status": "ready_for_upload",
  "status_details": {
    "ready_for_upload": {
      "upload_url": { "url": "https://…", "expires_at": "2026-11-24T12:05:00.000Z" }
    }
  }
}
```

## 2. Upload the file

`PUT` the file to `upload_url.url` within 5 minutes. The URL is signed, so send no `Authorization` header. The job starts by itself once the file lands.

```bash
curl -sS -X PUT "$UPLOAD_URL" \
  -H "Content-Type: application/jsonlines" \
  -T input.jsonl
```

If nothing arrives in time the job ends as `upload_timeout`; create another.

## Input lines

One JSON object per line, UTF-8, at most 50 MB in all and 256 KB a line.

```json
{"id": "line_001", "params": {"links": ["https://weidian.com/item.html?itemID=7581746714"], "campaign": "spring-haul"}}
{"id": "line_002", "params": {"links": ["https://www.ninjabuy.com/search?q=hoodie"], "campaign": "spring-haul"}}
```

| Field         | Rules                                                                                   |
| ------------- | --------------------------------------------------------------------------------------- |
| `id`          | Required, unique in the file: 1 to 64 letters, digits, `_` or `-`.                      |
| `path_params` | Required when the path has placeholders, with exactly their names: `{"id": "..."}`.     |
| `params`      | Optional object, the request body you would send directly. Left out, the body is empty. |

Any other field is an error. A line is the same request a direct call would be: the same validation, the same answers and the same side effects.

## 3. Follow the job

```bash
curl -sS https://www.ninjabuy.com/api/v1/batch_jobs/JOB_ID \
  -H "Authorization: Bearer $NINJABUY_API_KEY"
```

`GET /api/v1/batch_jobs` lists your jobs, newest first, with `limit` and `cursor` and the answer `{ data, has_more, cursor }`. Each read of a job carries a fresh download URL. A batch job also appears in [`GET /api/v1/jobs/{id}`](/docs/partners-api/jobs.mdx) as kind `batch_job`.

`status_details` has one key, named after the status:

| `status`                          | `status_details`                                                   |
| --------------------------------- | ------------------------------------------------------------------ |
| `ready_for_upload`                | `upload_url` with `url` and `expires_at`.                          |
| `validating`                      | `validated_count`: lines read so far.                              |
| `in_progress`, `cancelling`       | `success_count` and `failure_count`.                               |
| `complete`, `canceled`, `timeout` | The counts and `output_file`.                                      |
| `batch_failed`                    | `error.message`, and `partial_output_file` when any line ran.      |
| `validation_failed`               | `error` with `message`, `line` and, when known, `id`. No line ran. |
| `upload_timeout`                  | Empty.                                                             |

The path of a normal job is `ready_for_upload`, `validating` (not with `skip_validation`), `in_progress`, `complete`. Counts are numbers. A line succeeded when its status is `2xx`.

## 4. Download the results

`output_file` holds `content_type`, `size` and `download_url` with `url` and `expires_at`. Fetch the URL without an `Authorization` header:

```bash
curl -sS "$DOWNLOAD_URL" -o results.jsonl
```

```json
{"id": "line_001", "status": 200, "response": {"results": [{"input": "https://weidian.com/item.html?itemID=7581746714", "status": "ok", "url": "https://www.ninjabuy.com/r/…"}], "warnings": [], "request_id": "…"}}
{"id": "line_002", "status": 400, "response": {"type": "https://www.ninjabuy.com/api/v1/problems/bad-body", "title": "Invalid request", "status": 400, "detail": "…", "request_id": "…"}}
```

`status` is the HTTP status the line would have got and `response` its body: a problem document when it failed. Match results to input lines by `id`, not by position. With `skip_validation: true`, a line that cannot be read is answered under `@line-<n>`, `n` its line number from 1, when it has no readable `id`. A repeated `id` fails the job even then. A line's `status` is the HTTP status of its call, so a line whose links could not be converted (unsupported or unreadable links) still answers `200`, and each link's own `status` in `response.results` says what happened. `failure_count` counts lines whose call failed (not `2xx`), not links.

## Cancel a job

```bash
curl -sS -X POST https://www.ninjabuy.com/api/v1/batch_jobs/JOB_ID/cancel \
  -H "Authorization: Bearer $NINJABUY_API_KEY"
```

The answer is the job: `canceled` if nothing had started, otherwise `cancelling` until the running lines finish and the partial results file is written. A job that already finished is not changed: cancelling it answers `200` with the job as it is.

If the API key that created a job is revoked or expires, the remaining lines stop: the job ends as `batch_failed` and keeps the results so far. A job created with an OAuth token keeps running after the token expires.

## Limits

* At most 5 unfinished jobs per account. A sixth create answers `409` `too-many-batch-jobs`.
* At most 50 MB and the endpoint's maximum lines per file.
* The upload URL lasts 5 minutes.
* A job runs at most 7 days; one still running then ends as `timeout`, with the results so far.
* Results can be downloaded for 30 days after the job finishes; the input file is deleted when it finishes. After that `output_file` is gone and the counts stay.
* Every line runs under its own idempotency key, so a retry after a failure replays the lines already run instead of running them again.

Stripe's `notification_suppression` is not offered. Errors are [problem JSON](/docs/partners-api/errors.mdx); `Idempotency-Key` on `POST /api/v1/batch_jobs` replays the create like any [create request](/docs/partners-api/links.mdx#idempotency-key).
