# Content Preview API

Read unpublished drafts through the delivery endpoints with a preview token and ref=preview, for staging sites and draft modes.

The Content Preview API is the [Content Delivery API](/docs/api/delivery) with drafts switched on. It has the same URLs, the same parameters and the same response shape. The difference is what it serves: the newest saved version of every entry, published or not.

Use it on a staging site or in your framework's draft mode, so editors can see a change in place before they publish it. Do not use it for anything your visitors load.

Two things switch it on, and a request needs both: a token with the `preview` ability, and `ref=preview` in the query string.

```bash
curl "https://mojimoto.com/api/v1/acme/documents/9?ref=preview&select=fields.title,fields.category" \
  -H "Authorization: Bearer mjmt_your_preview_token"
```

```json
{
  "id": 9,
  "uid": "summer-menu",
  "type": "post",
  "lang": "en-gb",
  "status": "draft",
  "published_at": null,
  "updated_at": "2026-06-03T15:22:31+00:00",
  "data": {
    "title": "Summer menu (draft)",
    "category": "news"
  }
}
```

That entry has never been published. Without preview the same URL is a `404`.

## Turn preview on

### Create a preview token

On the project's API keys page, under Custom tokens, give the token a name, tick `preview` and create it. The token is shown once, so copy it then. Every token can read, and `preview` is added on top.

The `write` ability does not include `preview`. A token that can write but was not given `preview` is treated like a read token here. [Authentication](/docs/api/authentication) covers abilities in full.

### Add ref=preview

```http
GET /api/v1/{project}/documents?ref=preview
GET /api/v1/{project}/documents/{id}?ref=preview
```

| Parameter | Type | Description |
| --- | --- | --- |
| `ref` | string | `preview` serves drafts. The value is matched exactly, in lower case. Any other value is ignored and the request is an ordinary delivery request. |

Every other parameter of the Delivery API works alongside it: `type`, `uid`, `lang`, `page`, `per_page`, `sort`, `fields.{field}` filters, `select` and `depth`.

`ref` only means something on the two entry endpoints. The content types, assets and generated types endpoints have no drafts, so they return the same response with or without it.

### What each combination returns

| Token | `ref=preview` | What you get |
| --- | --- | --- |
| Has `preview` | Sent | Drafts. Never cached. |
| Has `preview` | Not sent | Published content, cached like any delivery response. |
| No `preview` | Sent | Published content, with a header saying preview was denied. Never cached. |
| No `preview` | Not sent | Published content, cached like any delivery response. |

A preview token is a read token too. One token can serve a staging site in both modes, and the query parameter decides which.

## What preview serves

For each entry and locale, preview serves the newest saved version, whichever kind it is:

- An entry that has never been published is served from its draft.
- A published entry with a newer draft is served from the draft.
- A published entry with no newer draft is served from its published version, as on delivery.
- An entry that was unpublished is served from the draft that unpublishing left behind.

So a preview list is the published list plus everything that is still being worked on. Each entry says which kind it is:

```bash
curl "https://mojimoto.com/api/v1/acme/documents?type=post&uid=summer-menu&ref=preview&select=fields.title,fields.category" \
  -H "Authorization: Bearer mjmt_your_preview_token"
```

```json
{
  "results": [
    {
      "id": 9,
      "uid": "summer-menu",
      "type": "post",
      "lang": "en-gb",
      "status": "draft",
      "published_at": null,
      "updated_at": "2026-06-03T15:22:31+00:00",
      "data": {
        "title": "Summer menu (draft)",
        "category": "news"
      }
    }
  ],
  "page": 1,
  "per_page": 20,
  "total_results": 1,
  "total_pages": 1,
  "lang": "en-gb",
  "ref": "preview"
}
```

| Field | On delivery | On preview |
| --- | --- | --- |
| `status` on an entry | Always `published` | `draft` or `published` |
| `published_at` on an entry | A timestamp | `null` when the entry is served from a draft |
| `updated_at` on an entry | When the published version was saved | When the version being served was saved |
| `ref` on a list | `published` | `preview` |

A single entry has no `ref` field. Read its `status` instead.

Here is a published entry that has a newer draft, first on preview and then on delivery:

```bash
curl "https://mojimoto.com/api/v1/acme/documents/7?ref=preview&select=fields.title" \
  -H "Authorization: Bearer mjmt_your_preview_token"
```

