# Key

> Keys: one of your account's own keys makes another for your code, no wider than itself.

## Key

```typescript
class Key
```

A platform secret key of your account's: its `id`, `name`, `scopes`, monthly cap, expiry, the user it's pinned to
(`userId`) and the key that made it (`mintedBy`; `null` for one the owner made). `secret` is the key itself
(`nk_live_...`): `Key.create` returns it once, and nothing shows it again.

| Attribute | Type | Description |
| --- | --- | --- |
| `id` | `string` |  |
| `name` | `string` |  |
| `scopes` | `string[]` |  |
| `spendCapUsdPerMonth` | `number | null` |  |
| `expiresAt` | `Date | null` |  |
| `userId` | `string | null` |  |
| `mintedBy` | `string | null` |  |
| `secret` | `string | null` |  |
| `raw` | `JsonObject` | The response JSON exactly as the API sent it. |

Types:

```typescript
type JsonObject = { [key: string]: unknown };
```

### Key.create

```typescript
static create(
  name: string,
  scopes: string[],
  options?: KeyOptions,
): Promise<Key>
```

Make a secret key from your server, with one of your account's own keys that names the `keys:create` scope (a key
with every scope, `*`, holds it only when it's named too). The new key is no wider than the one making it: only
scopes it holds itself (`*` only from a key holding `*`), a monthly cap when it has one, at most its own, an
expiry when it has one, no later, and the approval settings it set for itself. It names the key that made it
(`mintedBy`) and never outlives it: revoking a key revokes every key it made, and theirs. A key without
`keys:create`, one acting for a user, or a key wider than the one making it rejects with `PermissionDenied`
(its `field` names what). Signed in, the account's owner makes any key.

    const worker = await Key.create("queue worker", ["queues", "agents"], { spendCapUsdPerMonth: 20 });
    process.env.WORKER_KEY = worker.secret!;

Parameters:

- `name` (`string`, required)
- `scopes` (`string[]`, required)
- `options.spendCapUsdPerMonth` (`number`): A monthly cap: required (at most its own) when the key making it has one.
- `options.expiresAt` (`Date`): When it expires: required (no later than its own) when the key making it expires.
- `options.userId` (`string`): Pins it to one of your users: it acts only for them.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: ```python
The key, its ``secret`` shown this once: store it where your code reads it.
```

Raises:
noclick.PermissionDenied: a key without `keys:create`, one acting for a user, or a key wider than the
one making it (`field` names what: `scopes`, `spend_cap_usd_per_month`, `expires_at`).
