# Kody Open API

The Kody Open API and MCP `api` tool are available to every signed-in account.
Call Kody over HTTPS at [api.kody.codes](https://api.kody.codes)
(`/openapi.json` and `/v1/*`), run one operation from an agent with MCP `api`,
or use `kody api` from a terminal. Interactive docs:
[api-docs.kody.codes](https://api-docs.kody.codes).

This page covers the three ways to call it, minting scoped API tokens, editing a
Files page with `pagePatch`, and metering.

## What it is

- **Kody Open API** — JSON HTTP at [api.kody.codes](https://api.kody.codes)
  (`/openapi.json` and `/v1/*`).
- **MCP `api` tool** — run one Open API operation (`operationId` + `params`)
  from a connected agent. Use it to mint, list, and revoke scoped API tokens
  (`tokenCreate` / `tokenList` / `tokenRevoke`). There is no account UI for
  tokens.
- **CLI `kody api`** — the same call from a terminal:
  `kody api <operationId> --params '<json>'`. For large params, read them from a
  file (`kody api pagePatch --params-file patch.json`) or pipe them in
  (`cat patch.json | kody api pagePatch --params -`).

For CLI `--local` setup and usage (CapabilityProxy / package-graph), see
[Local CLI execute](/docs/local-execute) (`guide:local_execute`). That path is
separate from Open API; some `/v1` routes exist so the CLI can hop to origin.

## Mint a scoped API token

With MCP `api` available, mint a token (value returned once). Lifetime is
required: `short` (1h idle / 24h max), `long` (14d idle / 3mo max), or both
`idle_ttl_seconds` and `max_lifetime_seconds`. Tokens default to
`org_binding: "org"` (fixed to the current request org). Pass
`"org_binding": "user"` for a person-owned token that selects an org per request
with `?org=<slug>` (see [Git credential helper](/docs/auth-setup-git)).

```json
{
	"operationId": "tokenCreate",
	"params": {
		"name": "ci-bot",
		"scopes": ["org:read", "search:read"],
		"lifetime": "short"
	}
}
```

Include `"org:execute"` in `scopes` only when a headless CLI needs
CapabilityProxy / package-graph over a `kody_at_…` token (see
[Local CLI execute](/docs/local-execute)). Put the value in the environment
(`KODY_API_TOKEN`); never paste it into chat.

List and revoke with `tokenList` / `tokenRevoke`, or the same operations over
HTTP. Step by step, plus the CLI fallback: [API tokens](/docs/api-tokens)
(`guide:api_tokens`).

## Edit a Files page (`pagePatch`)

`pagePatch` (`POST /v1/files/{file_id}/blocks`) edits a Files page
(`application/vnd.kody.page+json`) by block ID. Send one batch of 1 to 100
operations; the batch applies whole or not at all.

1. Call `fileGet` for the page. Its `content` is page JSON:
   `{"version":1,"blocks":[{"id":"…","kind":"paragraph","text":"…"}]}`.
2. Send `headVersion` (or `revisionId` when `headVersion` is absent) as
   `base_revision_id`, and a new UUID as `write_id`.

```json
{
	"operationId": "pagePatch",
	"params": {
		"file_id": "<page id>",
		"write_id": "<new uuid>",
		"base_revision_id": "<fileGet headVersion or revisionId>",
		"operations": [
			{
				"kind": "insert",
				"after_id": null,
				"block": {
					"id": "<new uuid>",
					"kind": "paragraph",
					"text": "Opening line"
				}
			}
		]
	}
}
```

Each operation has a `kind` and accepts only the fields for that kind, as do
blocks in the batch. `after_id` names a block present at that step in the batch,
other than the block being moved; `null` places the block first. A `replace`
supplies the whole replacement block with the ID of the block it replaces.

| Operation `kind` | Fields                                       |
| ---------------- | -------------------------------------------- |
| `insert`         | `after_id`, `block` (a new block with an id) |
| `replace`        | `block` (same `id` as the block to replace)  |
| `move`           | `block_id`, `after_id`                       |
| `remove`         | `block_id`                                   |

Every block has an `id` UUID and a `kind`:

| Block `kind`                                                                                | Fields                                                     |
| ------------------------------------------------------------------------------------------- | ---------------------------------------------------------- |
| `paragraph`, `heading`, `bullet`, `numbered`, `task`, `quote`, `callout`, `code`, `mermaid` | `text`, optional boolean `checked` (used by tasks)         |
| `table`                                                                                     | `rows`: one or more nonempty string arrays of equal length |
| `file`                                                                                      | `fileId`: an attached file ID                              |
| `collapse`, `link`                                                                          | `title`, `text`                                            |
| `contents`                                                                                  | none                                                       |

If `kind` is missing or invalid, validation lists the supported kinds and their
fields. For an unexpected field, validation lists the fields for that kind and
suggests a correction when it recognizes the name.

### Retry and rebase

A response with `ok: false` means nothing was written. It carries
`current_revision_id` and `current` (the current page JSON with every block ID
and kind):

- `code: "revision_conflict"` — the page changed after your base. Rebuild the
  operations from `current`.
- `code: "invalid_patch"` — the batch does not fit the page. Read `message` for
  the reason. `operation_index` is zero-based for an error such as an unknown
  block ID, or `null` for a batch-level error such as a final page over 250
  blocks.

Send the rebuilt batch with `current_revision_id` as `base_revision_id` and a
new `write_id`. To resend an identical request after a timeout, reuse its
`write_id`, `base_revision_id`, and operations; Kody returns the saved result
instead of writing twice.

## Metering

Each Open API operation (HTTPS, MCP `api`, CapabilityProxy hop, or package-graph
prep) meters as an observe-only `api_call`. Capability work behind the operation
still meters as usual.

## Where to go next

- [api-docs.kody.codes](https://api-docs.kody.codes) — interactive OpenAPI
- [API versioning and deprecation](/docs/api-versioning) — `/v1` policy, windows,
  `Retry-After` on `429`
- [Local CLI execute](/docs/local-execute) — `@kodycodes/cli execute --local`
- [Runtime and efficiency](/docs/platform-efficiency) — how cloud execute meters
  worker days
- Contributor detail:
  [Open API architecture](https://raw.githubusercontent.com/kody-hq/kody/main/docs/contributing/architecture/open-api.md)
