# bankstatement.extract

Extract and reconcile transactions from text PDFs for Wells Fargo Advantage Checking, Chase checking and Barclays bank accounts.

- REST: `POST https://api.kanagu.dev/v1/bankstatement/extract`
- MCP tool: `bankstatement.extract` at https://api.kanagu.dev/mcp
- Price: $0.12 USD per page, on successful calls only. Trial: 10 calls per IP per UTC day without a key, shared by REST and MCP.
- Auth: `Authorization: Bearer <key>` for billed calls. Retries: reuse `Idempotency-Key`.

## Request body

Exactly one of these shapes:

1. With base64:

- `base64` (string (base64, max 6990508 chars), required): Base64-encoded statement PDF

2. With url:

- `url` (string (uri, max 2048 chars), required): Public URL of a statement PDF

## Response

- `layout` (string | null, required)
- `account` (object | null, required)
- `currency` ("USD" | "GBP" | null, required)
- `period` (object | null, required)
- `balances` (object, required)
- `balances.opening` (string | null, required)
- `balances.closing` (string | null, required)
- `pageCount` (integer (≤ 9007199254740991), required)
- `transactions` (array of object, required)
- `transactions[].date` (string (date), required)
- `transactions[].description` (string, required)
- `transactions[].amount` (string, required)
- `transactions[].page` (integer (≤ 9007199254740991), required)
- `transactions[].balance` (string)
- `reconciliation` (object, required)
- `reconciliation.status` ("reconciled" | "mismatch" | "unverified", required)
- `reconciliation.net` (string, required)
- `reconciliation.calculatedClosing` (string | null, required)
- `reconciliation.difference` (string | null, required)
- `needs_review` (boolean, required)
- `warnings` (array of object, required)
- `warnings[].code` (string, required)
- `warnings[].page` (integer (≤ 9007199254740991))

## Example

```sh
curl https://api.kanagu.dev/v1/bankstatement/extract \
  -H "Authorization: Bearer $KANAGU_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url":"https://www.wellsfargo.com/assets/pdf/personal/help/tester.pdf"}'
```

```ts
const res = await fetch("https://api.kanagu.dev/v1/bankstatement/extract", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.KANAGU_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "url": "https://www.wellsfargo.com/assets/pdf/personal/help/tester.pdf"
  }),
});
if (!res.ok) throw new Error((await res.json()).error.message);
const result = await res.json();
```

## Errors

JSON `{"error":{"code","message"}}`.

- 400 `invalid_input`: The body does not match the request schema. The message names each field.
- 400 `invalid_json`: The body is missing or is not valid JSON.
- 400 `invalid_idempotency_key`: Idempotency-Key must be 1–128 visible ASCII characters.
- 401 `invalid_api_key`: The Bearer key is missing, malformed, invalid or inactive.
- 402 `free_allowance_exhausted`: This month's free usage is used up and the account has no subscription. Subscribe from the dashboard, or wait for the next UTC month; Retry-After gives the seconds left.
- 404 `not_found`: No operation at this path.
- 409 `idempotency_conflict`: The Idempotency-Key was already used with a different body.
- 413 `payload_too_large`: JSON bodies may be at most 64 KiB.
- 429 `trial_limit`: The anonymous trial allows 10 calls per IP per UTC day, shared by REST and MCP. Retry-After gives the seconds left.
- 500 `internal_error`: The call failed on our side. It is not billed; retry with the same Idempotency-Key.

## JSON Schema

Request:
```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "base64": {
          "type": "string",
          "minLength": 4,
          "maxLength": 6990508,
          "format": "base64",
          "contentEncoding": "base64",
          "pattern": "^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$",
          "description": "Base64-encoded statement PDF"
        }
      },
      "required": [
        "base64"
      ],
      "additionalProperties": false
    },
    {
      "type": "object",
      "properties": {
        "url": {
          "type": "string",
          "maxLength": 2048,
          "format": "uri",
          "description": "Public URL of a statement PDF"
        }
      },
      "required": [
        "url"
      ],
      "additionalProperties": false
    }
  ],
  "examples": [
    {
      "url": "https://www.wellsfargo.com/assets/pdf/personal/help/tester.pdf"
    }
  ],
  "type": "object"
}
```

Response:
```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "layout": {
      "type": [
        "string",
        "null"
      ]
    },
    "account": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "last4": {
              "type": "string"
            }
          },
          "required": [
            "last4"
          ],
          "additionalProperties": false
        },
        {
          "type": "null"
        }
      ]
    },
    "currency": {
      "anyOf": [
        {
          "type": "string",
          "enum": [
            "USD",
            "GBP"
          ]
        },
        {
          "type": "null"
        }
      ]
    },
    "period": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "start": {
              "type": "string",
              "format": "date",
              "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))$"
            },
            "end": {
              "type": "string",
              "format": "date",
              "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))$"
            }
          },
          "required": [
            "start",
            "end"
          ],
          "additionalProperties": false
        },
        {
          "type": "null"
        }
      ]
    },
    "balances": {
      "type": "object",
      "properties": {
        "opening": {
          "type": [
            "string",
            "null"
          ]
        },
        "closing": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "required": [
        "opening",
        "closing"
      ],
      "additionalProperties": false
    },
    "pageCount": {
      "type": "integer",
      "exclusiveMinimum": 0,
      "maximum": 9007199254740991
    },
    "transactions": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "date": {
            "type": "string",
            "format": "date",
            "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))$"
          },
          "description": {
            "type": "string"
          },
          "amount": {
            "type": "string"
          },
          "page": {
            "type": "integer",
            "exclusiveMinimum": 0,
            "maximum": 9007199254740991
          },
          "balance": {
            "type": "string"
          }
        },
        "required": [
          "date",
          "description",
          "amount",
          "page"
        ],
        "additionalProperties": false
      }
    },
    "reconciliation": {
      "type": "object",
      "properties": {
        "status": {
          "type": "string",
          "enum": [
            "reconciled",
            "mismatch",
            "unverified"
          ]
        },
        "net": {
          "type": "string"
        },
        "calculatedClosing": {
          "type": [
            "string",
            "null"
          ]
        },
        "difference": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "required": [
        "status",
        "net",
        "calculatedClosing",
        "difference"
      ],
      "additionalProperties": false
    },
    "needs_review": {
      "type": "boolean"
    },
    "warnings": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "code": {
            "type": "string"
          },
          "page": {
            "type": "integer",
            "exclusiveMinimum": 0,
            "maximum": 9007199254740991
          }
        },
        "required": [
          "code"
        ],
        "additionalProperties": false
      }
    }
  },
  "required": [
    "layout",
    "account",
    "currency",
    "period",
    "balances",
    "pageCount",
    "transactions",
    "reconciliation",
    "needs_review",
    "warnings"
  ],
  "additionalProperties": false
}
```
