Background jobs and batches
Run large builds without holding a connection open, and apply one build to many documents.
A request to /v1/build waits for its result and may run for up to two minutes. For larger files,
long OCR runs or many documents, start a job instead: the request answers at once, the work
runs in the background for up to 15 minutes, and you collect the result when it is ready.
| Endpoint | What it does |
|---|---|
POST /v1/jobs | Starts a build in the background. Takes exactly what /v1/build takes. Answers 202 with the job. |
POST /v1/batch | Starts the same build over many documents (2 to 50). Answers 202 with the job. |
GET /v1/jobs/{id} | The job's state. |
GET /v1/jobs/{id}/result | The result, once the job has succeeded. |
DELETE /v1/jobs/{id} | Cancels a job that is still running, or deletes its result. |
A large build in the background
job=$(curl https://api.nexpdf.dev/v1/jobs \
-H "Authorization: Bearer $NEXPDF_API_KEY" \
-F doc=@scanned-archive.pdf \
-F 'instructions={
"parts": [
{
"file": "doc"
}
],
"actions": [
{
"type": "ocr",
"languages": "eng+por"
},
{
"type": "optimize"
}
]
}' | jq -r .id)
# Poll until it has finished (jq reads the JSON).
while :; do
status=$(curl https://api.nexpdf.dev/v1/jobs/$job -H "Authorization: Bearer $NEXPDF_API_KEY" | jq -r .status)
[ "$status" = succeeded ] && break
[ "$status" = failed ] && { echo "the job failed" >&2; exit 1; }
sleep 2
done
curl https://api.nexpdf.dev/v1/jobs/$job/result -H "Authorization: Bearer $NEXPDF_API_KEY" -o archive-searchable.pdfThe job answers like this while it runs, and gains result_url, content_type and expires_at
when it has succeeded:
{ "id": "job_8f2c1d9a03b4e57c6a1f0e2d", "status": "running", "created_at": 1796048144, "started_at": 1796048145, "units": 0 }status is queued, running, succeeded or failed. A failed job has an error with the same
code and message a request would have answered with. Asking for the result before the job has
finished answers 409 not_ready with retry-after; asking for the result of a failed job answers
409 job_failed.
The client libraries poll for you: waitForJob (wait_for_job, WaitForJob, …) checks every two
seconds and returns the job when it has succeeded, or raises its error when it has failed.
Batches
A batch applies one build to many documents. The instructions take the document from the file
named document; send the documents as document1, document2, … (uploads or
links, document3_url). Any other file the instructions name, such as
a signing certificate or a cover page, is shared by every document.
The result is a ZIP holding one file per document that succeeded, named after its field
(document1.pdf). The job's items lists how each document went, so one damaged file does not
fail the others:
{
"id": "job_…",
"status": "succeeded",
"units": 6,
"items": [
{ "name": "document1", "status": "succeeded", "units": 2 },
{ "name": "document2", "status": "succeeded", "units": 2 },
{ "name": "document3", "status": "failed", "units": 0, "error": { "code": "processing_failed", "message": "…" } }
]
}job=$(curl https://api.nexpdf.dev/v1/batch \
-H "Authorization: Bearer $NEXPDF_API_KEY" \
-F document1=@invoice-1041.pdf \
-F document2=@invoice-1042.pdf \
-F document3_url=https://files.example.com/invoices/1043.pdf \
-F 'instructions={
"parts": [
{
"file": "document"
}
],
"actions": [
{
"type": "redact",
"query": "iban",
"mode": "pattern"
}
],
"output": {
"type": "pdf"
}
}' | jq -r .id)
# Poll until it has finished (jq reads the JSON).
while :; do
status=$(curl https://api.nexpdf.dev/v1/jobs/$job -H "Authorization: Bearer $NEXPDF_API_KEY" | jq -r .status)
[ "$status" = succeeded ] && break
[ "$status" = failed ] && { echo "the job failed" >&2; exit 1; }
sleep 2
done
curl https://api.nexpdf.dev/v1/jobs/$job/result -H "Authorization: Bearer $NEXPDF_API_KEY" -o invoices-redacted.zipCosts and limits
- A job costs what the same build would cost; a batch, the sum of its documents. Units are reserved when the job starts and only what was used is billed when it ends. Failed documents cost nothing.
- A key may have 20 jobs queued or running at once.
- Only the key that started a job can see it, fetch its result or delete it.
- Results are kept for 24 hours after the job ends, then deleted. Delete them earlier with
DELETE /v1/jobs/{id}. - Jobs are held by the server that runs them: in the rare case it restarts, jobs in progress are
lost and answer
404. Start them again.