# Errors

Every status code the Mojimoto API returns, the JSON an error comes back as and the messages you are most likely to meet, with the fix for each.

When a request fails, the API answers with an HTTP status in the 400s or 500s and a JSON body. The body always has a `message` written for a person to read. When the problem is with something you sent, it also has an `errors` object that says which field was wrong.

This request tries to create a blog post without a `uid`:

```bash
curl -X POST https://mojimoto.com/api/v1/acme/manage/documents \
  -H "Authorization: Bearer mjmt_your_write_token" \
  -H "Content-Type: application/json" \
  -d '{"type": "post"}'
```

```json Response 422
{
  "message": "A URL slug is required - \"post\" is a collection (format \"page\"), so each entry is addressed by uid. Only types with format \"single\" or \"component\" omit it.",
  "errors": {
    "uid": [
      "A URL slug is required - \"post\" is a collection (format \"page\"), so each entry is addressed by uid. Only types with format \"single\" or \"component\" omit it."
    ]
  }
}
```

## Error format

Every error is JSON, whatever `Accept` header you send. There are two shapes.

Most errors have one key:

```json Response 403
{ "message": "Token does not belong to this project." }
```

A `422` adds `errors`. Each key names what was wrong and holds a list of messages about it. `message` repeats the first of them, and says how many more there are when there is more than one:

```json Response 422
{
  "message": "The field field is required. (and 1 more error)",
  "errors": {
    "field": ["The field field is required."],
    "definition": ["The definition field is required."]
  }
}
```

A key in `errors` is one of these:

| Key | When |
| --- | --- |
| A field of the request body, such as `type`, `uid`, `lang`, `data` or `file` | That field is missing or cannot be used. |
| A field id of your content type, such as `title` | The value breaks one of the field's rules when you publish, or cannot be stored. |
| A path into a blocks field, such as `sections.0.data.heading` | A field inside a block breaks a rule. The number is the block's position, counted from zero. |
| `schema` | A change to a field would break the rules of the content model. |
| A query parameter, such as `type`, `select` or `fields.nope` | A filter on the Delivery API cannot be applied. |
| `payload`, `format`, `content_type` or `unpublish` | The request as a whole was refused. The message says why. |

In code, branch on the status and on the keys of `errors`, not on the wording of a message.

## Status codes

| Status | When you get it |
| --- | --- |
| `200` | The request worked. |
| `201` | A content type, a field, an entry or an asset was created. |
| `304` | Delivery API only. Nothing has changed since the `ETag` you sent back in `If-None-Match`. The body is empty. See [HTTP details](/docs/api/http-details). |
| `401` | There is no token on the request, or the token is not one Mojimoto knows. |
| `402` | The project's subscription has ended and its grace period is over. |
| `403` | The token is real but is not allowed to do this. |
| `404` | The project, entry, content type, asset or path does not exist, or the entry has nothing to serve in that locale. |
| `405` | The path exists but not with that HTTP method. The `Allow` header lists the methods it takes. |
| `413` | The request body is bigger than the server accepts. |
| `422` | The request was understood but something in it breaks a rule. |
| `429` | Too many requests in a minute, or too many wrong tokens from your IP address. |
| `500` | Something went wrong on Mojimoto's side. The body is `{"message": "Server Error"}`. |

