# PDFTrusted API guide for coding agents

Canonical URL: `https://developers.pdftrusted.com/agent-guide.md`

Use this guide to add PDF work (compress, convert, OCR, merge, split, redact and more) to a project through the
PDFTrusted REST API. The [API reference](https://developers.pdftrusted.com/docs/reference) and the
[OpenAPI file](https://api.pdftrusted.com/openapi.json) are authoritative for parameters; do not guess tool or option names.

## Start without an account

- Base URL: `https://api.pdftrusted.com`. JSON in, JSON out.
- The API answers **without a key** as a guest, so work can start with no sign-up. Guest allowance when this guide
  was checked: 3 jobs a day, files up to 25 MB and 50 pages, 1 job at a time. Only successful jobs count.
- Do not rely on those numbers: the answers of `POST /v1/tasks` and `/process` carry the current
  `usage.caps` and `usage.remaining` (`/process` shows the count after the job). Read them. In `caps`:
  `jobs` per `window` (`day` or `month`), `concurrent` jobs at a time, `max_mb` and `max_pages` per file,
  `max_files` per task, `rate_per_min` (0 means no per-minute limit).
- For more, the user creates a key at <https://developers.pdftrusted.com/console/keys>:
  - free trial key (`sk_trial_...`): 50 jobs a month, 25 MB and 50 pages a file;
  - sandbox key (`sk_test_...`): 100 jobs a day, 5 MB and 10 pages a file, every tool except the AI ones.
- A key is read from the environment variable `PDFTRUSTED_API_KEY` and sent as the header `X-API-Key`.
  Never ask the user to paste a key into a prompt. Never print, log or commit it.

## One job is four calls, in this order

```bash
API=https://api.pdftrusted.com

# 1. Create the task. The tool AND its options are set here. The Idempotency-Key is any unique string.
curl -s -X POST $API/v1/tasks -H 'content-type: application/json' -H "Idempotency-Key: $(date +%s)-$RANDOM" \
  -d '{"tool":"compress","options":{"level":"recommended"}}'
# -> 201 {"task_id":"...","status":"created","usage":{"plan":"guest","caps":{...},"remaining":3}}

# 2. Upload the file: PUT, multipart, field "file".
curl -s -X PUT $API/v1/tasks/$ID/upload -F 'file=@input.pdf;type=application/pdf'
# -> 200 {"status":"uploaded"}

# 3. Process. No body.
curl -s -X POST $API/v1/tasks/$ID/process
# -> 200 {"status":"done","result_name":"input.pdf","usage":{...}}

# 4. Ask for the download link, then fetch it.
DOWNLOAD_URL=$(curl -s $API/v1/tasks/$ID/download | python3 -c "import sys,json; print(json.load(sys.stdin)['download_url'])")
# the answer is 200 {"download_url":"https://...signed link on a storage host..."}  (jq -r .download_url works too)
curl -s "$DOWNLOAD_URL" -o output.pdf
```

The answers above are shortened; the real ones have more fields. `signed_upload_url` and the other
`signed_upload...` fields of the create answer are `null` unless a file size was declared: ignore them and
upload with step 2.

With a key, add `-H "X-API-Key: $PDFTRUSTED_API_KEY"` to calls 1 to 4.

Four things that go wrong most often:

1. **Options at create, not at process.** Options sent to `/process` are refused: `400 bad_request`
   ("options are set when the task is created").
2. **Upload is `PUT`.** `POST` on `/upload` answers `405 method_not_allowed`.
3. **The download link is on another host.** Fetch `download_url` with a plain GET and **without** the
   `X-API-Key` header: the link is already signed, and a key must never be sent to another host. The link is
   valid for about one hour; ask step 4 again for a fresh one. (The `/process` answer also has a field
   `download_url`: that one is only the API path of step 4, not the file.)
4. **One `/process` per task.** A second one answers `409`. Do not retry `/process` blindly; ask the task for
   its status instead.

Tools that take several files (merge, images to PDF): send the field `files` once per file, or one `PUT` per file.

## Long jobs

- `POST /v1/tasks/{id}/process?async=1` answers `202` at once with `"status":"processing"` and a
  `Retry-After` header.
- Then `GET /v1/tasks/{id}?wait=20` holds the answer for up to 20 seconds; repeat until `status` is `done`,
  `failed` or `expired`.
- A failed task has `error.code`; use the table below.

## Find the right tool and its options

- `GET /v1/tools`: every tool name. `GET /v1/tools/{tool}`: its file limits per plan.
- [tool-catalog.json](https://api.pdftrusted.com/tool-catalog.json): options and credits of every tool.
- Example, `compress`: `level` is `low`, `recommended` (default) or `extreme`; `target_mb` asks for a size.
- [openapi.json](https://api.pdftrusted.com/openapi.json): a ready request example per tool under `POST /v1/tasks`.

## Errors

Every error is `{"error":{"code","message","request_id"}}` with a stable `code`. Quote `request_id` to
support. A `429` or `503` has a `Retry-After` header in seconds: wait that long, never shorter.

| Status | `code` | What to do |
| --- | --- | --- |
| 400 | `unknown_tool` | Take the name from `GET /v1/tools`. |
| 400 | `bad_request` | Read `message` and `fields`; fix the request. |
| 401 | `invalid_api_key` | The key is wrong or switched off. Tell the user; do not try other keys. |
| 403 | `test_key_tool` | A sandbox key cannot run AI tools. Use a trial or live key. |
| 404 | `not_found` | The task id is wrong, or the task was deleted or has expired. |
| 405 | `method_not_allowed` | Wrong HTTP method (upload is `PUT`). |
| 409 | `bad_state` | Calls out of order: upload before process, one process per task. |
| 409 | `task_busy` | The task is already starting. Poll its status; do not start it again. |
| 413 | `file_too_large`, `too_many_pages` | Over the plan's file limit (`usage.caps`). Tell the user. |
| 429 | `quota_exceeded`, `ip_quota_exceeded` | The allowance is used up. Tell the user when it resets (header `X-RateLimit-Reset`, Unix time in seconds, UTC); do not loop. |
| 429 | `rate_limited`, `too_many_concurrent` | Wait `Retry-After` seconds, then retry once. |
| task failed | `pdf_password` | The file needs a password: ask the user, then create the task again with the option `password`. |
| task failed | `scanned_pdf` | The pages are scanned images: run the tool `ocr` on the file first. |

## Care with files and retries

- Send an `Idempotency-Key` header (any unique string) with `POST /v1/tasks`: a repeated create then answers
  the first task again instead of making a second one.
- Files are deleted automatically 1 hour after a job finishes. `DELETE /v1/tasks/{id}` deletes input and
  output at once (`204`).
- Check the result before reporting success: the download answered `200`, the file is not empty, and a PDF
  starts with `%PDF-`.

## Other ways in

- The open-source [agent plugin](https://github.com/pdftrusted/pdftrusted-agent-plugin) (MIT): a local MCP server
  for any MCP client, Agent Skills and a small Python client. It works without a key on the guest allowance.
- Human-readable docs: <https://developers.pdftrusted.com/docs>. Plans and credits: <https://developers.pdftrusted.com/pricing>.
