# Authentication and API keys

Every request to the API (except a few public intercom endpoints) needs an API key. The key acts as the person who created it: it can see and do exactly what that person can in the dashboard, no more.

## Create a key in the dashboard

1. Sign in as an Organisation, Operator or Super Admin.
2. Open your **Profile** and choose **Manage Api**.
3. Press **Generate Token**, give it a name you will recognise later ("HR sync", "Reception kiosk"), and copy the token. It is shown only once.

To stop a key working, delete it on the same page. Delete keys you no longer use.

## Send the key

Add two headers to every request:

```http
Authorization: Bearer <your API key>
Accept: application/json
```

`Accept: application/json` makes sure errors come back as JSON instead of a web page.

## Get a token from code

If your system signs in on behalf of a person, swap their email and password for a token:

```bash
curl -X POST https://go.thebuilding.cloud/api/auth-token \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{"email":"you@company.com","password":"…","token_name":"my-integration"}'
```

```json
{
  "message": "User Login Successfully.",
  "access_token": "12|Xq3…",
  "expires_at": null,
  "data": { "id": 7, "name": "Alex Morgan", "email": "you@company.com", "role_id": 2, "status": 1 }
}
```

- Use `access_token` as the Bearer key. When `expires_at` is set, get a new token before then.
- A wrong email or password both answer `404` with the same message, on purpose.
- Only 10 tries a minute are allowed per email and address.
- Give every token a `token_name`, so you can find it in the dashboard later.

## Keep keys safe

- Treat a key like a password. Never put it in a mobile app, a web page or a public repository.
- Use one key per system, so you can switch one off without stopping the others.
- Create keys from an account that has only the access the system needs. An Operator limited to a few entrance groups is safer than an Organisation account.
- If a key leaks, delete it straight away and create a new one.

## Coming next

Keys with their own permissions (for example "open doors only"), expiry dates and a practice sandbox are planned. Until then a key always has the same access as the person who made it.
