# Authentication

> API keys, modes, Organizations, and how each request finds the platform credential it runs with.

Every data-plane request carries one API key as a bearer token:

```http
Authorization: Bearer koil_live_...
```

A key belongs to one Organization and one mode, and both follow from the key alone: there is no Organization or mode parameter to send. Every request with a key also names the API version it was written against in `Koil-Version`; see [Versioning](https://docs.koil.co/concepts/versioning).

## Managing keys

Keys are managed in the [Koil admin console](https://admin.koil.co/), not through the API. Organization admins create, roll and revoke them; every member can see them.

-   **Create.** Name the key and choose its permissions. The console shows the full key once; Koil stores only a hash of it, so a lost key cannot be recovered, only replaced. The list names each key by its prefix and last four characters, with when it was last used (to the minute).
-   **Revoke.** The key is refused from its next request.
-   **Roll.** Rolling a key creates a new one with the same name, mode and permissions, and the old key keeps working until the expiry you choose: immediately, or up to seven days later. Deploy the new key within that window and nothing is refused.

Deleting an Organization deletes its keys.

## Key format

A key is either live (`koil_live_…`) or sandbox (`koil_sandbox_…`). After the prefix come the key's id, an underscore, and the secret, which ends in a checksum. Treat the whole value as opaque and send it unchanged. The prefix makes the mode visible wherever the key appears, but it is checked against the key's own mode rather than trusted. A mismatched prefix, an unprefixed value, a value whose checksum does not match, and a revoked or expired key are all refused with `unauthorized`. See [Sandbox](https://docs.koil.co/sandbox) for what a sandbox key can do.

A key can also carry permissions beyond its mode, chosen when you create it. Today there is one: `credentials:export`, which [exports a Connection's platform grant](https://docs.koil.co/byo-apps#taking-your-grants-with-you). A request that needs a permission its key lacks is refused with `permission_denied` (403).

If Koil cannot check a key at all, the request answers `503` with `service_unavailable` and a `Retry-After` header. Nothing was attempted and your key was not judged, so retry the same request after that many seconds.

## Organizations

Everything Koil stores — Apps, Connections, Profiles, publish requests, media, Event Destinations — belongs to the key's Organization and mode. A request can never read or act on another Organization's resources, and live and sandbox resources never mix.

## Profiles, not credentials

Platform operations never take a platform token. They take a `profileId`, and Koil resolves everything else:

1.  The Profile, scoped to your Organization.
2.  The Connection it was created from, and that Connection's current platform credential.
3.  Whether that credential can run this operation: its type (the `credentials[].type` a collection lists), its scopes, the platform profile it reaches, and its health.

If any check fails — the credential expired, lacks a scope, or no longer reaches the profile — the request fails before any platform call, with `connection_invalid` or `scope_insufficient`. A scope Koil cannot confirm the credential holds counts as missing. A Profile of another platform than the operation's (an Instagram profile on a Facebook route, say) is refused with `validation_error`, even when both were authorized through the same Facebook Login.

Two end users who authorize the same platform account produce two Profiles. Choose the one whose user (or automation) is taking the action, so the platform's own audit trail names the right actor. Koil never picks a credential for you. Each of those Profiles also receives its own events, so an item on that account is delivered once for each ([Receiving deliveries](https://docs.koil.co/guides/subscribe-to-events#receiving-deliveries)).

## Keyless routes

Platform discovery (`GET /v1/platforms`, `GET /v1/platforms/{platformId}`) and the collection catalog (`GET /v1/collections`, `GET /v1/collections/{collection}`) need no key, and no `Koil-Version`: they describe what Koil supports, not anything of yours.
