> For the complete documentation index, see [llms.txt](https://docs.hostelmate.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hostelmate.co/api-documentation/authentication.md). Agents can also search and read this documentation through its [MCP server](https://docs.hostelmate.co/~gitbook/mcp).

# API Authentication

Authenticate API requests using your API key. Add required headers, handle rate limits, and follow security best practices.

All API requests must use HTTPS and include a valid **API key** in the `X-API-Key` header. There is no token exchange step — the API key is used directly.

If your tool can only send an `Authorization` header, `Authorization: Bearer <your_api_key>` works too.

The API works on every plan except Lite. [Webhooks](https://docs.hostelmate.co/api-documentation/webhooks.md) need the **Advanced** plan.

***

## 1. Create an API Key

From the Dashboard: **Settings → API Keys** For the first time, click **Generate** to create your key.

You'll get:

* **API Key** (shown once — copy and store securely)

> Keep the key on your server only. Do **not** embed it in browser/mobile apps.

***

## 2. Required Headers

| Header            | Required                        | Description                                                                                    |
| ----------------- | ------------------------------- | ---------------------------------------------------------------------------------------------- |
| `X-API-Key`       | All requests                    | Your API key.                                                                                  |
| `Content-Type`    | All requests                    | Must be `application/json`.                                                                    |
| `Idempotency-Key` | **All POST and PATCH requests** | A unique UUID you generate per request. Prevents duplicate processing if a request is retried. |

> **`Idempotency-Key` is enforced on write operations.** Any `POST` or `PATCH` request missing this header is rejected with `400 bad_request: "Idempotency-Key header is required"`. Generate a fresh UUID v4 for each distinct operation. You can reuse the same key to safely retry a request that timed out, but using the same key with a different payload will be rejected.

**GET request example**

```bash
curl "https://api.hostelmate.co/api/v1/client/bookings" \
  -H "X-API-Key: <your_api_key>" \
  -H "Content-Type: application/json"
```

**POST request example**

```bash
curl "https://api.hostelmate.co/api/v1/client/payments" \
  -X POST \
  -H "X-API-Key: <your_api_key>" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 550e8400-e29b-41d4-a716-446655440000" \
  -d '{"amount": "50.00", "paymentMethod": "Cash", "type": "income"}'
```

***

## 3. Rate Limits & Errors

**Rate limit:** 120 requests per minute for each API key (subject to change). Over the limit you get `429` with `retry_after`, the seconds to wait.

**Error format:** errors come back as JSON with a `code` and a `message`:

```json
{ "code": "unauthorized", "message": "Invalid API key" }
```

<table><thead><tr><th width="80">HTTP</th><th width="200">Code</th><th>When</th></tr></thead><tbody><tr><td>400</td><td><code>bad_request</code></td><td>The request is not valid, for example a missing <code>Idempotency-Key</code> on a POST or PATCH. The <code>message</code> says what to fix.</td></tr><tr><td>401</td><td><code>unauthorized</code></td><td>The API key is missing or wrong.</td></tr><tr><td>403</td><td><code>origin_not_allowed</code></td><td>The request comes from a website that is not on your allowed domains.</td></tr><tr><td>403</td><td></td><td>Your plan doesn't include this. This one has a different shape: <code>{"detail": "This feature is not available on the Lite plan.", "upgrade_required": "True"}</code>, or for webhooks on a plan other than Advanced <code>{"detail": "This feature is only available on the Advanced plan.", "upgrade_required": "True"}</code></td></tr><tr><td>404</td><td></td><td>Endpoint not found, or a record that isn't yours (each page lists its own <code>code</code>).</td></tr><tr><td>422</td><td><code>invalid_date_format</code>, …</td><td>A value can't be read, for example a date that is not YYYY-MM-DD.</td></tr><tr><td>429</td><td><code>rate_limited</code></td><td>Too many requests: <code>{"code": "rate_limited", "message": "Rate limit exceeded", "retry_after": 60}</code>. Wait, then retry with exponential backoff.</td></tr><tr><td>500</td><td><code>server_error</code></td><td>Something failed on our side. Retry with backoff; if it keeps happening, contact support.</td></tr></tbody></table>

***
