# Responses and errors

## The reply shape

Most successful replies look like this:

```json
{
  "success": true,
  "message": "Success.",
  "data": { "id": 42, "name": "Main Entrance" }
}
```

`data` holds the record, the list or the page you asked for. A few older endpoints return the record or page directly, without `success` and `message`; the [API reference](https://go.thebuilding.cloud/developers/reference) shows the shape of each one.

Errors look like this:

```json
{
  "success": false,
  "message": "Entrance Not Found."
}
```

`message` is written for people, so you can show it to your users as it is.

When fields are missing or wrong you get `422` and a list per field:

```json
{
  "message": "The name field is required.",
  "errors": {
    "name": ["The name field is required."]
  }
}
```

## Status codes

| Code | What it means | What to do |
|---|---|---|
| `200`, `201` | It worked. | Read `data`. |
| `400` | The request didn't make sense (for example an unknown option). | Fix the request; the `message` says what's wrong. |
| `401` | No key, a wrong key, or a deleted key. | Check the `Authorization` header. |
| `403` | The key's person isn't allowed to do this, or the record is turned off. | Use an account with the right access, or ask the building manager. |
| `404` | Nothing with that ID that you can see. | Check the ID. Records in other organisations usually answer 404 too. |
| `419` | The request was treated as a signed-in browser request without its security token. | Call the API from your server with a Bearer key, not from a page on our own domain. |
| `422` | Some fields are missing or wrong. | Show the messages in `errors` next to the fields. |
| `429` | Too many requests. | Wait the number of seconds in `Retry-After`, then try again. |
| `500`, `503` | Something went wrong on our side. | Try again a little later. If it keeps happening, email help@thebuilding.cloud with the time and the endpoint. |

## Good habits

- Always send `Accept: application/json`.
- Retry only on `429`, `500` and `503`, and wait longer after each try.
- Don't retry a door opening on a timeout without checking the access log first: the door may already have opened.
- Log the time, the endpoint and the status when something fails. It makes support much faster.
