# Key

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

## noclick.Key

```python
class Key
```

A platform secret key of your account's: its `id`, `name`, `scopes`, monthly cap, expiry, the user it's
pinned to (`user_id`) and the key that made it (`minted_by`; `None` 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` | `str` |  |
| `name` | `str` |  |
| `scopes` | `list[str]` |  |
| `spend_cap_usd_per_month` | `float | None` |  |
| `expires_at` | `datetime | None` |  |
| `user_id` | `str | None` |  |
| `minted_by` | `str | None` |  |
| `secret` | `str | None` |  |
| `raw` | `dict[str, Any]` | The response JSON exactly as the API sent it. |

### Key.create

```python
noclick.Key.create(
    name, scopes, *, spend_cap_usd_per_month=None, expires_at=None, user_id=None,
    client=None
) -> 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 (`minted_by`) and never outlives it: revoking a key revokes every key it made,
and theirs. `user_id` pins it to one of your users. Signed in, the account's owner makes any key.

Parameters:

- `name` (`str`, required)
- `scopes` (`Sequence[str]`, required)
- `spend_cap_usd_per_month` (`float | None`, default `None`)
- `expires_at` (`datetime | None`, default `None`)
- `user_id` (`str | None`, default `None`)
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `Key` ```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`).

Usage:

```python
worker = noclick.Key.create("queue worker", ["queues", "agents"], spend_cap_usd_per_month=20)
os.environ["WORKER_KEY"] = worker.secret
```
