# API overview

Everything the dashboard does is available over a JSON API, for scripts, CI tooling and fleets of coding agents. This page covers the conventions; the [API reference](/docs/api/reference) lists every operation, and the [OpenAPI 3.1 description](/api/v1/openapi.json) is machine-readable.

## Base URL

```text
https://kajiya.app/api/v1
```

Requests and responses are JSON (`Content-Type: application/json` on requests with a body). A few list endpoints can also return CSV.

## Authentication

Send an API key as a bearer token with every request:

```bash
curl -s "https://kajiya.app/api/v1/account" -H "Authorization: Bearer $KAJIYA_API_KEY"
```

Create keys in the dashboard under **API keys**, or with `POST /api/v1/keys` and an existing `admin` key. A key starts with `kjk_` and is shown once, when it is created; Kajiya only stores a hash of it. The dashboard's own session token also works as a bearer token, with `admin` scope. `DELETE /api/v1/sessions` signs out every browser session of the account at once. Accounts that were given an account token (`kjt_...`) can replace it with `POST /api/v1/account/token` or revoke it with `DELETE /api/v1/account/token`, called with a session or an `admin` key.

| Key field | Notes |
|---|---|
| `name` | Required, up to 80 characters. Name the agent, script or machine that uses it. |
| `scope` | `read`, `write` or `admin` (below). |
| `expires_in_days` | Optional, 1 to 3650. Without it the key does not expire. |
| `prefix` | The first 12 characters, shown in lists so you can tell keys apart. |
| `last_used_at` | Updated at most once a minute. |

An account can have up to 100 active keys. Revoke a key in the dashboard or with `DELETE /api/v1/keys/{id}`; it stops working immediately. A key cannot revoke itself.

## Scopes

Each scope includes the ones before it.

| Scope | Allows |
|---|---|
| `read` | Account, queue, jobs, usage, repositories, settings, webhook deliveries, payments |
| `write` | Also: connect, disconnect, pause, cap and sync repositories; cancel jobs |
| `admin` | Also: create, list and revoke API keys; change spend caps, the low-balance alert and the webhook URL; rotate the webhook secret; send test events |

Give each agent or machine its own key with the smallest scope it needs. A key with too small a scope gets `403` with a message naming the scope it needs.

## Rate limit

Each account may make 1,200 requests per minute, shared by all of its keys and the dashboard. The window is the calendar minute. Over the limit, Kajiya answers `429` with a `Retry-After` header: the number of seconds until the next minute starts.

```bash
status=$(curl -s -o body.json -w '%{http_code}' -D headers.txt "https://kajiya.app/api/v1/queue" -H "Authorization: Bearer $KAJIYA_API_KEY")
if [ "$status" = 429 ]; then sleep "$(grep -i '^retry-after:' headers.txt | tr -dc '0-9')"; fi
```

## Errors

Errors are JSON with one human-readable `error` string, and the status code tells you the kind:

```json
{ "error": "This API key has the \"read\" scope; this action needs \"write\"." }
```

| Status | Meaning |
|---|---|
| `400` | A parameter or the body is invalid. The message names the field. |
| `401` | Missing, unknown, revoked or expired key. |
| `403` | The key's scope is too small. |
| `404` | No such object on your account (or no such endpoint). |
| `409` | The object is in a state that does not allow this, for example cancelling a job that already finished. |
| `429` | Rate limit; wait `Retry-After` seconds. |
| `502` | GitHub could not be reached or refused the call Kajiya made for you. |
| `500` | Something went wrong on Kajiya's side. Retrying later is safe for reads. |

## Data formats

- **Times** are ISO 8601 in UTC, for example `2026-12-04T09:12:09.000Z`. Query parameters such as `since` and `until` accept `YYYY-MM-DD` (UTC midnight) or a full ISO timestamp.
- **Money** is US dollars as a JSON number, exact to the micro-dollar (`0.0021`), never cents.
- **Ids** are integers. `github_run_id` and `github_job_id` are GitHub's ids, and `github_url` links to the job on GitHub.
- New fields may be added to responses. Ignore fields you do not know.

## Pagination

`GET /api/v1/jobs` returns the newest jobs first, `limit` at a time (default 50, at most 500), and a `next_before` cursor. Pass it as `before` to get the next, older page; it is `null` on the last page.

```bash
before=""
while :; do
  page=$(curl -s "https://kajiya.app/api/v1/jobs?limit=500&since=2026-12-01${before:+&before=$before}" \
    -H "Authorization: Bearer $KAJIYA_API_KEY")
  echo "$page" | jq -c '.jobs[]'
  before=$(echo "$page" | jq -r '.next_before // empty')
  [ -z "$before" ] && break
done
```

## CSV exports

Add `format=csv` to `GET /api/v1/jobs` (up to 10,000 rows per request) or `GET /api/v1/usage` to get a CSV file. Cells that a spreadsheet would treat as a formula are prefixed with `'`.

```bash
curl -s "https://kajiya.app/api/v1/usage?group_by=repo&since=2026-12-01&format=csv" \
  -H "Authorization: Bearer $KAJIYA_API_KEY" -o kajiya-usage.csv
```

## Retrying safely

- Every `GET` is safe to repeat.
- `POST /api/v1/repos` is safe to repeat: a repository that is already connected comes back as `updated`.
- `POST /api/v1/jobs/{id}/cancel` answers `409` once the job is finished, so a repeated cancel does no harm.
- Changing settings with `PATCH` sets values; repeating it gives the same result.

## A minimal TypeScript client

```ts
const BASE = "https://kajiya.app/api/v1";

export async function kajiya<T>(path: string, init: RequestInit = {}): Promise<T> {
  for (;;) {
    const res = await fetch(BASE + path, {
      ...init,
      headers: {
        authorization: `Bearer ${process.env.KAJIYA_API_KEY}`,
        ...(init.body ? { "content-type": "application/json" } : {}),
        ...init.headers,
      },
    });
    if (res.status === 429) {
      await new Promise((r) => setTimeout(r, Number(res.headers.get("retry-after") ?? 5) * 1000));
      continue;
    }
    const body = await res.json();
    if (!res.ok) throw new Error(`Kajiya ${res.status}: ${body.error}`);
    return body as T;
  }
}

const account = await kajiya<{ balance_usd: number; concurrency: { limit_vcpu: number } }>("/account");
console.log(account.balance_usd, account.concurrency.limit_vcpu);
```

CSV responses are text, so read them with `res.text()` instead of this helper.
