# Bank Statement Parser API Documentation

> Reference for the Statement Parser API: uploads, jobs, results, signed job.completed webhooks, idempotency, job statuses and every error code.

Source: https://parser.lekhavo.com/docs/

[Home](https://parser.lekhavo.com/) / API docs 

# Statement Parser API documentation

The API takes a bank statement or NCD schedule, parses it in the background and gives you the result as a JSON file. A job takes four calls: register the upload, send the file, confirm it, create the job. Then take the webhook or poll, and fetch the result. 

## Authentication

Send your API key as `Authorization: Bearer pk_live_…` (or a `pk_test_` key). Keys are created in the [console](https://parser.lekhavo.com/app/) for a project and carry scopes: `uploads:write`, `jobs:write`, `jobs:read`, `results:read` and `usage:read`. Every response has an `X-Request-ID` header; quote it when you ask for help. The examples use `$PARSER_URL` for the API address and `$PARSER_API_KEY` for the key. 

## Endpoints

| Call                           | Scope         | Success | What it does                                                 |
| ------------------------------ | ------------- | ------- | ------------------------------------------------------------ |
| POST /v1/uploads               | uploads:write | 201     | Register a file: kind, name, content type, size and SHA-256. |
| POST /v1/uploads/{id}/complete | uploads:write | 200     | Check the stored file and mark the upload ready.             |
| POST /v1/jobs                  | jobs:write    | 202     | Create a parsing job for a ready upload.                     |
| GET /v1/jobs/{id}              | jobs:read     | 200     | Job status, detected parser, error code and credits charged. |
| POST /v1/jobs/{id}/cancel      | jobs:write    | 200     | Cancel a job that is still queued.                           |
| GET /v1/jobs/{id}/result       | results:read  | 200     | A short-lived link to the result file.                       |
| GET /v1/billing/balance        | usage:read    | 200     | Credits available, reserved and consumed.                    |
| GET /v1/billing/usage          | usage:read    | 200     | Credit ledger entries, newest first, by cursor.              |

## Uploads

`POST /v1/uploads` takes `document_kind`, `filename`, `content_type`, `size` in bytes and the file's `sha256` as 64 lower-case hex characters. A `bank_statement` may be .pdf, .csv, .xls or .xlsx; `ncd_schedule` is .xlsx. Files may be up to 25 MiB. The response gives an `upload_url` that lasts 15 minutes: PUT the file to it with the same Content-Type. 

`POST /v1/uploads/{id}/complete` then checks the stored file: its size, content type and SHA-256 must match what you declared, and its content must be what the suffix says (a PDF must start with `%PDF-`, a workbook must open as one). A file that fails is rejected and deleted, and the call returns 404 `not_found`. Calling it again on a ready upload returns 200\. 

```
# 1. Register the upload (declare what you are sending)
curl -X POST "$PARSER_URL/v1/uploads" \
  -H "Authorization: Bearer $PARSER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "document_kind": "bank_statement",
    "filename": "statement.pdf",
    "content_type": "application/pdf",
    "size": 482113,
    "sha256": "<hex sha-256 of the file>"
  }'
# 201 -> { "id": "<upload_id>", "upload_url": "<upload URL>", "expires_at": "..." }

# 2. Send the file to the returned upload URL
curl -X PUT "<upload_url>" -H "Content-Type: application/pdf" \
  --data-binary @statement.pdf

# 3. Mark it complete (suffix and MIME are checked again here)
curl -X POST "$PARSER_URL/v1/uploads/<upload_id>/complete" \
  -H "Authorization: Bearer $PARSER_API_KEY"
# 200 -> { "id": "<upload_id>", "status": "ready" }
```

## Jobs

`POST /v1/jobs` needs an `Idempotency-Key` header and a body with the `upload_id`. Create the job within the upload's 15 minutes. Optional fields: `password` for a [protected file](https://parser.lekhavo.com/password-protected-bank-statements/), `requested_parser` to name the bank parser instead of detecting it, and `metadata`, up to 10 string pairs of your own (keys of letters, digits, `_ . -`, values up to 100 characters), returned on the job, the result and the webhook. 

Retrying with the same key and the same body returns the existing job, without a second job or a second charge. The same key with a different body, including a different password, returns 409 `idempotency_conflict`. 

```
# 4. Create the job (Idempotency-Key is required)
curl -X POST "$PARSER_URL/v1/jobs" \
  -H "Authorization: Bearer $PARSER_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{ "upload_id": "<upload_id>" }'
# add "password": "..." for protected statements
# -> { "id": "<job_id>", "status": "queued", "document_kind": "bank_statement", ... }

# 5a. Poll if you would rather not take a webhook
curl "$PARSER_URL/v1/jobs/<job_id>" -H "Authorization: Bearer $PARSER_API_KEY"

# 5b. When the status is "succeeded", fetch the result
curl "$PARSER_URL/v1/jobs/<job_id>/result" -H "Authorization: Bearer $PARSER_API_KEY"
# -> { "download_url": "<short-lived>", "expires_in": ..., "schema_version": "2.0", ... }
```

## Job statuses

A job starts `queued`, moves to `processing` and ends as `succeeded`, `failed_document` (the file could not be parsed; see the error codes below) or `failed_internal`. An internal error or timeout is retried: up to three attempts, 5 and then 10 seconds apart, with the job in `retry_wait` between them; each attempt may run for 240 seconds. Only a queued job can be cancelled, which ends it as `cancelled` and releases its credits. Failed and cancelled jobs are not charged. 

```
{
  "id": "<job_id>",
  "status": "succeeded",
  "document_kind": "bank_statement",
  "requested_parser": null,
  "detected_parser": "AxisBankPdfParser",
  "parser_version": "0.1.0",
  "schema_version": "2.0",
  "error_code": null,
  "credits_charged": 200,
  "created_at": "2025-04-30T10:15:02Z",
  "completed_at": "2025-04-30T10:15:09Z"
}
```

## Results

When the status is `succeeded`, `GET /v1/jobs/{id}/result` returns a `download_url` that expires after 300 seconds, with `document_kind`, `schema_version` and `credits_charged`. The file it points to is JSON: `{"document_kind", "schema_version", "result"}`. For bank statements the result is described field by field in [bank statement to JSON](https://parser.lekhavo.com/bank-statement-to-json/). 

## Webhooks

A webhook endpoint is set up for your project during onboarding; you receive its signing secret once. The service then POSTs `job.completed` (and, if you ask for it, `job.cancelled`) to your HTTPS URL. The body is `{created_at, data, event_id, event_type, version: "webhook.v1"}`; `data` holds `job_id`, `status`, `schema_version`, `error_code` and `credits_charged`. 

Each delivery carries `X-Webhook-Event-ID`, `X-Webhook-Timestamp` (Unix seconds) and `X-Webhook-Signature`: `sha256=` plus the hex HMAC-SHA256 of `timestamp.event_id.raw_body` with your secret. Check the timestamp's age yourself. Any 2xx answer counts as delivered. A 5xx, a timeout (10 seconds) or a connection error is retried up to six attempts in all, waiting 1, 2, 4, 8 and 16 minutes; a 3xx or 4xx answer is not retried, and redirects are not followed. 

```
import { createHmac, timingSafeEqual } from "node:crypto";

// Signature = "sha256=" + HMAC-SHA256(secret, `${timestamp}.${eventId}.${rawBody}`)
export function verify(req, rawBody, secret) {
  const ts = req.headers["x-webhook-timestamp"];
  const id = req.headers["x-webhook-event-id"];
  const got = Buffer.from(req.headers["x-webhook-signature"] ?? "");
  const want = Buffer.from(
    "sha256=" + createHmac("sha256", secret).update(`${ts}.${id}.${rawBody}`).digest("hex"),
  );
  return got.length === want.length && timingSafeEqual(got, want);
}

// event_type: "job.completed"
// body.data: { job_id, status: "succeeded" | "failed_document" | "failed_internal", error_code }
```

## Credits and usage

`GET /v1/billing/balance` returns `mode` (prepaid or postpaid), `entitlement`, `reserved`, `consumed`, `available_credits`and `limit` for your account. `GET /v1/billing/usage` lists ledger entries newest first; pass `limit` (1 to 100, default 50) and the `next_cursor` it returns. Creating a job reserves the 200-credit minimum; see [pricing](https://parser.lekhavo.com/pricing/) for how a parsed document is charged. 

## Error codes

Errors come back as `{"error": {"code", "message"}, "request_id"}`. 

| HTTP | Code                  | Meaning                                                                                                                |
| ---- | --------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| 400  | invalid\_upload       | The upload declaration fails a check: size over the limit, or a kind, suffix and content type that do not go together. |
| 400  | invalid\_pagination   | limit is outside 1 to 100.                                                                                             |
| 401  | invalid\_api\_key     | The key is missing or not valid.                                                                                       |
| 402  | insufficient\_credits | Fewer than 200 credits are available when the job is created.                                                          |
| 403  | forbidden             | The key lacks the scope for this call.                                                                                 |
| 404  | not\_found            | Unknown resource, or one from another project; an upload that is not ready or has expired; any failed /complete check. |
| 409  | idempotency\_conflict | An Idempotency-Key reused with a different body, or a key outside 1 to 200 characters.                                 |
| 409  | job\_not\_cancellable | The job has left the queued state.                                                                                     |
| 409  | result\_not\_ready    | The job has not succeeded.                                                                                             |
| 413  | request\_too\_large   | Body over 1 MiB or headers over 64 KiB.                                                                                |
| 422  | invalid\_request      | The body or a required header does not match the schema.                                                               |
| 422  | invalid\_job          | A password for a format that takes none, or an unknown requested\_parser.                                              |
| 429  | rate\_limited         | Too many requests; wait for the Retry-After seconds.                                                                   |
| 503  | billing\_unavailable  | The billing account cannot be read right now; retry.                                                                   |

A job that fails reports one of these as `error_code`, on the job and in the webhook:

| error\_code                     | Meaning                                                                                    |
| ------------------------------- | ------------------------------------------------------------------------------------------ |
| incorrect\_password             | The PDF or Excel file is encrypted and the password is missing or wrong.                   |
| password\_required              | An encrypted Excel file was sent to a named parser without a password.                     |
| unsupported\_statement          | No bank parser recognised the file.                                                        |
| no\_parseable\_transactions     | A parser matched but found no transactions.                                                |
| invalid\_pdf                    | The PDF is damaged or cannot be read.                                                      |
| invalid\_document               | The workbook or archive is damaged, or an NCD file has no data rows.                       |
| internal\_error, parse\_timeout | The service failed or a parse ran out of time after its retries (status failed\_internal). |

The banks and formats the parser reads are listed on [supported banks](https://parser.lekhavo.com/banks/). Back to the [Statement Parser API](https://parser.lekhavo.com/).
