# Declare the credential

The credential declares how a person connects an account at the service; the gateway runs the flow, stores the result encrypted and hands the secret to `execute` for the duration of one call. A plugin writes no route, never reads the environment and never sees an OAuth client secret.

## One way, the simplest the service offers

A plugin declares one way of connecting: a key a person makes by hand whenever the service has one, OAuth only where it has none.

| Kind      | When                                         | You declare                                                                                     | `execute` receives                                               |
| --------- | -------------------------------------------- | ----------------------------------------------------------------------------------------------- | ---------------------------------------------------------------- |
| `api-key` | The person pastes a key or token.            | `fields` (at least one `secret: true`), `label`, `createUrl`, `guide`, `verify`                 | `fields`, `metadata`                                             |
| `oauth2`  | The person consents on the service's page.   | `authorizationUrl`, `tokenUrl`, `clientRegistration`, `scopes`, `pkce`… — or `broker`, `scopes` | `accessToken`, `refreshToken`, `expiresAt`, `scopes`, `metadata` |
| `ssh-key` | The gateway generates a key pair for a host. | `algorithm: 'ed25519'`, `fields` (host, port, user)                                             | `privateKeyPem`, `publicKey`, `fields`, `metadata`               |

GitHub takes a fine-grained personal access token, Supabase a personal access token, Notion an internal account token: all `api-key`. Gmail offers no API key, but its IMAP door takes an app password a person makes by hand: the Gmail plugin is an `api-key` way over IMAP. How each one reads to a person: the "Connecting an account" section of its page, such as [GitHub](/plugins/github#connect).

## A pasted key: the guide is part of the contract

`definePlugin` refuses an `api-key` way without these parts:

| Part                | What it says                                                                                                                                                                                                                                                                                    |
| ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `label`             | What the person creates, in the service's words, lower case (`fine-grained personal access token`).                                                                                                                                                                                             |
| `createUrl`         | The token page itself, not the dashboard: one visit.                                                                                                                                                                                                                                            |
| `guide.steps`       | One imperative sentence each (`do`), in order, the last one at the paste. The menus to follow go in `path`, in the service's own labels (1 to 8, 40 characters each at most), never repeated in the sentence; a page in `url`, a command in `code`.                                             |
| `guide.screenshots` | Optional: one picture of the hard screen, in the plugin's `docs/connect/` (PNG or WebP, 300 KB and 1600 px wide at most), named by one step's `screenshot`, with its `source`, `licence` (`CC-BY-4.0`, `MIT`, `Apache-2.0` or `ours`), `taken` and `alt`. No email, avatar or real token in it. |
| `guide.reach`       | What the token reaches, in the service's words; the **widest** grant there; why each mission narrows it.                                                                                                                                                                                        |
| `guide.permissions` | The permissions to tick, as the service's page names them, each with the tools it serves; empty when the token carries no choice.                                                                                                                                                               |
| `guide.expiry`      | `service-reports` (`verify` reads it), `person-types` with `recommendedDays`, or `none` with a reason (how the person ends the token).                                                                                                                                                          |
| `guide.docsUrl`     | The service's documentation of this kind of token.                                                                                                                                                                                                                                              |
| `verify(ctx)`       | Asked before anything is stored: who the token acts as and, when the service says it, its expiry. A cause you can name is a `CredentialRefusal` in your words; a rejected token an `UpstreamAuthError`.                                                                                         |

**Ask for the widest grant, and say why.** The person grants once, and the gateway narrows what each mission may do, call by call; a guide that asks for the narrowest permissions sends the person back to the service each time a mission needs a little more. Where the key itself can be wide or confined (`alternatives`, two to four shapes: a whole team, or one project), list the narrowest one that fits the missions first, and say in `reach.why` that a key per project keeps a leak or a mistake inside that project. Never ask the person to create an integration, an OAuth app or a service account when a key pasted once does the job. Write the guide from the service's documentation, never from memory.

## OAuth

- **The gateway's own client.** Only for a service that offers no key a person can make **and** accepts dynamic client registration: declare `clientRegistration.registrationUrl`, and the gateway registers its own client there the first time someone connects. Nobody declares an OAuth client by hand.
- **An OAuth broker.** A service with neither a key nor dynamic registration — Google Calendar is the case — takes its client from an OAuth broker the instance is enrolled with: declare `broker` with its `provider`, and the `scopes`, nothing else.
- Set `refreshable: true` when the token endpoint issues refresh tokens: the gateway performs the standard refresh itself. Write `refresh()` only when the service's refresh is not the standard one.

## Check

```bash
npm run check
```

The kit's credential test holds the guide to its parts; `definePlugin` names a missing one. Next: [discover resources and execute calls](/docs/plugins-guide/execute).