```json
{
  "id": 7,
  "uid": "weekend-croissants",
  "type": "post",
  "lang": "en-gb",
  "status": "draft",
  "published_at": null,
  "updated_at": "2026-06-04T11:08:45+00:00",
  "data": {
    "title": "Weekend croissants, the long way"
  }
}
```

```bash
curl "https://mojimoto.com/api/v1/acme/documents/7?select=fields.title" \
  -H "Authorization: Bearer mjmt_your_read_token"
```

```json
{
  "id": 7,
  "uid": "weekend-croissants",
  "type": "post",
  "lang": "en-gb",
  "status": "published",
  "published_at": "2026-04-18T08:41:53+00:00",
  "updated_at": "2026-04-18T08:41:53+00:00",
  "data": {
    "title": "Weekend croissants"
  }
}
```

The rest of the request follows the drafts as well:

- Linked entries are resolved to their drafts, so a page and the entries it links to are previewed together.
- `fields.{field}` filters and field sorts test the draft values, not the published ones.
- Locale fallback fills empty fields from the newest versions of the fallback locales, drafts included.

Three things to expect from draft content:

- Drafts are not validated. Required fields are only enforced on publish, so a draft can be missing any of them. Write preview code that copes with an empty field.
- The editor saves drafts as people type. Preview shows work in progress without anyone pressing a button.
- An entry that was created with no content saved has no version at all. It is left out of preview lists and is a `404` by id, the same as on delivery.

## How preview responses differ

| | Delivery | Preview |
| --- | --- | --- |
| Content | Published versions only | The newest version, draft or published |
| `Cache-Control` | `max-age=0, public, s-maxage=60, stale-while-revalidate=86400` | `no-store, private` |
| `ETag` | Sent | Not sent |
| `If-None-Match` | Answered with `304` when nothing changed | Ignored. Every request gets a full `200`. |
| `X-Mojimoto-Content-Version` | Sent | Not sent |
| Rate limit | Counted | Counted in the same way |

A preview response is never stored by a browser or a shared cache, and every preview request is answered by the API itself. A change shows up on the next request after the draft is saved, with no minute to wait as there can be on delivery. [HTTP details](/docs/api/http-details) explains the delivery side.

If your framework caches fetches of its own accord, turn that off for preview requests, or editors will see a stale draft.

## When the token cannot preview

A request with `ref=preview` and a token that lacks the `preview` ability is not refused. It gets published content with a `200`, so a staging site with the wrong token keeps working. The response is marked so you can tell:

```bash
curl -i "https://mojimoto.com/api/v1/acme/documents?type=post&uid=summer-menu&ref=preview" \
  -H "Authorization: Bearer mjmt_your_read_token"
```

```text
Cache-Control: no-store, private
X-Mojimoto-Preview: denied - token lacks the "preview" ability, serving published content
```

```json
{
  "results": [],
  "page": 1,
  "per_page": 20,
  "total_results": 0,
  "total_pages": 1,
  "lang": "en-gb",
  "ref": "published"
}
```

The draft is not in the list, and `ref` says `published`. On a single entry the published version is served if there is one. If there is none it is the usual `404`:

```json
{
  "message": "No content for that locale."
}
```

To check that preview is on, look at `ref` on a list response: it says `preview`. On a single entry, check that the `X-Mojimoto-Preview` header is not there. Browser JavaScript on another origin cannot read that header, so check it from a server or with curl.

A token that is missing, not valid or from another project is refused in the usual way. See [Errors](/docs/api/errors).

## What not to use it for

- **Production traffic.** Preview responses are never cached, so every page view would reach the API and count towards the [rate limit](/docs/api/http-details). Published content belongs on the Delivery API.
- **Anything in a browser.** A preview token reads content that is not public yet. Keep it in server-side code and server-side environment variables, and never ship it in a client bundle or a public repository.
- **Reading history.** Preview serves the newest version of each entry and nothing else. It cannot return an older version or a version by its id.
- **Changing content.** It is read-only, like the Delivery API. Saving, publishing and unpublishing go through the [write API](/docs/api/management).

## Wire it to a site

The usual set-up is one code path and two configurations: production uses a read token and sends no `ref`, staging uses a preview token and sends `ref=preview`. The JavaScript client adds the parameter for you when its `preview` option is on. See [JavaScript](/docs/frameworks/javascript).

[Preview](/docs/concepts/preview) explains how previewing fits into an editor's day.
