# Authentication

Every request to the Management API sends an API key in the `Authorization` header as a Bearer token. An API key is a secret, and it never ships inside your application.

## API keys

An API key belongs to one [integration](../integrations-and-scopes/). Create the key on the integration in the dashboard. A request with the key can do what the [scopes](../integrations-and-scopes/#scopes) of the integration allow.

The dashboard shows the key only once, when you create it. Seatify cannot show the key again. Keep the key in a secret store, such as the secrets of your CI system. If the key is lost, create a new key and delete the old one.

| Header | Required | Description |
| --- | --- | --- |
| `Authorization` | Yes | The word `Bearer`, a space and the API key. |

The Management API does not accept a publishable key. A publishable key is for the [License API](../../license-api/authentication/).

## Several keys

An integration can have more than one API key. For example, give each server or each CI system its own key. Every key of an integration has the same scopes and the same product.

## Expiry and deletion

A key can have an expiry date. Choose the date when you create the key. The date cannot be changed later. After that date, the Management API answers every request with the key with the status 401. The expired key stays in the dashboard until you delete it.

To stop a key immediately, delete it. The Management API then answers every request with the key with the status 401. A deleted key gets the same answer as a key that never existed.

## Rotate a key

1. Create a second key on the same integration.
2. Put the new key in your backend or CI job.
3. Delete the old key.

Both keys work until you delete the old key. The integration keeps its scopes and its product.

## Errors

| Status | Message | Description |
| --- | --- | --- |
| 401 | `An API key is required.` | The request has no `Authorization` header, the header is empty, or the header does not use the `Bearer` scheme. |
| 401 | `The API key is not valid.` | No integration has this key. The key was deleted, or it has a typing error. |
| 401 | `The API key has expired.` | The expiry date of the key has passed. |
| 403 | `This integration does not have the licenses:write scope.` | The integration does not have the scope that the endpoint requires. The message names that scope. |

The body has the error format of the Management API. See [Errors](../#errors).

## WWW-Authenticate header

Every 401 and 403 answer has a `WWW-Authenticate` header. The header follows the Bearer token standard, RFC 6750.

| Status | `WWW-Authenticate` |
| --- | --- |
| 401, no API key | `Bearer` |
| 401, key not valid or expired | `Bearer error="invalid_token"` |
| 403 | `Bearer error="insufficient_scope", scope="licenses:write"` |

In a 403 answer, `scope` names the scope that the endpoint requires.

## Example

Check an API key with [Get the current integration](../me/). The answer names the integration and its scopes.

{{< tabs >}}
{{< tab name="curl" >}}
```bash
curl https://api.seatify.dev/v1/me \
  -H "Authorization: Bearer $SEATIFY_API_KEY"
```
{{< /tab >}}
{{< tab name="Swift" >}}
```swift
var request = URLRequest(url: URL(string: "https://api.seatify.dev/v1/me")!)
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")

let (data, response) = try await URLSession.shared.data(for: request)
```
{{< /tab >}}
{{< tab name="C#" >}}
```csharp
using System.Net.Http.Headers;

using var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);

using var response = await client.GetAsync("https://api.seatify.dev/v1/me");
var body = await response.Content.ReadAsStringAsync();
```
{{< /tab >}}
{{< tab name="Go" >}}
```go
req, err := http.NewRequest("GET", "https://api.seatify.dev/v1/me", nil)
if err != nil {
	return err
}
req.Header.Set("Authorization", "Bearer "+apiKey)

res, err := http.DefaultClient.Do(req)
if err != nil {
	return err
}
defer res.Body.Close()
```
{{< /tab >}}
{{< tab name="JavaScript" >}}
```javascript
const response = await fetch("https://api.seatify.dev/v1/me", {
  headers: { Authorization: `Bearer ${apiKey}` },
});
const body = await response.json();
```
{{< /tab >}}
{{< /tabs >}}

A valid key answers with the status 200:

```json
{
  "org": {
    "id": "<org id>",
    "name": "Northwind Software"
  },
  "integration": {
    "id": "<integration id>",
    "name": "Zapier",
    "product_id": "<product id>"
  },
  "key": {
    "id": "<key id>",
    "name": "production",
    "expires_at": null
  },
  "scopes": [
    "policies:read",
    "licenses:read",
    "licenses:write",
    "customers:read",
    "customers:write"
  ]
}
```

A request without the header answers with the status 401:

```json
{
  "message": "An API key is required."
}
```

