> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cimento.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication

> Exchange a service account's client ID and client secret for an access token, and send the token with each request.

The API uses the OAuth 2.0 client credentials grant. Your system sends its client ID and client secret to the token URL, gets back an access token, and sends that token with each request.

The client secret goes only to the token URL. The access token goes to every other route.

## Get an access token

Send a `POST` to the token URL:

```text theme={null}
https://api.cimento.app/oauth2/token
```

| Part | Value |
| - | - |
| `Authorization` header | HTTP Basic, with the client ID as the username and the client secret as the password. |
| `Content-Type` header | `application/x-www-form-urlencoded` |
| `grant_type` | `client_credentials`. Required. |
| `scope` | Optional. The scopes the token should carry, separated by spaces. See [Ask for fewer scopes](#ask-for-fewer-scopes). |

```bash curl theme={null}
CLIENT_ID='5247f82f-7225-4159-ad28-7ad72da4a379.0b813a8b-b803-4a51-9d0e-3f1f0c2b7a11'
curl -s -u "$CLIENT_ID" \
  -d grant_type=client_credentials \
  'https://api.cimento.app/oauth2/token'
```

```python Python theme={null}
import base64
import json
import os
import urllib.parse
import urllib.request

API = "https://api.cimento.app"


def get_token(client_id, client_secret):
    credentials = base64.b64encode(f"{client_id}:{client_secret}".encode()).decode()
    request = urllib.request.Request(
        f"{API}/oauth2/token",
        data=urllib.parse.urlencode({"grant_type": "client_credentials"}).encode(),
        headers={"Authorization": f"Basic {credentials}"},
    )
    with urllib.request.urlopen(request) as response:
        return json.load(response)["access_token"]


def get(path, token):
    request = urllib.request.Request(
        f"{API}{path}", headers={"Authorization": f"Bearer {token}"}
    )
    with urllib.request.urlopen(request) as response:
        return json.load(response)


token = get_token(os.environ["CIMENTO_CLIENT_ID"], os.environ["CIMENTO_CLIENT_SECRET"])
print(get("/v1/phishing/analytics?metrics=failure_rate", token))
```

The curl command gives curl only the client ID, so curl asks for the client secret. Paste it at the prompt. Typed there, it is not shown and is not kept in your shell history, as it would be inside a command.

The Python example reads both from environment variables. Use only the standard library, as it does, or the HTTP client you already have.

> [!NOTE]
>
> Your tool may ask for the client ID and client secret as form fields instead of a header. The token URL accepts `client_id` and `client_secret` in the body too. Use one or the other.

### The response

```json 200 OK theme={null}
{
  "access_token": "eyJhbGciOi...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "phishing_analytics:view training_courses:view"
}
```

| Field | Description |
| - | - |
| `access_token` | The token to send with each request. Treat it as opaque: don't parse it. |
| `token_type` | Always `Bearer`. |
| `expires_in` | How long the token lasts, in seconds. Always `3600`, one hour. |
| `scope` | The scopes the token carries, separated by spaces. |

### Reuse a token until it expires

An access token works for an hour. Keep it and reuse it for every request in that hour. Don't ask for a new one before each request: the token URL is [rate limited](#when-the-token-url-refuses).

There are no refresh tokens. When a token expires, or a request answers `401`, ask for a new token the same way.

### Ask for fewer scopes

Without `scope`, a token carries every scope the client secret holds. To get a token that can do less, name the scopes you want:

```bash theme={null}
curl -s -u "$CLIENT_ID" \
  -d grant_type=client_credentials \
  -d scope=training_courses:view \
  'https://api.cimento.app/oauth2/token'
```

You can ask only for scopes the client secret holds. A client secret holds the scopes its service account had when the secret was made, so a scope added since is not among them until you [rotate the secret](https://docs.cimento.ai/api/rotating-secrets.md#adding-a-scope).

## Send the access token

Send the token in the `Authorization` header of every request:

```bash theme={null}
curl -H "Authorization: Bearer $TOKEN" \
  'https://api.cimento.app/v1/phishing/analytics?metrics=failure_rate'
```

Send the access token there, never the client secret. A request with anything else as the Bearer value answers `401`.

## When the token URL refuses

The token URL answers errors in the OAuth 2.0 shape, which differs from the [rest of the API](https://docs.cimento.ai/api/errors-and-rate-limits.md):

```json 401 Unauthorized theme={null}
{
  "error": "invalid_client",
  "error_description": "Client authentication failed."
}
```

| Status | `error` | What it means | What to do |
| - | - | - | - |
| `400` | `invalid_request` | `grant_type` is missing, the body is not form-encoded, or the header and the body name different clients. | Fix the request. |
| `400` | `unsupported_grant_type` | `grant_type` is something other than `client_credentials`. | Send `client_credentials`. |
| `400` | `invalid_scope` | `scope` names a scope the client secret does not hold. | Ask for fewer scopes, or [rotate the secret](https://docs.cimento.ai/api/rotating-secrets.md#adding-a-scope) if the scope was added since it was made. |
| `401` | `invalid_client` | The client ID or client secret is wrong, the client secret was revoked or has expired, the service account is disabled or deleted, or API access is off for your organization. | Check the client ID is whole and the client secret is current. The answer is the same for every cause, on purpose. |
| `429` | `temporarily_unavailable` | Too many token requests. | Wait the seconds the `Retry-After` header gives, then try again. Reuse tokens for their hour. |
| `503` | `temporarily_unavailable` | Cimento could not issue a token just now. | Try again in a minute. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.