# Open a door from your own app

A reception desk, a visitor kiosk or your own staff app can open doors with one request. This recipe finds the door, opens it and handles every answer.

**You need:** an API key from an account that may open the door (see [Authentication and API keys](https://go.thebuilding.cloud/developers/guides/authentication)).

## 1. Find the door

List the entrances the key can see. Filter by building if you know it:

```bash
curl "https://go.thebuilding.cloud/api/entrances?building_id=7&per_page=50" \
  -H "Authorization: Bearer $TBC_KEY" \
  -H "Accept: application/json"
```

Keep the `id` of the door. Entrance IDs don't change, so you can store them in your app's settings instead of looking them up every time. `status` `1` means the entrance is turned on.

## 2. Open it

```bash
curl -X POST https://go.thebuilding.cloud/api/entrances/42/unlock \
  -H "Authorization: Bearer $TBC_KEY" \
  -H "Accept: application/json"
```

The same request in JavaScript:

```js
const res = await fetch(`https://go.thebuilding.cloud/api/entrances/${doorId}/unlock`, {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.TBC_KEY}`, Accept: "application/json" },
});
const body = await res.json();
if (!res.ok) throw new Error(body.message);
```

And in PHP (Laravel):

```php
$res = Http::withToken(config('services.tbc.key'))
    ->acceptJson()
    ->post("https://go.thebuilding.cloud/api/entrances/{$doorId}/unlock");

if ($res->failed()) {
    throw new RuntimeException($res->json('message'));
}
```

## 3. Handle the answer

| Status | What happened | What to show |
|---|---|---|
| `200` | The door was told to open. The opening is logged under the key's person. | "Door open". |
| `403` | No access to this door, the entrance is turned off, or the person is too far away for a door that only opens nearby. | The `message` as it is. |
| `404` | The ID is wrong, the door isn't in this organisation, or its controller isn't set up ("Firebird Device Not Found."). | "Door not found" and check your settings. |
| `422` | The door only opens nearby and no position was sent. | Ask for the location and send it (below). |
| `429` | Too many requests. | Wait the seconds in `Retry-After`. |

A `200` means the command was sent. If the door's controller is offline the door may not move; the access log shows what really happened.

## Doors that only open nearby

Some doors are set to open only when the person is close to the building. Send their position in the body:

```bash
curl -X POST https://go.thebuilding.cloud/api/entrances/42/unlock \
  -H "Authorization: Bearer $TBC_KEY" \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -d '{"lat": 51.5072, "lng": -0.1276}'
```

## Opening for someone else

A reception desk often opens the door for a visitor. Managers (Organisation, Operator) can add `"user_id": <the person's ID>` to the body, and the access log names that person instead.

## Don't

- Don't retry an opening automatically after a timeout: the door may already have opened. Check the access log first.
- Don't put the API key in a web page or a mobile app. Call the API from your server.