One thing that looks like an error and is not: asking for drafts with a token that lacks the `preview` ability returns published content with a `200`. See [Authentication](/docs/api/authentication#abilities).

## Token errors

A `401` means the API does not know who is calling. A `403` means it knows the token and the token is not allowed to do what was asked.

```bash
curl https://mojimoto.com/api/v1/acme/documents
```

```json Response 401
{ "message": "Missing API token. Provide it as a Bearer token in the Authorization header." }
```

[When a token is refused](/docs/api/authentication#when-a-token-is-refused) lists every token check in the order it runs, with the message for each.

## Not found errors

A `404` body has the same single `message` key. Several of the messages come straight from the framework and name an internal class, so read them for the id rather than the wording.

| Message | What was not found |
| --- | --- |
| `No query results for model [App\Models\Project] nope` | The project in the path. Check the slug. |
| `No query results for model [App\Models\Document] 999` | No entry with that id in this project. An id from another project is a `404` too. |
| `No query results for model [App\Models\ContentType].` | Management API: the content type in the path or in `type`. |
| `This project has no content type called "recipe".` | Delivery API: the content type in the path. |
| `No query results for model [App\Models\Asset] 999` | No asset with that id. |
| `No content for that locale.` | Delivery API: the entry exists but has nothing published in that locale, or has never been saved. |
| `The route api/v1/acme/nope could not be found.` | The path itself. An entry id that is not a number gets this too. |

The project in the path is looked up before the token is checked, and so is the entry or asset id on the Delivery API, so these can come back even when the token is missing or wrong. A token limited to some content types gets a `404` with an empty message for an entry of another type.

## Validation errors

A `422` means the request reached the right place and was read, but something in it breaks a rule. The `errors` object says what.

- A missing or badly typed body field reads like `The type field is required.` or `The data field must be an array.`
- A body that is not valid JSON is read as an empty body, so you get a `422` about a required field. Check the JSON and the `Content-Type: application/json` header.
- Content rules such as `required` are checked when you publish, not when you save a draft. [Validation](/docs/api/management#validation) lists each rule and its message.

A `422` means nothing was changed, with one exception: creating an entry with `data` that cannot be saved leaves an empty entry behind. See [Create an entry](/docs/api/management#create-an-entry).

## Rate limit errors

There are two kinds of `429`.

The first is the rate limit. You have sent more requests in a minute than the limit allows:

```json Response 429
{ "message": "Too Many Attempts." }
```

It comes with headers that say when to try again. `Retry-After` is in seconds and `X-RateLimit-Reset` is a Unix timestamp:

```text
Retry-After: 60
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 1795695560
```

Wait for `Retry-After`, then carry on. Successful responses carry `X-RateLimit-Limit` and `X-RateLimit-Remaining` too, so a script can slow down before it hits the limit. The limits themselves are in [HTTP details](/docs/api/http-details).

The second is the lock on wrong tokens. It has no `Retry-After` header and clears within a minute:

```json Response 429
{ "message": "Too many failed authentication attempts. Try again later." }
```

It means something at your IP address sent 30 unknown tokens inside a minute. See [Too many wrong tokens](/docs/api/authentication#too-many-wrong-tokens).

## Subscription ended

```json Response 402
{ "message": "Subscription required. The project's subscription ended and its 30-day grace period has passed - renew it to restore API access." }
```

When a project's trial or subscription ends, its tokens keep working through a 30-day grace period, so a live site does not go down the day a payment fails. After that every request with one of the project's tokens gets this `402`, on the Delivery API and the Management API alike. Renewing the subscription brings the same tokens back. A project on the Free plan never gets it.

## Error messages

These are the messages you are most likely to meet. Names in quotes, such as `"post"`, are filled in from your request.

### Tokens and access

| Message | Status | Cause and fix |
| --- | --- | --- |
| `Missing API token. Provide it as a Bearer token in the Authorization header.` | `401` | No `Authorization: Bearer` header. A token in the query string is not read. Add the header. |
| `Invalid API token.` | `401` | The token is mistyped, cut short or revoked. Copy it again or create a new one. |
| `Token does not belong to this project.` | `403` | The token is for a different project from the one in the URL. Use that project's token, or fix the slug. |
| `Token lacks the "write" ability.` | `403` | A read token was used on the Management API. Create a token with `write`. |
| `This token isn't allowed to publish and unpublish. Ask a project admin for a token with that permission.` | `403` | The write token is limited and this operation was not ticked. The message names it: create and edit drafts, publish and unpublish, delete entries, upload media or change content models. |
| `This token can only work with these content types: post.` | `403` | The token is limited to the content types listed. Use a token that covers the type. |
| `This token is limited to certain content types, so it cannot list the media library. Media used in those entries is included with them.` | `403` | A token limited to content types asked for the media library. Read media from the entries, or use a token with no content type limit. |

### Entries

All of these are a `422`.

| Message | Cause and fix |
| --- | --- |
| `A document with that slug already exists.` | Another entry of the type has that `uid`. Choose another, or update the entry that has it. |
| `This single entry already exists.` | A `single` type holds one entry and it is already there. Save a draft of that entry instead. |
| `The URL slug "second-post" is already used by another entry.` | The value in the type's slug field belongs to another entry. Change the slug. |
| `This project has no locale "fr-fr". It has: en-gb. Add it under Settings, Locales, then write to it.` | `lang` is not a locale of the project. Use one it lists, or add the locale in the dashboard. |
| `The data field is required.` | A draft was saved with no `data`, or with an empty object. |
| `Title is required.` | A required field is empty at publish. Fill it and publish again. |
| `Author must reference a valid entry.` | A reference field holds something that is not an entry id, such as a `uid`. Send the numeric id. |
| `Author references an entry that does not exist in this project.` | The id is wrong, the entry was deleted, or it is in another project. |
| `Hero image links to a file that does not exist in this project.` | The `asset_id` is wrong or belongs to another project. Upload the file to this project. |
| A message starting `Media fields store` | A media field was sent an image object without an `asset_id`. Upload the file and send `{"asset_id": 15}`. |
| `Sections, block 2: unknown block type. Use one of: hero, quote.` | A block's `type` is not one the blocks field defines. |
| `This locale is not published - there is nothing to unpublish.` | The entry is not published in that locale. Check `lang`. |

### Content types and fields

All of these are a `422`. Messages about the rules of the model come under `errors.schema`.

| Message | Cause and fix |
| --- | --- |
| A message starting `Unknown parameter(s): kind.` | The body for a new content type has a key the endpoint does not know. Send only `api_id`, `label`, `format` and `icon`. |
| `A content type "post" already exists.` | The `api_id` is taken. Nothing is overwritten. |
| `The api_id "Blog Post" must be snake_case.` | Ids start with a lower-case letter and use only lower-case letters, digits and underscores. A bad field id gets a matching message. |
| `The selected format is invalid.` | `format` has to be `page`, `single` or `component`. |
| `"post" has multiple entries, so it cannot become a single. Delete the extras first.` | A format change to `single` with more than one entry. |
| `"nav_item" has entries without a uid, so it cannot become a collection. Give every entry a slug first.` | A format change to `page` while some entries have no `uid`. |
| `"post" still has documents. Delete them first - removing a type never deletes content as a side effect.` | A content type with entries cannot be deleted. Delete its entries first. |
| `Field "title" already exists on "post". Use update to change it.` | Adding a field id the type already has. Update that field instead. |
| `Field "nope" does not exist on "post".` | Updating or removing a field id the type does not have. |
| `Field "body" in Main has unknown type "markdown".` | The field type is not one of the fourteen. |
| `A content type may only have one slug field.` | The type already has a `slug` field. |
| `Reference field "author" must allow at least one content type.` | A reference field needs `config.content_types`. |
| `Reference field "author" points at unknown content type "nope".` | A type named in `config.content_types` is not in the project. Create it first. |
| `Blocks field "sections" needs at least one block type in config.blocks, keyed by api id.` | A blocks field was defined with no block types. |

### Uploads and plan limits

All of these are a `422`.

| Message | Cause and fix |
| --- | --- |
| `The file field is required.` | The upload was not `multipart/form-data`, or the file was not in a field called `file`. |
| `The file field must not be greater than 20480 kilobytes.` | The file is over 20 MB. |
| `The Free plan includes 2,000 records (entries and media files). Upgrade the project to add more.` | A Free project is at its limit of entries and media files together. Delete some, or upgrade. |
| `The Free plan includes 1,024 MB of media. Upgrade the project to upload more.` | A Free project's media library is full. |
| `This project has no locale "de-de". It has: en-gb, es-es, fr-fr. Its plan includes 3 locales and they are all in use.` | The locale does not exist and the plan has no room for another. Write to a locale the project has. |

### Reading content

| Message | Status | Cause and fix |
| --- | --- | --- |
| `Filtering or sorting by a field needs a content type, because fields belong to one. Add it to the request, like ?type=post&fields.title=Hello` | `422` | A field filter with no `type`. Add `type`. |
| `"post" has no field called "nope". Its fields are: slug, title, meta_title, meta_description.` | `422` | The field in a filter is not on the content type. Use one of the ids listed. |
| `No content for that locale.` | `404` | The entry is not published in that locale. Publish it, or check `lang`. |
| `The PATCH method is not supported for route api/v1/acme/manage/documents/1. Supported methods: PUT, DELETE.` | `405` | Wrong HTTP method. Use one the message lists. |
