# REST API

Every resource gets REST endpoints built from the same definition as MCP and the CLI.

Charter serves a JSON:API-shaped Representational State Transfer (REST) API for every enabled resource. The same [permissions](/docs/roles-and-permissions/) that gate Model Context Protocol (MCP) apply here.

To make your first request, send a Bearer credential to a resource list:

```sh
curl http://127.0.0.1:3000/v1/posts \
  -H "Authorization: Bearer crt_…"
```

Keys use the `crt_` prefix. PASETO access tokens from sign-in or `/api/auth/exchange` work the same way in the Bearer header.

Prefer `Content-Type: application/vnd.api+json` for document bodies. Registry writes under `/registry/...` take flat JSON; their responses are JSON:API, same as `/v1`.

> [!INFO]
> `GET /v1/{resource}` and `GET /v1/{resource}/{id}` also speak Server-Sent Events when `Accept` prefers `text/event-stream`. Same URI; see [Realtime](/docs/realtime/).

## Paths

| Method | Path | Action |
| --- | --- | --- |
| `GET` | `/v1/{resource}` | List (or SSE stream) |
| `POST` | `/v1/{resource}` | Create |
| `GET` | `/v1/{resource}/{id}` | Retrieve (or SSE stream) |
| `PUT` | `/v1/{resource}/{id}` | Replace |
| `PATCH` | `/v1/{resource}/{id}` | Partial update |
| `DELETE` | `/v1/{resource}/{id}` | Soft delete |
| `POST` | `/v1/{resource}/import` | CSV import |
| `POST` | `/v1/mcp` | Resource MCP (JSON-RPC record tools) |

Auth and account routes live under `/api/auth/...`. Operator tools are `POST /mcp` (Admin MCP).

## Create example

You may create a record by posting a JSON:API document:

```sh
curl -X POST http://127.0.0.1:3000/v1/posts \
  -H "Content-Type: application/vnd.api+json" \
  -H "Authorization: Bearer crt_…" \
  -d '{
    "data": {
      "type": "posts",
      "attributes": {
        "title": "New listing",
        "body": "Details here",
        "author_id": "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d",
        "tags": ["7c9e6679-7425-40de-944b-e07fc1f90ae7"]
      }
    }
  }'
```

> [!INFO]
> This sample assumes `posts` declares `author_id` (many-to-one) and `tags` (many-to-many). Omit a field the resource does not have. A to-many field you leave out still comes back as `[]` when the role may read it.

The `201` body is the saved resource. Relation ids are in `attributes`. There is no `relationships` object and no `included` array.

```json
{
  "data": {
    "type": "posts",
    "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "attributes": {
      "title": "New listing",
      "body": "Details here",
      "author_id": "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d",
      "tags": [
        "7c9e6679-7425-40de-944b-e07fc1f90ae7"
      ],
      "created_at": "2026-04-12T18:25:43Z",
      "updated_at": "2026-04-12T18:25:43Z"
    }
  }
}
```

Replace and update return the same shape. To sideload the related rows, `GET` the record with `include`.

## Document shape

A successful read returns `data`. A list also returns `meta`. Create, replace, and update return the same resource object. They do not return `relationships` or `included`.

Relation values live in `attributes`. A many-to-one or one-to-one value is one UUID string. A one-to-many or many-to-many value is an array of UUID strings. That array is empty when the caller can see no related rows. Field names are the ones declared on the resource, and only fields the role may read are present.

```json
{
  "data": {
    "type": "posts",
    "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "attributes": {
      "title": "Hello",
      "author_id": "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d",
      "tags": [
        "7c9e6679-7425-40de-944b-e07fc1f90ae7"
      ],
      "created_at": "2026-01-15T10:30:00Z",
      "updated_at": "2026-01-15T10:30:00Z"
    }
  }
}
```

`author_id` is many-to-one. `tags` is many-to-many.

### Included relationships

`include` on list or retrieve adds `relationships` and `included`. The relationship key is the alias from `relations.name`, or the field name when that alias is omitted. A to-one `data` value is one `{ "type", "id" }` object, or `null` when the relation is empty. A to-many `data` value is an array of those objects.

