# Content API

Read a site’s CMS from its own code: a read-only JSON API, a key in an environment variable, and TypeScript types generated for your collections.

The Content API serves one site’s collections and items as JSON, read-only, to a server or a build step. Open the site’s **Content API** for the address, the keys and **Copy .env lines**.

## Keys

| Key | Starts with | Serves |
| --- | --- | --- |
| **Production** | `cml_live_` | Published items, as last published. Cached for 30 seconds. |
| **Development** | `cml_test_` | Every item as it is being edited, drafts marked `draft: true`. Never cached. |

> **Warning:** A development key serves drafts and unapproved edits. Never ship one in a public deployment: the live site reads with a production key.

1. **Create a key** On the site’s **Content API**, press **Create a key** (owners and admins). Name it, pick **Production** or **Development**, and choose when it expires.
2. **Copy it once** The key is shown once. Use **Copy .env lines** and keep it in the server’s environment, never in code a browser downloads.
3. **Add it to the site** On **Deploys → Environment**, **Add the content key** keeps it as `STACKLUMEN_CONTENT_KEY` for every build.

.env:

```bash
STACKLUMEN_CONTENT_URL=https://www.stackide.com/terminal/api/content/v1
STACKLUMEN_CONTENT_KEY=cml_live_…
```

## Reading content

fetch:

```ts
const res = await fetch(`${process.env.STACKLUMEN_CONTENT_URL}/collections/posts?limit=20`, {
  headers: {Authorization: `Bearer ${process.env.STACKLUMEN_CONTENT_KEY}`},
});
if (!res.ok) throw new Error(`Content API ${res.status}`);
const {items, pagination} = await res.json();
```

curl:

```bash
curl https://www.stackide.com/terminal/api/content/v1/collections/posts \
  -H "Authorization: Bearer $STACKLUMEN_CONTENT_KEY"
```

## Routes

| Route | Returns |
| --- | --- |
| `GET /` | The site, its languages and time zone, the key’s environment and every collection |
| `GET /collections` | Collections with their fields and item counts |
| `GET /collections/:collection` | Items, a page at a time |
| `GET /collections/:collection/items/:item` | One item, by slug or id |
| `GET /types` | TypeScript for this site’s collections, with a small client, as plain text |
| `GET /sites` | The sites the key reads |
| `GET /sites/:site/…` | Any route above, for one named site: how a key for every site reads each one |

### List parameters

| Parameter | Values |
| --- | --- |
| `limit` | 1–100 (default 20) |
| `offset` | 0–20,000 |
| `sort` | `position` (the CMS order, the default), `slug`, or a field key |
| `order` | `asc` or `desc` |
| `filter.<key>` | An exact match on a field, ignoring case; up to 10 filters |
| `locale` | One of the site’s languages |

## Types for your collections

`GET /types` answers TypeScript for this site: a type for every collection and a small client with no dependencies. Fetch it in a build step to keep your types in step with the CMS.

```bash
curl https://www.stackide.com/terminal/api/content/v1/types \
  -H "Authorization: Bearer $STACKLUMEN_CONTENT_KEY" > src/cms.ts
```

## Plans

Content API keys are free on every site while it is on its staging address. On its own domain, a site needs the **Premium** [site plan](https://www.stackide.com/docs/billing#site-plans) for its keys: a key on a site on **Basic** answers `402`, with a sentence that names the plan.
