# Get API credentials

> Create an integration in the console: its key pair, its credentials file, its scopes, and how to replace a key or revoke it.

An API credential is called an integration: one system of yours that calls the API, such as a
billing backend or a data warehouse. It proves who it is with a key pair. Your system keeps the
private key, and YOU(th) stores only the public key. There is no client secret.

## Who creates it

An administrator of your organization, in the YOU(th) console, in your organization's developer
settings. Your [sandbox](/docs/sandbox) has integrations of its own, created the same way there.

## Create an integration

1. **Name it** after the system that uses it, such as "Billing backend".
2. **Choose its scopes**: see [Scopes](#scopes).
3. **Choose where its key pair is made:**

| Choice                         | What happens                                                                                                |
| ------------------------------ | ----------------------------------------------------------------------------------------------------------- |
| The console generates the pair | Your browser makes the key pair, and you download a credentials file, once. YOU(th) receives the public key |
| You upload a public key        | You make the key pair on your own systems and upload its public key. The private key never leaves your side |

Either way, the private key never reaches YOU(th). For a key pair of your own, use an `ES256` key
(ECDSA on the P-256 curve); `RS256` works too, with an RSA key of 2048 bits or more.

## The credentials file

When the console generates the key pair, it offers a credentials file with the four values your
OAuth library needs to [get a token](/docs/authentication#get-a-token):

- the token URL;
- the client id;
- the key id;
- the private key, as PEM (PKCS 8).

You can download it **once**. The console cannot show the private key again, because YOU(th) does
not have it. Put the file's contents in your server's secret store at once, and keep it out of
apps, browsers, repositories and logs. If you lose it, [replace the key](#replace-a-key).

When you upload your own public key, there is no file: the console shows the integration's client
id and the key id, and the token URL is the one on [Authentication](/docs/authentication).

## Scopes

A scope is what an integration may do. Give each integration only the scopes its job needs.

| Scope                | Lets the integration                          |
| -------------------- | --------------------------------------------- |
| `access_codes:read`  | Read batches and codes                        |
| `access_codes:write` | Order batches, block a code, end an access    |
| `webhooks:write`     | Create, read and disable webhook endpoints    |
| `checkups:read`      | Read checkups: health data                    |
| `patients:read`      | Read members, as FHIR `Patient`s: health data |

- `access_codes:write` also reads batches and codes.
- Webhook endpoints have one scope: reading them takes `webhooks:write` too.
- A token request names the scopes it wants, of those the integration was given: see
  [Get a token](/docs/authentication#get-a-token).

### Health data

`checkups:read` and `patients:read` give access to health data, and have rules of their own:

- **Only a Doctor gives them.** Being an administrator is not enough: one of your organization's
  Doctors gives an integration these scopes, and they last as long as that Doctor's own access to
  health data does.
- **Only with consent.** They cover the members who consented to share their data with your
  organization, and no one else.
- **Every read is logged**, before it is answered.

> **Preview:** the routes these two scopes read are not available yet: see
> [Catch up after an outage](/docs/webhooks/catch-up#the-read-api).

## Check your credentials

[Get a token](/docs/authentication#get-a-token), then
[ask which organization and integration it belongs to](/docs/api#operation-get_current_organization).
The route needs no particular scope:

```bash
curl https://api.youth-platform.io/v1/organization \
  --header "Authorization: Bearer $YOUTH_ACCESS_TOKEN"
```

```json
{
  "organization": { "alias": "example-mobile", "name": "Example Mobile" },
  "integration": {
    "id": "0193b1a0-52c4-7b1e-a2d3-4f5a6b7c8d9e",
    "name": "Billing backend",
    "scopes": ["access_codes:read", "access_codes:write"]
  }
}
```

`scopes` holds the scopes of the token you sent. The organization always comes from the
integration: no route takes one in its address or its body.

## Replace a key

A new key belongs to the same integration, so its client id and scopes stay:

1. Add a second key to the integration, generated in the console or uploaded.
2. Move your servers to the new key.
3. Remove the old key. YOU(th) refuses it from then on.

An integration holds two keys at most, which is what a replacement needs. Replace a key at once
when it is lost or may have leaked.

## Revoke an integration

Revoke an integration when its system is retired, or when you cannot replace a leaked key fast
enough. The API refuses it from the next request with `401` and `integration_revoked`, even with a
token that has not expired yet. A revoked integration does not come back: create a new one.

## Several integrations

An organization can have several integrations, one per system, each with its own key and scopes:
five active at most. Revoking one leaves the others untouched, and frees its place.