```
GET /v1/posts/3fa85f64-5717-4562-b3fc-2c963f66afa6?include=author,tags
```

```json
{
  "data": {
    "type": "posts",
    "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "attributes": {
      "title": "Hello",
      "author_id": "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d",
      "tags": [
        "7c9e6679-7425-40de-944b-e07fc1f90ae7"
      ],
      "created_at": "2026-01-15T10:30:00Z",
      "updated_at": "2026-01-15T10:30:00Z"
    },
    "relationships": {
      "author": {
        "data": {
          "type": "users",
          "id": "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d"
        }
      },
      "tags": {
        "data": [
          {
            "type": "tags",
            "id": "7c9e6679-7425-40de-944b-e07fc1f90ae7"
          }
        ]
      }
    }
  },
  "included": [
    {
      "type": "users",
      "id": "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d",
      "attributes": {
        "name": "Ada"
      }
    },
    {
      "type": "tags",
      "id": "7c9e6679-7425-40de-944b-e07fc1f90ae7",
      "attributes": {
        "name": "Draft"
      }
    }
  ]
}
```

A dotted path can continue through a to-one relation, up to depth 3. A dotted path under a to-many relation returns 400.

Errors use an `errors` array. Each entry carries a machine-readable `code`. See [Errors](/docs/errors/).

## Registry

Operator routes under `/registry/...` take flat JSON bodies and return JSON:API documents. They require an operator account. The running server's `/docs/registry/...` pages are the try-it reference. The collections are resources, fields, permissions, and webhooks, plus:

| Method | Path | Action |
| --- | --- | --- |
| `GET` | `/registry/resources/{resource}/fields` | List fields attached to a resource |
| `POST` | `/registry/resources/{resource}/fields` | Attach a field (`field` is a definition id or name) |
| `GET` | `/registry/resources/{resource}/fields/{field}` | Fetch one binding |
| `PATCH` | `/registry/resources/{resource}/fields/{field}` | Update binding rules (`required`, `sortable`, `searchable`, `unique`) |
| `DELETE` | `/registry/resources/{resource}/fields/{field}` | Detach a field |
| `GET` | `/registry/ledger` | List audit rows |
| `GET` | `/registry/ledger/{id}` | Fetch one audit row |

`{resource}` and `{field}` each accept a UUID or a name. Create the field definition with `POST /registry/fields` before attaching it. The ledger is read-only.

## List query

List filters use `field__operator=value`, not JSON:API `filter[...]` or `page[...]`.

### Filters

Comparison operators: `eq`, `neq`, `gt`, `gte`, `lt`, `lte`, `in`, `nin`, `is_null`, `is_not_null`.

Text pattern operators (field must be `searchable: true` and a text-kind type): `like`, `ilike`, `contains`, `starts_with`, `ends_with`. Prefer `contains` / `starts_with` / `ends_with` for new clients; `like` and `ilike` pass the pattern through as written.

```sh
curl "http://127.0.0.1:3000/v1/posts?status__eq=published&title__contains=hello" \
  -H "Authorization: Bearer crt_…"
```

`is_null` and `is_not_null` take `true` or `1` only. To negate, use the other operator.

There is no global `?q=` full-text search. Mark fields `searchable` and filter them individually.

### Sort

`_sort` names one sortable field. Prefix with `-` for descending:

```
?_sort=-published_at
```

### Page and projection

| Param | Default | Notes |
| --- | --- | --- |
| `_limit` | `100` | Max `1000` |
| `_offset` | `0` | Max `100000` |
| `fields` | all allowed | Comma-separated attribute names |
| `include` | (none) | Relation aliases or field names. Dotted paths walk through a to-one, depth 3. A path under a to-many is rejected. |

List `meta` includes `limit`, `offset`, `total`, `pages` and `currentPage`.

## Soft delete

`DELETE` sets `deleted_at`. Later `GET` for that id returns not found. Soft-deleted rows are excluded from normal lists.

## Next steps

For the machine-readable contract, open [OpenAPI](/docs/openapi/). For error codes, see [Errors](/docs/errors/).
