# Cashplannr API v1

The Cashplannr API lets external systems (ERP, accounting, CRM, your own
data pipeline) read the figures of a Cashplannr account, push data into it
and change what they manage. AI assistants use the same API through the
Cashplannr MCP server (see [MCP server](#mcp-server) below).

- Base URL: `https://api.cashplannr.com/public/v1`
- Contract: [`openapi.yaml`](openapi.yaml) (OpenAPI 3.1). It is exported from
  the Cashplannr backend, so it always matches the API. Human-readable:
  [Redoc](https://cashplannr.com/api/redoc), try it out:
  [Swagger UI](https://cashplannr.com/api/docs).
- Recipes: [`examples.md`](examples.md).

## Getting credentials

An integration authenticates with OAuth2 `client_credentials`. Cashplannr
creates the client for you: tell us which companies (legal entities) and
which scopes your integration needs. You receive:

- `client_id`
- `client_secret`, shown once: store it in your secret manager.

The scopes and the companies a client may see are fixed on the client.
Asking for other scopes when you request a token changes nothing.

An AI assistant needs no credentials: it connects through the MCP server
on behalf of a user (see below).

## Authentication

```bash
curl -X POST https://api.cashplannr.com/oauth/token \
  -d grant_type=client_credentials \
  -d client_id=$CLIENT_ID \
  -d client_secret=$CLIENT_SECRET
```

```json
{ "token_type": "Bearer", "expires_in": 3600, "access_token": "eyJ0eXAiOiJKV1Qi…" }
```

The token is valid for one hour. Request a new one with the same
credentials when it expires. Send it on every call:

```
Authorization: Bearer eyJ0eXAi…
```

Start with `GET /public/v1/` (discovery): it tells you the account, the
token's scopes and companies, the active modules, the limits and every
endpoint this token may call (`access`: `direct`, `proposal` or `none`).

### Two kinds of tokens

| | Integration (`client_credentials`) | AI assistant (authorization code + PKCE) |
|---|---|---|
| Acts as | a service user of the account | the signed-in user |
| Reads | what its scopes and companies allow | everything that user may see |
| Writes | directly, with `<resource>:write` | never directly: every write becomes a **proposal** |
| `actual` / `outstanding` transactions | may create them | may not (`type_not_allowed`) |
| `/batch` routes | yes | no (`machine_only`) |
| `Idempotency-Key` | honoured on `POST` | ignored |

## Scopes

| Scope | Gives |
|---|---|
| `account:read` | Every `GET` endpoint: everything the account may see. |
| `transactions:write` | Create and change transactions and recurring series, batches, bulk update, match. |
| `companies:write` | Create, change and merge parties (customers, suppliers). |
| `categories:write` | Create and change categories. |
| `payment_terms:write` | Create and change payment terms. |
| `projects:write` | Push the units of a project from your source and give a project its code. |
| `tenants:write` | Start a sync of a company. |
| `connections:write` | Start a sync of a connection. |
| `balances:write` | Push bank balance snapshots. |
| `gl_codes:write` | Push general ledger accounts. |

`tenants:read` is still accepted on `GET /tenants`, but use `account:read`.
The `*:propose` scopes in the spec belong to AI assistants; an integration
does not need them.

## Endpoints

All paths below are relative to `/public/v1`. Each list also has a detail
route `GET /<resource>/{id}`. The reference has the fields per endpoint.

| Area | Endpoints |
|---|---|
| Discovery | `GET /`, `GET /ping` |
| Companies (legal entities) | `GET /tenants`, `POST /tenants/{id}/sync` |
| Transactions | `GET /transactions`, `POST /transactions`, `PATCH /transactions/{id}`, `PUT /transactions/external/{source}/{external_id}`, `POST /transactions/batch`, `POST /transactions/bulk_update`, `POST /transactions/{id}/match` |
| Recurring series | `GET /recurrences`, `POST /recurrences`, `PATCH /recurrences/{id}`, `PUT /recurrences/external/{source}/{external_id}` |
| Parties | `GET /companies`, `POST /companies`, `PATCH /companies/{id}`, `PUT /companies/external/{source}/{external_id}`, `POST /companies/batch`, `POST /companies/{id}/merge` |
| Categories | `GET /categories`, `POST /categories`, `PATCH /categories/{id}`, `PUT /categories/external/{source}/{external_id}`, `POST /categories/batch` |
| Payment terms | `GET /payment_terms`, `POST /payment_terms`, `PATCH /payment_terms/{id}`, `PUT /payment_terms/external/{source}/{external_id}` |
| Cash | `GET /cash_position`, `GET /cashflow`, `GET /outstanding_invoices` |
| Scenarios | `GET /scenarios`, `GET /scenarios/{id}/compare` |
| Budgets (budget module) | `GET /budgets`, `GET /budgets/{id}/sources` |
| Projects (project module) | `GET /projects`, `GET /projects/{id}/units`, `GET /projects/{id}/unit_types`, `POST /projects/{id}/units`, `POST /projects/{id}/units/batch`, `PUT /projects/{id}/external_code` |
| Balances and GL accounts | `POST /balances`, `POST /balances/batch`, `POST /gl_codes`, `POST /gl_codes/batch` |
| Connections and jobs | `GET /connections`, `POST /connections/{id}/sync`, `GET /jobs/{id}` |
| Proposals | `GET /proposals`, `POST /proposals/{id}/withdraw` |

Not available yet: `POST /tenants`, `POST /tenants/batch`,
`POST /bank_accounts` and `POST /bank_accounts/batch` answer `501
not_implemented`.

## Conventions

- **Amounts** are integers in euro cents, in fields ending in `_cents`. An
  amount is always positive; `direction` (`in` or `out`) sets the sign.
- **Dates** are ISO `YYYY-MM-DD`; timestamps ISO 8601.
- **Companies.** A token sees one account. Limit a request to certain
  companies with `tenant_ids[]`. Every response carries `meta.tenant_ids`.
- **Modules.** Budgets need the budget module and projects the project
  module; without it you get `403 feature_locked`.
- **Transaction types.** `forecast`, `commitment`, `suggestion`, `ignored`
  and `vat` can be created with `transactions:write`; `actual` (bank lines)
  and `outstanding` (open invoices) only by an integration. On a bank line or
  invoice that did not come from your integration, only `description`,
  `category_id` and `company_id` can change (`not_writable`).
- **No deletes.** Archive a record with `PATCH {"is_active": false}`. Lists
  hide archived records unless `include_archived=true`.

## Lists

Lists return `{"data": [...], "meta": {"page", "per_page", "total",
"has_more", "tenant_ids"}}`. Paginate with `page` (at most 10 000) and
`per_page` (default 50, at most 200). Search with `q` where the list
supports it. Query parameters are strict: an unknown key is refused with
`422 unknown_query_parameter`, with the valid keys in `details.valid`. A
`GET` with a body is refused with `400 body_not_allowed`.

## Writing

- **Create**: `POST /<resource>` answers `201` with the record.
- **Change**: `PATCH /<resource>/{id}` with only the fields that change.
- **Upsert on your own code**: `PUT /<resource>/external/{source}/{external_id}`
  creates the record the first time (`201`) and updates it afterwards
  (`200`, `meta.outcome` `updated` or `unchanged`). Codes are unique per
  company and case-sensitive. A code already held by another record is
  `409 external_key_taken`. A plain `POST` does not take `source` or
  `external_id` (`422 unknown_field`).
- **Unknown fields** in a body are refused: `422 unknown_field`.

## Idempotency

Send an `Idempotency-Key` header (1 to 128 characters, for example a UUID)
on a `POST`. The first successful (2xx) answer is kept for 48 hours per
client; a repeat with the same key returns that answer unchanged, with the
header `Idempotent-Replayed: true`, and runs nothing again. The body of the
repeat is not compared, so use a new key for a new request. A second call
with the same key while the first is still running gets `409 locked`. Only
integrations use this header; batches ignore it (use stable codes per item).

## Batches

- Body: `{"items": [...]}` or a bare JSON array.
- Up to **500** items for transactions, parties, categories and project
  units; up to **10 000** for balances and GL accounts. More is
  `422 batch_too_large`.
- Each item of `/transactions/batch`, `/companies/batch` and
  `/categories/batch` is the body of the `PUT .../external/...` upsert with
  its own `source` and `external_id`.
- Every item is handled on its own: `200` when all succeeded, `207` when one
  or more failed. Each result has `index`, `outcome` (`created`, `updated`,
  `unchanged` or `error`), `id`, and on an error `http_status` and `error`.
  The same code twice in one batch: the later item gets
  `duplicate_in_batch`.
- Batches are for integrations only (`403 machine_only` for an AI token).

## Asynchronous jobs

`POST /tenants/{id}/sync` and `POST /connections/{id}/sync` answer `202`
with a job and its `status_url`. Poll `GET /jobs/{id}` until `state` is
`succeeded` or `failed`. A sync that is already running is
`409 sync_already_running`.

## Proposals

When an AI assistant writes, nothing changes. The route validates the call
as usual and answers `202` with a proposal: `preview` (what would change),
`confirm_url` and `expires_at`. The user confirms or declines it, in the AI
app or in Cashplannr, within **10 minutes**. A proposal reads as
`pending`, `expired`, `confirmed`, `failed`, `cancelled`, `withdrawn` or
`superseded`. List them with `GET /proposals`, withdraw one with
`POST /proposals/{id}/withdraw` (`409 proposal_closed` once it is closed).
A confirmation can never come from the same token.

## Rate limits

| Limit | Value |
|---|---|
| Per client (or per user for an AI token) | 600 requests per minute |
| Heavy routes (cash position, cash flow, scenario compare, budget sources, batches, bulk update, recurring series writes, syncs) | 60 requests per minute, on top |
| Per IP address, before authentication | 1 200 requests per minute |

Over the limit you get `429 rate_limited` with `Retry-After` (and
`X-RateLimit-Limit`, `X-RateLimit-Remaining`). Discovery returns your
limits in `data.limits`.

## Errors

Every error has the same shape:

```json
{
  "error": {
    "code": "scope_missing",
    "meaning": "What went wrong, in one sentence.",
    "next_step": "What your client should do next.",
    "details": { "required_scope": "transactions:write", "granted_scopes": ["account:read"] }
  }
}
```

| HTTP | `code` | When |
|---|---|---|
| 400 | `bad_request`, `body_not_allowed` | Malformed request; a `GET` with a body. |
| 401 | `token_invalid` | Token missing, malformed, expired or revoked. |
| 403 | `scope_missing` | The token lacks the scope (`details.required_scope`). |
| 403 | `forbidden` | Not allowed for this account, company or role. |
| 403 | `feature_locked` | The module is not part of the account's plan. |
| 403 | `machine_only` | A batch route called with an AI token. |
| 404 | `not_found` | Unknown id, or an id outside what the token may see. |
| 405 | `method_not_allowed` | Wrong HTTP method; see the `Allow` header. |
| 409 | `external_key_taken` | The code belongs to another record. |
| 409 | `locked` | Same `Idempotency-Key` while the first call still runs. |
| 409 | `sync_already_running` | A sync of this company or connection is running. |
| 409 | `proposal_closed` | The proposal was already confirmed, declined, withdrawn or expired. |
| 422 | `validation_failed` | Invalid body; `details.errors` per field. |
| 422 | `unknown_field`, `unknown_query_parameter` | A key the endpoint does not know. |
| 422 | `type_not_allowed` | This transaction type cannot be created by this token. |
| 422 | `not_writable` | The field belongs to the integration that brought the row in. |
| 422 | `not_matchable` | These two transactions cannot be linked. |
| 422 | `too_many_rows` | A bulk update selects more than 500 rows. |
| 422 | `invalid_batch`, `empty_batch`, `batch_too_large`, `duplicate_in_batch` | Batch envelope problems. |
| 429 | `rate_limited` | Too many requests; see `Retry-After`. |
| 501 | `not_implemented` | Reserved endpoint, not available yet. |
| 500 | `server_error` | Unexpected error; retry later or contact support. |

The reference lists the codes per endpoint. Every code comes with a
`meaning` and a `next_step`, so a client can act on a code it does not know.

## Request id and audit

Send an `X-Request-Id` header with a UUID; it is echoed in the response
(we create one when you do not send it). Cashplannr keeps a short record of
every call for 90 days: request id, client, user, method, path, status,
error code and duration. Never the body or the query string. Quote the
request id when you contact support.

## Versioning

The version is in the path (`/public/v1`). New endpoints and new fields are
added within v1, so ignore response keys you do not know. The OpenAPI file
is the contract.

## MCP server

The Cashplannr MCP server gives AI assistants such as Claude and ChatGPT
the same reads, with every write as a proposal the user confirms.

- URL: `https://api.cashplannr.com/api/mcp` (Streamable HTTP, `POST`).
- Auth: OAuth 2.1 with discovery
  (`/.well-known/oauth-protected-resource`,
  `/.well-known/oauth-authorization-server`), dynamic client registration
  (`POST /oauth/register`), authorization code with PKCE (S256), scope
  `mcp:use`. Access token 1 hour, refresh token 30 days. Sign-in redirects
  are accepted for Claude and ChatGPT.
- Limit: 120 requests per minute per user, on top of the API limits.

The tool list and how to connect are on
[cashplannr.com/en/developers](https://cashplannr.com/en/developers#mcp)
and [cashplannr.com/en/connect-ai](https://cashplannr.com/en/connect-ai).

## Support

support@cashplannr.com
