# Links between entries

Reference fields link one entry to others - how to define and write them, how they come back, how depth follows them and what a broken link returns.

A reference field links an entry to other entries: a post to its author, a product to its category, a page to a call to action it shares with other pages. You write the linked entry's id. The read API gives you the linked entry itself, so one request returns the post and its author together.

Use a link when the thing you point at has a life of its own or is used in more than one place. For sections that belong to a single page, use [blocks](/docs/concepts/blocks) instead.

## One or many

| Type | Holds | You write | You read |
| --- | --- | --- | --- |
| `reference` | One entry | An entry id, `3` | The linked entry, or `null` |
| `references` | An ordered list of entries | An array of ids, `[21, 22]` | An array of linked entries, in that order |

## Restrict a field to content types

Every reference field names the content types it may link to, in `content_types`. The list is required and needs at least one content type that exists in the project.

```bash
curl -X POST https://mojimoto.com/api/v1/acme/manage/content-types/post/fields \
  -H "Authorization: Bearer mjmt_your_write_token" \
  -H "Content-Type: application/json" \
  -d '{"field": "author", "definition": {"type": "reference", "config": {"label": "Author", "content_types": ["author"]}}}'
```

In the app, the entry picker for the field only offers entries of those types. The write API checks that a linked entry exists in the project. It does not check the entry's type against the list, so keep to the list in your own scripts.

## Write a link

Put ids in `data`. The order of a `references` array is kept and is the order you get back.

```json request body
{ "data": { "title": "Hello world", "author": 3, "tags": [21, 22] } }
```

A draft can hold any id. Publishing checks that every id is an entry in the same project, and answers `422` if one is not. To clear a link, write `null` or an empty array.

## How links come back

```bash
curl https://mojimoto.com/api/v1/acme/documents/42 \
  -H "Authorization: Bearer mjmt_your_read_token"
```

```json response
{
  "id": 42,
  "uid": "hello-world",
  "type": "post",
  "lang": "en-gb",
  "status": "published",
  "published_at": "2026-06-12T14:30:45+00:00",
  "updated_at": "2026-06-12T14:30:45+00:00",
  "data": {
    "title": "Hello world",
    "author": {
      "id": 3,
      "uid": "sam-okafor",
      "type": "author",
      "lang": "en-gb",
      "status": "published",
      "published_at": "2026-05-02T09:12:00+00:00",
      "updated_at": "2026-05-02T09:12:00+00:00",
      "data": {
        "name": "Sam Okafor",
        "team": { "id": 9, "type": "team", "uid": "editorial" }
      }
    },
    "tags": [
      {
        "id": 21,
        "uid": "baking",
        "type": "tag",
        "lang": "en-gb",
        "status": "published",
        "published_at": "2026-04-20T08:00:00+00:00",
        "updated_at": "2026-04-20T08:00:00+00:00",
        "data": { "label": "Baking" }
      }
    ]
  }
}
```

The author is a whole entry in the same envelope as the post. The author's own link, `team`, is a **stub**: the id, type and uid with no `data`. That is where the read API stopped following links.

A link holds only the id of its target. It is looked up on each request, so publishing a new version of the author changes every post's response without touching the posts.

## Follow links with depth

`depth` sets how many levels of links are filled in. It goes on the list and on a single entry.

| `depth` | What is filled in |
| --- | --- |
| `1` | Entries the entry links to. This is the default. |
| `2` | Entries those entries link to as well. |
| `3` | One level further. This is the most. |

A value under 1 is read as 1 and a value over 3 as 3. Past the last level, a link is a stub. To tell the two apart, check for `data`.

```bash
curl "https://mojimoto.com/api/v1/acme/documents/42?depth=2" \
  -H "Authorization: Bearer mjmt_your_read_token"
```

With `depth=2` the author's `team` above becomes a whole entry. Links can run in a circle, and `depth` is what ends the loop.

Media fields do not count towards depth. A file is always returned as a full object. `select` saves lookups: links in fields you did not select are not followed.

## Preview and locales

Linked entries follow the request they are part of. A [preview](/docs/concepts/preview) request fills in the newest version of each linked entry, draft or published. Linked entries are served in the locale of the request, with the same fallbacks. See [Locales](/docs/concepts/locales).

## What a link can come back as

| Situation | `reference` | Inside `references` |
| --- | --- | --- |
| The target is published and within `depth` | The entry | The entry |
| The target is past `depth` | Stub | Stub |
| The target exists but has nothing published in this locale | Stub | Stub |
| The target was deleted | `null` | Left out of the array |
| The field is empty | `null` | `[]` |

So unpublishing a target does not break the entry that links to it. The link becomes a stub, and a whole entry again when the target is published.

A deleted target is different. The entry that linked to it is still served, but the dead id stays in its content, and the next time you publish that entry the check fails with `Author references an entry that does not exist in this project.` Remove the id first.

## Find entries by what they link to

The read API can filter a list by a link, using the linked entry's id.

```bash
curl "https://mojimoto.com/api/v1/acme/documents?type=post&fields.author=3" \
  -H "Authorization: Bearer mjmt_your_read_token"
```

On a `references` field the same filter means "the list holds this entry", so `fields.tags=21` finds every post tagged with entry 21. `[ne]`, `[in]`, `[nin]` and `[exists]` work on both. The [delivery API reference](/docs/api/delivery) has the full list of filters.

## Links in the app

In the entry editor a reference field is a picker. It lists entries of the allowed types, can create and link a new entry in one step, and lets editors drag a `references` list into order.

When you press **Publish to site**, the app looks through the entry's links, up to four levels down, for entries that have never been published or have unpublished changes. You tick the ones to publish with it. A linked entry that fails its own checks is skipped and named, and the rest still go live. Scheduling a publish offers the same list.

The API publishes one entry per call. Publish the linked entries first, then the entry that links to them.

## Links or blocks

Reference fields are for content that is shared or stands alone. For sections that belong to one page, a blocks field keeps them inside the entry, with nothing to publish separately and no `depth` to think about. A field inside a block cannot be a reference. [Blocks](/docs/concepts/blocks) compares the two side by side.
