---
title: "nextmail.keys"
description: "Every method in this namespace: its signature, its parameters, what it returns and an example."
url: "https://nextmail.sh/docs/python/reference/keys"
area: "Python"
category: "Reference"
---

# nextmail.keys

Every method in this namespace: its signature, its parameters, what it returns and an example.

## Methods

Every key in the workspace: list them, read their request log and activity, create, change, switch off, rotate, revoke and delete them, never wider than the calling key, and rotate the calling key's own secret. `workspace_id=`, or `workspaceId` in the body of `create`, reaches the keys of another workspace the account owns. Changes take an API key that holds `keys:manage`, so an OAuth access token is refused them with 403 `api_key_only`.

### `keys.list()`

List one page of the workspace's API keys

```python
def list(
    *,
    limit: int | None = None,
    cursor: str | None = None,
    q: str | None = None,
    workspace_id: str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> Page[ApiKeyResource]
```

Returns one page of the workspace's keys, newest first, with what the Settings, API keys page shows: status, scopes, role, send scope, when each was last used, how often, and who made and last changed it. Revoked and expired keys stay listed until somebody deletes them. No secret is ever returned; `maskedKey` is enough to tell two keys apart.

A key narrowed to some domains or addresses only sees the keys whose send scope sits inside its own, so any other is a 404 rather than a refusal. Over OAuth only the workspace owner reaches it, and a member's token is 403 `owner_only`.

`workspace_id=` lists the keys of another workspace the account owns, for an API key with no role and no address or domain limit. An OAuth access token that names one is 403 `api_key_only`.

Scopes: `keys:read`.

**Parameters**

- `limit` (`int`): Rows per page, a whole number from 1 to 100. The server defaults to 25.
- `cursor` (`str`): The `nextCursor` from the previous page, passed back unchanged. Never build one yourself.
- `q` (`str`): Narrows to keys whose name, masked key, id, scopes, or one of the addresses, domains and IP addresses they are limited to matches, up to 200 characters. Every word has to appear in one of them, ignoring case and accents, and when nothing matches exactly the search allows for a typo instead.
- `workspace_id` (`str`): Another workspace whose keys to reach, by the id `workspaces.list` returns, such as one `workspaces.create` just made. The account the key acts for has to own it, and the calling key needs no role and no address or domain limit, or the call is 403 `beyond_caller_authority`. Any other workspace is a 404. Left out, the calling key's own workspace.
- `api_key` (`str`): Overrides the client API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`Page[ApiKeyResource]`, a dict with `items`, `hasMore` and `nextCursor`. Each item is an `ApiKeyResource`: `id`, `name`, `mode`, `maskedKey`, `keyLast4`, `status`, `scopes`, `roleId`, `roleName`, `addressAllowlist`, `domainAllowlist`, `ipAllowlist`, `expiresAt`, `lastUsedAt`, `totalUses`, `rotatedAt`, `rotationCount`, `deactivatedAt`, `revokedAt`, `revokedReason`, `createdAt`, `createdBy`, `updatedAt`, `updatedBy`, `lastChangeAt` and `lastChangeType`. Never a secret.

**Example**

```python
from nextmail import nextmail

page = nextmail.keys.list()

for key in page['items']:
    print(key['maskedKey'], key['name'], key['status'], key['lastUsedAt'])
```

**Notes**

- It needs `keys:read`, which no key holds unless somebody gave it one.
- The cursor is opaque: pass `nextCursor` back as it came.

Also available in: API [`GET /keys`](https://nextmail.sh/docs/api/reference/api-keys#get-keys); TypeScript [`keys.list()`](https://nextmail.sh/docs/sdk/reference/keys#list); Ruby [`keys.list`](https://nextmail.sh/docs/ruby/reference/keys#list); PHP [`keys->list`](https://nextmail.sh/docs/php/reference/keys#list); Go [`Keys.List`](https://nextmail.sh/docs/go/reference/keys#list); Java [`keys().list`](https://nextmail.sh/docs/java/reference/keys#list); C# [`Keys.ListAsync`](https://nextmail.sh/docs/csharp/reference/keys#list); CLI [`nextmail keys list`](https://nextmail.sh/docs/cli/reference/keys#keys-list).

### `keys.list_all()`

Collect every API key into one list

```python
def list_all(
    *,
    limit: int | None = None,
    cursor: str | None = None,
    q: str | None = None,
    workspace_id: str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> builtins.list[ApiKeyResource]
```

Walks every page of `list` and returns every key the caller can see, newest first, in one list.

Scopes: `keys:read`.

**Parameters**

- `limit` (`int`): Page size for each request, 1 to 100. The server defaults to 25.
- `cursor` (`str`): Starts the walk from this cursor instead of the newest row.
- `q` (`str`): Narrows to keys whose name, masked key, id, scopes, or one of the addresses, domains and IP addresses they are limited to matches, up to 200 characters. Every word has to appear in one of them, ignoring case and accents, and when nothing matches exactly the search allows for a typo instead.
- `workspace_id` (`str`): Another workspace whose keys to reach, by the id `workspaces.list` returns, such as one `workspaces.create` just made. The account the key acts for has to own it, and the calling key needs no role and no address or domain limit, or the call is 403 `beyond_caller_authority`. Any other workspace is a 404. Left out, the calling key's own workspace.
- `api_key` (`str`): Overrides the client API key for every page of this walk.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`list[ApiKeyResource]`, newest first.

**Example**

```python
from nextmail import nextmail

keys = nextmail.keys.list_all()

idle = [key['name'] for key in keys if key['status'] == 'active' and key['lastUsedAt'] is None]

print(idle)
```

**Notes**

- If any page fails, the call raises and the keys already fetched are discarded.

Also available in: API [`GET /keys`](https://nextmail.sh/docs/api/reference/api-keys#get-keys); TypeScript [`keys.listAll()`](https://nextmail.sh/docs/sdk/reference/keys#listAll); Ruby [`keys.list_all`](https://nextmail.sh/docs/ruby/reference/keys#listAll); PHP [`keys->listAll`](https://nextmail.sh/docs/php/reference/keys#listAll); Go [`Keys.ListAll`](https://nextmail.sh/docs/go/reference/keys#listAll); Java [`keys().listAll`](https://nextmail.sh/docs/java/reference/keys#listAll); C# [`Keys.ListAllAsync`](https://nextmail.sh/docs/csharp/reference/keys#listAll).

### `keys.iterate()`

Stream the workspace's API keys one at a time

```python
def iterate(
    *,
    limit: int | None = None,
    cursor: str | None = None,
    q: str | None = None,
    workspace_id: str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> Iterator[ApiKeyResource]
```

A generator over `list`, fetching a page only when the one before is drained. Nothing is requested until you start iterating, and breaking out of the loop stops the requests.

Scopes: `keys:read`.

**Parameters**

- `limit` (`int`): Page size per request, 1 to 100. The server defaults to 25.
- `cursor` (`str`): Starts the walk from this cursor instead of the newest row.
- `q` (`str`): Narrows to keys whose name, masked key, id, scopes, or one of the addresses, domains and IP addresses they are limited to matches, up to 200 characters. Every word has to appear in one of them, ignoring case and accents, and when nothing matches exactly the search allows for a typo instead. Sent with every page.
- `workspace_id` (`str`): Another workspace whose keys to reach, by the id `workspaces.list` returns, such as one `workspaces.create` just made. The account the key acts for has to own it, and the calling key needs no role and no address or domain limit, or the call is 403 `beyond_caller_authority`. Any other workspace is a 404. Left out, the calling key's own workspace.
- `api_key` (`str`): Overrides the client API key for every page of this walk.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`Iterator[ApiKeyResource]`, a generator yielding one key per step.

**Example**

```python
from datetime import datetime, timedelta, timezone

from nextmail import nextmail

soon = datetime.now(timezone.utc) + timedelta(days=7)

for key in nextmail.keys.iterate():
    expires = key['expiresAt']

    if expires and datetime.fromisoformat(expires.replace('Z', '+00:00')) < soon:
        print(key['name'], 'expires soon')
```

Also available in: API [`GET /keys`](https://nextmail.sh/docs/api/reference/api-keys#get-keys); TypeScript [`keys.iterate()`](https://nextmail.sh/docs/sdk/reference/keys#iterate); Ruby [`keys.iterate`](https://nextmail.sh/docs/ruby/reference/keys#iterate); PHP [`keys->iterate`](https://nextmail.sh/docs/php/reference/keys#iterate); Go [`Keys.Iterate`](https://nextmail.sh/docs/go/reference/keys#iterate); Java [`keys().iterate`](https://nextmail.sh/docs/java/reference/keys#iterate); C# [`Keys.IterateAsync`](https://nextmail.sh/docs/csharp/reference/keys#iterate).

### `keys.get()`

Read one API key, without its secret

```python
def get(
    id: str,
    *,
    workspace_id: str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> ApiKeyResource
```

Returns one key as `list` shows it. A key narrowed to some domains or addresses only sees the keys whose send scope sits inside its own, so any other is a 404 rather than a refusal, which `is_not_found` reports on the error. Over OAuth only the workspace owner reaches it, and a member's token is 403 `owner_only`.

`nextmail.me.get()` describes the calling key itself and needs no scope; this reads any key the caller can see. `workspace_id=` reads a key of another workspace the account owns.

Scopes: `keys:read`.

**Parameters**

- `id` (`str`, required): Key id, the 24 hex characters after `nm_live_`.
- `workspace_id` (`str`): Another workspace whose keys to reach, by the id `workspaces.list` returns, such as one `workspaces.create` just made. The account the key acts for has to own it, and the calling key needs no role and no address or domain limit, or the call is 403 `beyond_caller_authority`. Any other workspace is a 404. Left out, the calling key's own workspace.
- `api_key` (`str`): Overrides the client API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`ApiKeyResource`: `id`, `name`, `mode`, `maskedKey`, `keyLast4`, `status`, `scopes`, `roleId`, `roleName`, `addressAllowlist`, `domainAllowlist`, `ipAllowlist`, `expiresAt`, `lastUsedAt`, `totalUses`, `rotatedAt`, `rotationCount`, `deactivatedAt`, `revokedAt`, `revokedReason`, `createdAt`, `createdBy`, `updatedAt`, `updatedBy`, `lastChangeAt` and `lastChangeType`. Never a secret.

**Example**

```python
from nextmail import NextmailApiError, nextmail

try:
    key = nextmail.keys.get('4c1b257a66287fd113bd89d0')
except NextmailApiError as error:
    if not error.is_not_found:
        raise

    print('No such key, or one this key cannot see')
else:
    print(key['status'], key['scopes'], key['domainAllowlist'])
```

Also available in: API [`GET /keys/{id}`](https://nextmail.sh/docs/api/reference/api-keys#get-keys-id); TypeScript [`keys.get()`](https://nextmail.sh/docs/sdk/reference/keys#get); Ruby [`keys.get`](https://nextmail.sh/docs/ruby/reference/keys#get); PHP [`keys->get`](https://nextmail.sh/docs/php/reference/keys#get); Go [`Keys.Get`](https://nextmail.sh/docs/go/reference/keys#get); Java [`keys().get`](https://nextmail.sh/docs/java/reference/keys#get); C# [`Keys.GetAsync`](https://nextmail.sh/docs/csharp/reference/keys#get); CLI [`nextmail keys get`](https://nextmail.sh/docs/cli/reference/keys#keys-get).

### `keys.create()`

Mint a new API key and receive its secret once

```python
def create(
    body: ApiKeyCreate,
    *,
    idempotency_key: str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> CreatedApiKeyResource
```

Creates a key and returns it plus `token`, the whole secret. That is the only time it appears, so store it before doing anything else.

Left out, `scopes` is `['emails:send']`, the role is the caller's own or none, the send scope is the caller's own or none (none means every address the workspace owns), the IP allowlist is the caller's own or none, and the expiry is the caller's own or none. The key gets the caller's mode. The workspace cap on live keys applies, as a 422 `workspace_limit_reached`.

It takes an API key that holds `keys:manage`, and the new key never reaches further than the calling key: anything wider is 403 `beyond_caller_authority`, and the error's `param` names the axis. An OAuth access token is refused with 403 `api_key_only`, because no app is ever given `keys:manage`.

`workspaceId` in the body creates the key in another workspace the account owns, which is how a program that has just made a workspace with `workspaces.create` gives it a key of its own.

Pass `idempotency_key=` to make a retry safe. A retry with the same key and the same body returns the key the first call made, with `replayed` set to `True`, and mints nothing. The secret is never shown again, so `token` is `None` on a replay: rotate the key if the first answer was lost. The same key with a different body is a 422 `idempotency_key_reuse`.

Scopes: `keys:manage`.

**Parameters**

- `body['name']` (`str`, required): A name, 1 to 60 characters.
- `body['scopes']` (`list[ApiScope]`): The scopes the key holds, at least one, each held by the caller. Defaults to `['emails:send']`.
- `body['roleId']` (`str | None`): A role to cap the key. Left out, the caller's own role. A caller with a role can only give its own, and `None` is refused for it.
- `body['addressAllowlist']` (`list[str]`): Single addresses the key may send as, at most 50, each owned by the workspace.
- `body['domainAllowlist']` (`list[str]`): Whole domains the key may send as, at most 25, including addresses added to them later. Leave both lists out to inherit the caller's own; send both empty for none.
- `body['expiresInMinutes']` (`int`): Minutes until the key expires, 5 to 5,256,000 (ten years). Left out, the caller's own expiry.
- `body['ipAllowlist']` (`list[str]`): Up to 50 IP addresses and ranges the key works from, such as `203.0.113.7`, `198.51.100.0/24` or `2001:db8::/32`. A call or an SMTP sign-in from anywhere else is refused with 403 `ip_not_allowed`. Left out, the caller's own list, or none. A caller with a list can only give a list inside it.
- `body['workspaceId']` (`str`): Creates the key in another workspace the account owns, by the id `workspaces.list` returns, such as one `workspaces.create` just made. The calling key needs no role and no address or domain limit, or the call is 403 `beyond_caller_authority`, and any other workspace is a 404. `roleId` then names a role of that workspace. Left out, the calling key's own workspace.
- `idempotency_key` (`str`): Makes a retry safe: the same key with the same body returns the key the first call made, with `replayed` set to `True` and `token` set to `None`, instead of minting another. 1 to 255 letters, digits, `_`, `.`, `:` or `-`, and the same key with a different body is a 422 `idempotency_key_reuse`.
- `api_key` (`str`): Overrides the client API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`CreatedApiKeyResource`: the `ApiKeyResource` fields `id`, `name`, `mode`, `maskedKey`, `keyLast4`, `status`, `scopes`, `roleId`, `roleName`, `addressAllowlist`, `domainAllowlist`, `ipAllowlist`, `expiresAt`, `lastUsedAt`, `totalUses`, `rotatedAt`, `rotationCount`, `deactivatedAt`, `revokedAt`, `revokedReason`, `createdAt`, `createdBy`, `updatedAt`, `updatedBy`, `lastChangeAt` and `lastChangeType`, plus `token`, `nm_live_` followed by the id, an underscore and the secret, or `None` on a replay, and `replayed`.

**Example**

```python
from nextmail import nextmail

workspace = nextmail.workspaces.create({'name': 'Acme Support'}, idempotency_key='signup-4192')

key = nextmail.keys.create(
    {
        'name': 'Acme Support',
        'scopes': ['emails:send', 'emails:read'],
        'workspaceId': workspace['id'],
    },
    idempotency_key='signup-4192-key',
)

token = key['token']

if token is None:
    token = nextmail.keys.rotate(key['id'], workspace_id=workspace['id'])['token']

print('Store this secret now, it is never shown again:', token)
```

**Notes**

- Retried on network failure and retryable statuses only when `idempotency_key=` is given, because a retry can then only replay the first key. Without one it is not retried, since a retry after a lost response would mint a second key.
- The success status is 201, and 200 on a replay.

Also available in: API [`POST /keys`](https://nextmail.sh/docs/api/reference/api-keys#post-keys); TypeScript [`keys.create()`](https://nextmail.sh/docs/sdk/reference/keys#create); Ruby [`keys.create`](https://nextmail.sh/docs/ruby/reference/keys#create); PHP [`keys->create`](https://nextmail.sh/docs/php/reference/keys#create); Go [`Keys.Create`](https://nextmail.sh/docs/go/reference/keys#create); Java [`keys().create`](https://nextmail.sh/docs/java/reference/keys#create); C# [`Keys.CreateAsync`](https://nextmail.sh/docs/csharp/reference/keys#create); CLI [`nextmail keys create`](https://nextmail.sh/docs/cli/reference/keys#keys-create).

### `keys.update()`

Rename a key, change its scopes or send scope, or switch it off and on

```python
def update(
    id: str,
    patch: ApiKeyPatch,
    *,
    workspace_id: str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> ApiKeyResource
```

Applies a partial change and returns the key as it now stands. `scopes`, `addressAllowlist` and `domainAllowlist` REPLACE what the key had, and a field left out stays as it was. `'enabled': False` switches the key off: every call with it is refused with `inactive_api_key` and it keeps its secret, scopes, role and send scope, so `'enabled': True` restores it exactly. That is the reversible alternative to `revoke`. A revoked key cannot be changed, and answers 409 `revoked`.

It takes an API key that holds `keys:manage`, and an OAuth access token is refused with 403 `api_key_only`, because no app is ever given `keys:manage`. A key may rename itself, switch itself off and change its own scopes and send scope within what it holds, but never its own IP allowlist. Any other key has to sit inside the calling key before the change and after it, or the call is 403 `beyond_caller_authority`. `workspace_id=` reaches a key of another workspace the account owns.

Scopes: `keys:manage`.

**Parameters**

- `id` (`str`, required): Key id, the 24 hex characters after `nm_live_`.
- `patch['name']` (`str`): A new name, 1 to 60 characters.
- `patch['scopes']` (`list[ApiScope]`): The whole new list of scopes, at least one.
- `patch['addressAllowlist']` (`list[str]`): The whole new list of single addresses.
- `patch['domainAllowlist']` (`list[str]`): The whole new list of whole domains.
- `patch['enabled']` (`bool`): `False` switches the key off, `True` switches it back on.
- `patch['ipAllowlist']` (`list[str]`): The whole new list of IP addresses and ranges the key works from, at most 50. An empty list lets it work from anywhere again. A key cannot change its own list, and a caller with a list can only set one inside it.
- `workspace_id` (`str`): Another workspace whose keys to reach, by the id `workspaces.list` returns, such as one `workspaces.create` just made. The account the key acts for has to own it, and the calling key needs no role and no address or domain limit, or the call is 403 `beyond_caller_authority`. Any other workspace is a 404. Left out, the calling key's own workspace.
- `api_key` (`str`): Overrides the client API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`ApiKeyResource`: `id`, `name`, `mode`, `maskedKey`, `keyLast4`, `status`, `scopes`, `roleId`, `roleName`, `addressAllowlist`, `domainAllowlist`, `ipAllowlist`, `expiresAt`, `lastUsedAt`, `totalUses`, `rotatedAt`, `rotationCount`, `deactivatedAt`, `revokedAt`, `revokedReason`, `createdAt`, `createdBy`, `updatedAt`, `updatedBy`, `lastChangeAt` and `lastChangeType`. Never a secret.

**Example**

```python
from nextmail import nextmail

nextmail.keys.update('4c1b257a66287fd113bd89d0', {'enabled': False})

key = nextmail.keys.update('4c1b257a66287fd113bd89d0', {'name': 'Billing sender (paused)'})

print(key['status'], key['deactivatedAt'])
```

**Notes**

- An empty patch is a 422. The call is retried on network failure and retryable statuses, because applying the same patch twice leaves the same key.

Also available in: API [`PATCH /keys/{id}`](https://nextmail.sh/docs/api/reference/api-keys#patch-keys-id); TypeScript [`keys.update()`](https://nextmail.sh/docs/sdk/reference/keys#update); Ruby [`keys.update`](https://nextmail.sh/docs/ruby/reference/keys#update); PHP [`keys->update`](https://nextmail.sh/docs/php/reference/keys#update); Go [`Keys.Update`](https://nextmail.sh/docs/go/reference/keys#update); Java [`keys().update`](https://nextmail.sh/docs/java/reference/keys#update); C# [`Keys.UpdateAsync`](https://nextmail.sh/docs/csharp/reference/keys#update); CLI [`nextmail keys update`](https://nextmail.sh/docs/cli/reference/keys#keys-update).

### `keys.delete()`

Remove a revoked key from the list

```python
def delete(
    id: str,
    *,
    workspace_id: str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> DeletedApiKeyResource
```

Deletes a key that has already been revoked. Its request log and activity stay, under `Deleted key`, so the history of what it did is not lost with it. A key that has not been revoked is refused with 409 `not_revoked`, so nothing still calling with it loses its credential without somebody deciding that first.

It takes an API key that holds `keys:manage`, and deletes only a key that sits inside the calling key. An OAuth access token is refused with 403 `api_key_only`, because no app is ever given `keys:manage`. `workspace_id=` reaches a key of another workspace the account owns.

Scopes: `keys:manage`.

**Parameters**

- `id` (`str`, required): Key id, the 24 hex characters after `nm_live_`.
- `workspace_id` (`str`): Another workspace whose keys to reach, by the id `workspaces.list` returns, such as one `workspaces.create` just made. The account the key acts for has to own it, and the calling key needs no role and no address or domain limit, or the call is 403 `beyond_caller_authority`. Any other workspace is a 404. Left out, the calling key's own workspace.
- `api_key` (`str`): Overrides the client API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`DeletedApiKeyResource`, a dict with `'object': 'api_key'`, the `id` and `'deleted': True`.

**Example**

```python
from nextmail import nextmail

revoked = nextmail.keys.revoke('4c1b257a66287fd113bd89d0', {'reason': 'Leaked in a build log'})

nextmail.keys.delete(revoked['id'])

print('Deleted', revoked['name'], 'after revoking it at', revoked['revokedAt'])
```

**Notes**

- Not retried automatically. If you repeat it yourself after a lost response, a 404 means the first attempt already worked.

Also available in: API [`DELETE /keys/{id}`](https://nextmail.sh/docs/api/reference/api-keys#delete-keys-id); TypeScript [`keys.delete()`](https://nextmail.sh/docs/sdk/reference/keys#delete); Ruby [`keys.delete`](https://nextmail.sh/docs/ruby/reference/keys#delete); PHP [`keys->delete`](https://nextmail.sh/docs/php/reference/keys#delete); Go [`Keys.Delete`](https://nextmail.sh/docs/go/reference/keys#delete); Java [`keys().delete`](https://nextmail.sh/docs/java/reference/keys#delete); C# [`Keys.DeleteAsync`](https://nextmail.sh/docs/csharp/reference/keys#delete); CLI [`nextmail keys delete`](https://nextmail.sh/docs/cli/reference/keys#keys-delete).

### `keys.rotate()`

Give a key a new secret and receive it once

```python
def rotate(
    id: str,
    *,
    workspace_id: str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> RotatedApiKeyResource
```

Mints a new secret for a key and returns the key plus `token`, `rotationCount` and `rotatedAt`. The id, name, scopes, role, send scope, expiry and request history all carry on; only the secret and `keyLast4` change. There is no overlap window: the old secret stops working the instant this returns.

An API key rotates itself with `keys:write` or `keys:manage`, as `nextmail.me.rotate()` does, and any other key that sits inside it with `keys:manage`. An OAuth access token is refused with 403 `api_key_only`, because no app is ever given `keys:write` or `keys:manage`. `workspace_id=` reaches a key of another workspace the account owns. A revoked or expired key cannot be rotated, and answers 409 `revoked` or `expired`.

Scopes: `keys:manage`.

**Parameters**

- `id` (`str`, required): Key id, the 24 hex characters after `nm_live_`.
- `workspace_id` (`str`): Another workspace whose keys to reach, by the id `workspaces.list` returns, such as one `workspaces.create` just made. The account the key acts for has to own it, and the calling key needs no role and no address or domain limit, or the call is 403 `beyond_caller_authority`. Any other workspace is a 404. Left out, the calling key's own workspace.
- `api_key` (`str`): Overrides the client API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`RotatedApiKeyResource`: the `ApiKeyResource` fields `id`, `name`, `mode`, `maskedKey`, `keyLast4`, `status`, `scopes`, `roleId`, `roleName`, `addressAllowlist`, `domainAllowlist`, `ipAllowlist`, `expiresAt`, `lastUsedAt`, `totalUses`, `rotatedAt`, `rotationCount`, `deactivatedAt`, `revokedAt`, `revokedReason`, `createdAt`, `createdBy`, `updatedAt`, `updatedBy`, `lastChangeAt` and `lastChangeType`, plus the new secret in `token`. `rotationCount` and `rotatedAt` already count this rotation.

**Example**

```python
from nextmail import nextmail

rotated = nextmail.keys.rotate('4c1b257a66287fd113bd89d0')

print(rotated['rotationCount'], rotated['rotatedAt'])
print('Store the new secret now, the old one has stopped working:', rotated['token'])
```

**Notes**

- Not retried automatically. Repeating a rotation would invalidate the secret the first attempt returned.

Also available in: API [`POST /keys/{id}/rotate`](https://nextmail.sh/docs/api/reference/api-keys#post-keys-id-rotate); TypeScript [`keys.rotate()`](https://nextmail.sh/docs/sdk/reference/keys#rotate); Ruby [`keys.rotate`](https://nextmail.sh/docs/ruby/reference/keys#rotate); PHP [`keys->rotate`](https://nextmail.sh/docs/php/reference/keys#rotate); Go [`Keys.Rotate`](https://nextmail.sh/docs/go/reference/keys#rotate); Java [`keys().rotate`](https://nextmail.sh/docs/java/reference/keys#rotate); C# [`Keys.RotateAsync`](https://nextmail.sh/docs/csharp/reference/keys#rotate); CLI [`nextmail keys rotate`](https://nextmail.sh/docs/cli/reference/keys#keys-rotate).

### `keys.revoke()`

Revoke a key for good

```python
def revoke(
    id: str,
    body: ApiKeyRevoke | None = None,
    *,
    workspace_id: str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> ApiKeyResource
```

Revokes a key: every later call with it is refused with `revoked_api_key`, and it can never be switched back on, rotated or changed. `reason` is kept on the key and in the activity log. Revoking a key that is already revoked changes nothing and returns it as it is. A key may revoke itself, which is how an integration that believes its secret leaked retires it at once.

A key never reaches a key wider than itself. A key it revokes has to sit inside it on every axis: scopes the caller holds after its own role has narrowed them, the same role when the caller has one, an expiry no later than the caller's when the caller expires, the caller's mode, an IP allowlist inside the caller's own when it has one, and a send scope inside the caller's own, where one address never covers its whole domain. Anything wider is 403 `beyond_caller_authority`, and the error's `param` names the axis.

A key narrowed to some domains or addresses only sees the keys whose send scope sits inside its own, so any other is a 404 rather than a refusal. An OAuth access token is refused with 403 `api_key_only`, because no app is ever given `keys:manage`. `workspace_id=` reaches a key of another workspace the account owns.

Scopes: `keys:manage`.

**Parameters**

- `id` (`str`, required): Key id, the 24 hex characters after `nm_live_`.
- `body['reason']` (`str`): Why, at most 200 characters. The body is optional.
- `workspace_id` (`str`): Another workspace whose keys to reach, by the id `workspaces.list` returns, such as one `workspaces.create` just made. The account the key acts for has to own it, and the calling key needs no role and no address or domain limit, or the call is 403 `beyond_caller_authority`. Any other workspace is a 404. Left out, the calling key's own workspace.
- `api_key` (`str`): Overrides the client API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`ApiKeyResource`: `id`, `name`, `mode`, `maskedKey`, `keyLast4`, `status`, `scopes`, `roleId`, `roleName`, `addressAllowlist`, `domainAllowlist`, `ipAllowlist`, `expiresAt`, `lastUsedAt`, `totalUses`, `rotatedAt`, `rotationCount`, `deactivatedAt`, `revokedAt`, `revokedReason`, `createdAt`, `createdBy`, `updatedAt`, `updatedBy`, `lastChangeAt` and `lastChangeType`. Never a secret. `status` is `'revoked'`.

**Example**

```python
from nextmail import nextmail

key = nextmail.keys.revoke('4c1b257a66287fd113bd89d0', {'reason': 'Contractor offboarded'})

print(key['status'], key['revokedAt'], key['revokedReason'])
```

**Notes**

- Retried on network failure and retryable statuses, because revoking twice leaves the same key. Prefer switching the key off with `keys.update` while you find out whether anything still depends on it, since that can be undone.

Also available in: API [`POST /keys/{id}/revoke`](https://nextmail.sh/docs/api/reference/api-keys#post-keys-id-revoke); TypeScript [`keys.revoke()`](https://nextmail.sh/docs/sdk/reference/keys#revoke); Ruby [`keys.revoke`](https://nextmail.sh/docs/ruby/reference/keys#revoke); PHP [`keys->revoke`](https://nextmail.sh/docs/php/reference/keys#revoke); Go [`Keys.Revoke`](https://nextmail.sh/docs/go/reference/keys#revoke); Java [`keys().revoke`](https://nextmail.sh/docs/java/reference/keys#revoke); C# [`Keys.RevokeAsync`](https://nextmail.sh/docs/csharp/reference/keys#revoke); CLI [`nextmail keys revoke`](https://nextmail.sh/docs/cli/reference/keys#keys-revoke).

### `keys.list_requests()`

List one page of one key's request log

```python
def list_requests(
    id: str,
    *,
    limit: int | None = None,
    cursor: str | None = None,
    since: datetime | str | None = None,
    until: datetime | str | None = None,
    failed_only: bool | None = None,
    statuses: Sequence[int] | None = None,
    path: str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> Page[ApiKeyRequestResource]
```

Returns one page of the calls one key made, newest first, the Requests tab of the key in the app. The request log records every authenticated call a key made: method, path, status, error code, duration, IP and user agent, and never a body or a query string. A call refused before a key could be identified is not in it, and neither is a call made with an OAuth access token. Nothing is pruned, so the log reaches back to a key's first call. `failed_only=`, `statuses=`, `path=`, `since=` and `until=` narrow it, and they combine.

A deleted key's log stays readable to a key that is not narrowed. A key narrowed to some domains or addresses only sees the keys whose send scope sits inside its own, so any other is a 404 rather than a refusal. Over OAuth only the workspace owner reaches it, and a member's token is 403 `owner_only`.

Scopes: `keys:read`.

**Parameters**

- `id` (`str`, required): Key id, the 24 hex characters after `nm_live_`.
- `limit` (`int`): Rows per page, a whole number from 1 to 100. The server defaults to 25.
- `cursor` (`str`): The `nextCursor` from the previous page, passed back unchanged. Never build one yourself.
- `since` (`datetime | str`): Only rows at or after this instant. A `datetime` is sent as ISO 8601 in UTC, and a string must already be one.
- `until` (`datetime | str`): Only rows before this instant. It has to be later than `since`, or the server answers 400 `invalid_parameter`.
- `failed_only` (`bool`): Only calls answered with a status of 400 or more.
- `statuses` (`Sequence[int]`): Only calls answered with one of these HTTP status codes, such as `[401, 403]`. At most 20, each from 100 to 599, sent comma-separated as `status`.
- `path` (`str`): Only calls to this route, whatever the method, such as `/emails` or `/emails/:id`. It matches the route, not the exact path: an id counts as `:id` and an email address as `:address`. End it with `*` to read every route that starts with it, such as `/emails/*`.
- `api_key` (`str`): Overrides the client API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`Page[ApiKeyRequestResource]`, a dict with `items`, `hasMore` and `nextCursor`. Each item has `id`, `keyId`, `keyName`, `requestId`, `method`, `path`, `status`, `errorCode`, `durationMs`, `ip`, `userAgent` and `createdAt`.

**Example**

```python
from nextmail import nextmail

page = nextmail.keys.list_requests('4c1b257a66287fd113bd89d0', failed_only=True, limit=50)

for call in page['items']:
    print(call['createdAt'], call['method'], call['path'], call['status'], call['errorCode'])
```

**Notes**

- The cursor is opaque and stays valid under the same filters. One this log did not hand out is a 400 `invalid_cursor`.
- `path=` matches the route rather than the exact path, because the log folds ids and addresses: every call to `/emails/<id>` is the one route `/emails/:id`, and passing a real id reads that whole route. Each row still carries the exact `path`, so filter the page yourself to follow one id.
- The key making the call reads its own log with no scope through `nextmail.me.list_requests()`.

Also available in: API [`GET /keys/{id}/requests`](https://nextmail.sh/docs/api/reference/api-keys#get-keys-id-requests); TypeScript [`keys.listRequests()`](https://nextmail.sh/docs/sdk/reference/keys#listRequests); Ruby [`keys.list_requests`](https://nextmail.sh/docs/ruby/reference/keys#listRequests); PHP [`keys->listRequests`](https://nextmail.sh/docs/php/reference/keys#listRequests); Go [`Keys.ListRequests`](https://nextmail.sh/docs/go/reference/keys#listRequests); Java [`keys().listRequests`](https://nextmail.sh/docs/java/reference/keys#listRequests); C# [`Keys.ListRequestsAsync`](https://nextmail.sh/docs/csharp/reference/keys#listRequests); CLI [`nextmail keys list-requests`](https://nextmail.sh/docs/cli/reference/keys#keys-list-requests).

### `keys.list_all_requests()`

Collect one key's whole request log into one list

```python
def list_all_requests(
    id: str,
    *,
    limit: int | None = None,
    cursor: str | None = None,
    since: datetime | str | None = None,
    until: datetime | str | None = None,
    failed_only: bool | None = None,
    statuses: Sequence[int] | None = None,
    path: str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> builtins.list[ApiKeyRequestResource]
```

Walks every page of `list_requests` under the same filters and returns every call in one list. The log is never pruned, so give it a window unless you mean to read a busy key's whole history, or use `iterate_requests` to stop early.

Scopes: `keys:read`.

**Parameters**

- `id` (`str`, required): Key id, the 24 hex characters after `nm_live_`.
- `limit` (`int`): Page size for each request, 1 to 100. The server defaults to 25.
- `cursor` (`str`): Starts the walk from this cursor instead of the newest row.
- `since` (`datetime | str`): Only rows at or after this instant. A `datetime` is sent as ISO 8601 in UTC, and a string must already be one.
- `until` (`datetime | str`): Only rows before this instant. It has to be later than `since`, or the server answers 400 `invalid_parameter`.
- `failed_only` (`bool`): Only calls answered with a status of 400 or more.
- `statuses` (`Sequence[int]`): Only calls answered with one of these HTTP status codes, such as `[401, 403]`. At most 20, each from 100 to 599, sent comma-separated as `status`.
- `path` (`str`): Only calls to this route, whatever the method, such as `/emails` or `/emails/:id`. It matches the route, not the exact path: an id counts as `:id` and an email address as `:address`. End it with `*` to read every route that starts with it, such as `/emails/*`.
- `api_key` (`str`): Overrides the client API key for every page of this walk.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`list[ApiKeyRequestResource]`, newest first.

**Example**

```python
from datetime import datetime, timedelta, timezone

from nextmail import nextmail

today = nextmail.keys.list_all_requests(
    '4c1b257a66287fd113bd89d0',
    since=datetime.now(timezone.utc) - timedelta(days=1),
    limit=100,
)

print(len(today), 'calls in the last 24 hours')
```

**Notes**

- If any page fails, the call raises and the rows already fetched are discarded.

Also available in: API [`GET /keys/{id}/requests`](https://nextmail.sh/docs/api/reference/api-keys#get-keys-id-requests); TypeScript [`keys.listAllRequests()`](https://nextmail.sh/docs/sdk/reference/keys#listAllRequests); Ruby [`keys.list_all_requests`](https://nextmail.sh/docs/ruby/reference/keys#listAllRequests); PHP [`keys->listAllRequests`](https://nextmail.sh/docs/php/reference/keys#listAllRequests); Go [`Keys.ListAllRequests`](https://nextmail.sh/docs/go/reference/keys#listAllRequests); Java [`keys().listAllRequests`](https://nextmail.sh/docs/java/reference/keys#listAllRequests); C# [`Keys.ListAllRequestsAsync`](https://nextmail.sh/docs/csharp/reference/keys#listAllRequests).

### `keys.iterate_requests()`

Stream one key's request log one call at a time

```python
def iterate_requests(
    id: str,
    *,
    limit: int | None = None,
    cursor: str | None = None,
    since: datetime | str | None = None,
    until: datetime | str | None = None,
    failed_only: bool | None = None,
    statuses: Sequence[int] | None = None,
    path: str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> Iterator[ApiKeyRequestResource]
```

A generator over `list_requests` under the same filters, fetching a page only when the one before is drained. Nothing is requested until you start iterating, and breaking out of the loop stops the requests.

Scopes: `keys:read`.

**Parameters**

- `id` (`str`, required): Key id, the 24 hex characters after `nm_live_`.
- `limit` (`int`): Page size per request, 1 to 100. The server defaults to 25.
- `cursor` (`str`): Starts the walk from this cursor instead of the newest row.
- `since` (`datetime | str`): Only rows at or after this instant. A `datetime` is sent as ISO 8601 in UTC, and a string must already be one.
- `until` (`datetime | str`): Only rows before this instant. It has to be later than `since`, or the server answers 400 `invalid_parameter`.
- `failed_only` (`bool`): Only calls answered with a status of 400 or more.
- `statuses` (`Sequence[int]`): Only calls answered with one of these HTTP status codes, such as `[401, 403]`. At most 20, each from 100 to 599, sent comma-separated as `status`.
- `path` (`str`): Only calls to this route, whatever the method, such as `/emails` or `/emails/:id`. It matches the route, not the exact path: an id counts as `:id` and an email address as `:address`. End it with `*` to read every route that starts with it, such as `/emails/*`.
- `api_key` (`str`): Overrides the client API key for every page of this walk.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`Iterator[ApiKeyRequestResource]`, a generator yielding one call per step.

**Example**

```python
from nextmail import nextmail

for call in nextmail.keys.iterate_requests(
    '4c1b257a66287fd113bd89d0', path='/emails', statuses=[403]
):
    if call['errorCode'] == 'from_address_forbidden':
        print('First refused send at', call['createdAt'])
        break
```

Also available in: API [`GET /keys/{id}/requests`](https://nextmail.sh/docs/api/reference/api-keys#get-keys-id-requests); TypeScript [`keys.iterateRequests()`](https://nextmail.sh/docs/sdk/reference/keys#iterateRequests); Ruby [`keys.iterate_requests`](https://nextmail.sh/docs/ruby/reference/keys#iterateRequests); PHP [`keys->iterateRequests`](https://nextmail.sh/docs/php/reference/keys#iterateRequests); Go [`Keys.IterateRequests`](https://nextmail.sh/docs/go/reference/keys#iterateRequests); Java [`keys().iterateRequests`](https://nextmail.sh/docs/java/reference/keys#iterateRequests); C# [`Keys.IterateRequestsAsync`](https://nextmail.sh/docs/csharp/reference/keys#iterateRequests).

### `keys.list_activity()`

List one page of what happened to one key

```python
def list_activity(
    id: str,
    *,
    limit: int | None = None,
    cursor: str | None = None,
    since: datetime | str | None = None,
    until: datetime | str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> Page[ApiKeyActivityResource]
```

Returns one page of one key's audit log, newest first, the Activity tab of the key in the app. Every change to a key is a row: `created`, `updated`, `rotated`, `deactivated`, `reactivated`, `revoked` and `deleted`, plus `auth_failed` for every call that presented the key and was refused. `actor` names who made the change, a person as `@username` or a key as `API key <name>` in `label`, and `detail['source']` says where it came from: `console`, `api`, `mcp` or `documentation`.

A deleted key keeps its history, readable to a key that is not narrowed. A key narrowed to some domains or addresses only sees the keys whose send scope sits inside its own, so any other is a 404 rather than a refusal. Over OAuth only the workspace owner reaches it, and a member's token is 403 `owner_only`.

Scopes: `keys:read`.

**Parameters**

- `id` (`str`, required): Key id, the 24 hex characters after `nm_live_`.
- `limit` (`int`): Rows per page, a whole number from 1 to 100. The server defaults to 25.
- `cursor` (`str`): The `nextCursor` from the previous page, passed back unchanged. Never build one yourself.
- `since` (`datetime | str`): Only rows at or after this instant. A `datetime` is sent as ISO 8601 in UTC, and a string must already be one.
- `until` (`datetime | str`): Only rows before this instant. It has to be later than `since`, or the server answers 400 `invalid_parameter`.
- `api_key` (`str`): Overrides the client API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`Page[ApiKeyActivityResource]`, a dict with `items`, `hasMore` and `nextCursor`. Each item has `id`, `keyId`, `keyName`, `type`, `createdAt`, `actor` and `detail`.

**Example**

```python
from nextmail import nextmail

page = nextmail.keys.list_activity('4c1b257a66287fd113bd89d0')

for change in page['items']:
    actor = change['actor']['label'] if change['actor'] else 'unknown'

    print(change['createdAt'], change['type'], actor, change['detail'].get('source'))
```

Also available in: API [`GET /keys/{id}/activity`](https://nextmail.sh/docs/api/reference/api-keys#get-keys-id-activity); TypeScript [`keys.listActivity()`](https://nextmail.sh/docs/sdk/reference/keys#listActivity); Ruby [`keys.list_activity`](https://nextmail.sh/docs/ruby/reference/keys#listActivity); PHP [`keys->listActivity`](https://nextmail.sh/docs/php/reference/keys#listActivity); Go [`Keys.ListActivity`](https://nextmail.sh/docs/go/reference/keys#listActivity); Java [`keys().listActivity`](https://nextmail.sh/docs/java/reference/keys#listActivity); C# [`Keys.ListActivityAsync`](https://nextmail.sh/docs/csharp/reference/keys#listActivity); CLI [`nextmail keys list-activity`](https://nextmail.sh/docs/cli/reference/keys#keys-list-activity).

### `keys.list_all_activity()`

Collect one key's whole audit log into one list

```python
def list_all_activity(
    id: str,
    *,
    limit: int | None = None,
    cursor: str | None = None,
    since: datetime | str | None = None,
    until: datetime | str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> builtins.list[ApiKeyActivityResource]
```

Walks every page of `list_activity` under the same window and returns every change in one list, newest first.

Scopes: `keys:read`.

**Parameters**

- `id` (`str`, required): Key id, the 24 hex characters after `nm_live_`.
- `limit` (`int`): Page size for each request, 1 to 100. The server defaults to 25.
- `cursor` (`str`): Starts the walk from this cursor instead of the newest row.
- `since` (`datetime | str`): Only rows at or after this instant. A `datetime` is sent as ISO 8601 in UTC, and a string must already be one.
- `until` (`datetime | str`): Only rows before this instant. It has to be later than `since`, or the server answers 400 `invalid_parameter`.
- `api_key` (`str`): Overrides the client API key for every page of this walk.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`list[ApiKeyActivityResource]`, newest first.

**Example**

```python
from nextmail import nextmail

history = nextmail.keys.list_all_activity('4c1b257a66287fd113bd89d0')

refused = [change for change in history if change['type'] == 'auth_failed']

print(len(refused), 'refused attempts')
```

**Notes**

- If any page fails, the call raises and the rows already fetched are discarded.

Also available in: API [`GET /keys/{id}/activity`](https://nextmail.sh/docs/api/reference/api-keys#get-keys-id-activity); TypeScript [`keys.listAllActivity()`](https://nextmail.sh/docs/sdk/reference/keys#listAllActivity); Ruby [`keys.list_all_activity`](https://nextmail.sh/docs/ruby/reference/keys#listAllActivity); PHP [`keys->listAllActivity`](https://nextmail.sh/docs/php/reference/keys#listAllActivity); Go [`Keys.ListAllActivity`](https://nextmail.sh/docs/go/reference/keys#listAllActivity); Java [`keys().listAllActivity`](https://nextmail.sh/docs/java/reference/keys#listAllActivity); C# [`Keys.ListAllActivityAsync`](https://nextmail.sh/docs/csharp/reference/keys#listAllActivity).

### `keys.iterate_activity()`

Stream one key's audit log one change at a time

```python
def iterate_activity(
    id: str,
    *,
    limit: int | None = None,
    cursor: str | None = None,
    since: datetime | str | None = None,
    until: datetime | str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> Iterator[ApiKeyActivityResource]
```

A generator over `list_activity` under the same window, fetching a page only when the one before is drained. Nothing is requested until you start iterating, and breaking out of the loop stops the requests.

Scopes: `keys:read`.

**Parameters**

- `id` (`str`, required): Key id, the 24 hex characters after `nm_live_`.
- `limit` (`int`): Page size per request, 1 to 100. The server defaults to 25.
- `cursor` (`str`): Starts the walk from this cursor instead of the newest row.
- `since` (`datetime | str`): Only rows at or after this instant. A `datetime` is sent as ISO 8601 in UTC, and a string must already be one.
- `until` (`datetime | str`): Only rows before this instant. It has to be later than `since`, or the server answers 400 `invalid_parameter`.
- `api_key` (`str`): Overrides the client API key for every page of this walk.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`Iterator[ApiKeyActivityResource]`, a generator yielding one change per step.

**Example**

```python
from nextmail import nextmail

for change in nextmail.keys.iterate_activity('4c1b257a66287fd113bd89d0'):
    if change['type'] == 'rotated':
        actor = change['actor']['label'] if change['actor'] else 'unknown'

        print('Last rotated by', actor, 'at', change['createdAt'])
        break
```

Also available in: API [`GET /keys/{id}/activity`](https://nextmail.sh/docs/api/reference/api-keys#get-keys-id-activity); TypeScript [`keys.iterateActivity()`](https://nextmail.sh/docs/sdk/reference/keys#iterateActivity); Ruby [`keys.iterate_activity`](https://nextmail.sh/docs/ruby/reference/keys#iterateActivity); PHP [`keys->iterateActivity`](https://nextmail.sh/docs/php/reference/keys#iterateActivity); Go [`Keys.IterateActivity`](https://nextmail.sh/docs/go/reference/keys#iterateActivity); Java [`keys().iterateActivity`](https://nextmail.sh/docs/java/reference/keys#iterateActivity); C# [`Keys.IterateActivityAsync`](https://nextmail.sh/docs/csharp/reference/keys#iterateActivity).

### `keys.list_workspace_requests()`

List one page of the request log of every key

```python
def list_workspace_requests(
    *,
    limit: int | None = None,
    cursor: str | None = None,
    since: datetime | str | None = None,
    until: datetime | str | None = None,
    failed_only: bool | None = None,
    statuses: Sequence[int] | None = None,
    path: str | None = None,
    key_ids: Sequence[str] | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> Page[ApiKeyRequestResource]
```

Returns one page of every call the workspace's keys made, newest first, the Requests tab of Settings, API keys. The request log records every authenticated call a key made: method, path, status, error code, duration, IP and user agent, and never a body or a query string. A call refused before a key could be identified is not in it, and neither is a call made with an OAuth access token. Nothing is pruned, so the log reaches back to a key's first call. `key_ids=`, `failed_only=`, `statuses=`, `path=`, `since=` and `until=` narrow it, and they combine. `key_ids=` may name a deleted key.

A narrowed key reads only the log of the keys it can see, so naming another key in `key_ids=` matches nothing.

Scopes: `keys:read`.

**Parameters**

- `limit` (`int`): Rows per page, a whole number from 1 to 100. The server defaults to 25.
- `cursor` (`str`): The `nextCursor` from the previous page, passed back unchanged. Never build one yourself.
- `since` (`datetime | str`): Only rows at or after this instant. A `datetime` is sent as ISO 8601 in UTC, and a string must already be one.
- `until` (`datetime | str`): Only rows before this instant. It has to be later than `since`, or the server answers 400 `invalid_parameter`.
- `failed_only` (`bool`): Only calls answered with a status of 400 or more.
- `statuses` (`Sequence[int]`): Only calls answered with one of these HTTP status codes, such as `[401, 403]`. At most 20, each from 100 to 599, sent comma-separated as `status`.
- `path` (`str`): Only calls to this route, whatever the method, such as `/emails` or `/emails/:id`. It matches the route, not the exact path: an id counts as `:id` and an email address as `:address`. End it with `*` to read every route that starts with it, such as `/emails/*`.
- `key_ids` (`Sequence[str]`): Key ids to read, at most 50, sent comma-separated. Left out, every key the caller can see.
- `api_key` (`str`): Overrides the client API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`Page[ApiKeyRequestResource]`, a dict with `items`, `hasMore` and `nextCursor`. Each item has `id`, `keyId`, `keyName`, `requestId`, `method`, `path`, `status`, `errorCode`, `durationMs`, `ip`, `userAgent` and `createdAt`.

**Example**

```python
from nextmail import nextmail

page = nextmail.keys.list_workspace_requests(path='/emails', statuses=[403, 422])

for call in page['items']:
    print(call['keyName'], call['status'], call['errorCode'])
```

Also available in: API [`GET /keys/requests`](https://nextmail.sh/docs/api/reference/api-keys#get-keys-requests); TypeScript [`keys.listWorkspaceRequests()`](https://nextmail.sh/docs/sdk/reference/keys#listWorkspaceRequests); Ruby [`keys.list_workspace_requests`](https://nextmail.sh/docs/ruby/reference/keys#listWorkspaceRequests); PHP [`keys->listWorkspaceRequests`](https://nextmail.sh/docs/php/reference/keys#listWorkspaceRequests); Go [`Keys.ListWorkspaceRequests`](https://nextmail.sh/docs/go/reference/keys#listWorkspaceRequests); Java [`keys().listWorkspaceRequests`](https://nextmail.sh/docs/java/reference/keys#listWorkspaceRequests); C# [`Keys.ListWorkspaceRequestsAsync`](https://nextmail.sh/docs/csharp/reference/keys#listWorkspaceRequests); CLI [`nextmail keys list-workspace-requests`](https://nextmail.sh/docs/cli/reference/keys#keys-list-workspace-requests).

### `keys.list_all_workspace_requests()`

Collect the request log of every key into one list

```python
def list_all_workspace_requests(
    *,
    limit: int | None = None,
    cursor: str | None = None,
    since: datetime | str | None = None,
    until: datetime | str | None = None,
    failed_only: bool | None = None,
    statuses: Sequence[int] | None = None,
    path: str | None = None,
    key_ids: Sequence[str] | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> builtins.list[ApiKeyRequestResource]
```

Walks every page of `list_workspace_requests` under the same filters and returns every call in one list. The log is never pruned, so give it a window, or use `iterate_workspace_requests` to stop early.

Scopes: `keys:read`.

**Parameters**

- `limit` (`int`): Page size for each request, 1 to 100. The server defaults to 25.
- `cursor` (`str`): Starts the walk from this cursor instead of the newest row.
- `since` (`datetime | str`): Only rows at or after this instant. A `datetime` is sent as ISO 8601 in UTC, and a string must already be one.
- `until` (`datetime | str`): Only rows before this instant. It has to be later than `since`, or the server answers 400 `invalid_parameter`.
- `failed_only` (`bool`): Only calls answered with a status of 400 or more.
- `statuses` (`Sequence[int]`): Only calls answered with one of these HTTP status codes, such as `[401, 403]`. At most 20, each from 100 to 599, sent comma-separated as `status`.
- `path` (`str`): Only calls to this route, whatever the method, such as `/emails` or `/emails/:id`. It matches the route, not the exact path: an id counts as `:id` and an email address as `:address`. End it with `*` to read every route that starts with it, such as `/emails/*`.
- `key_ids` (`Sequence[str]`): Key ids to read, at most 50, sent comma-separated. Left out, every key the caller can see.
- `api_key` (`str`): Overrides the client API key for every page of this walk.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`list[ApiKeyRequestResource]`, newest first.

**Example**

```python
from datetime import datetime, timedelta, timezone

from nextmail import nextmail

failures = nextmail.keys.list_all_workspace_requests(
    failed_only=True,
    since=datetime.now(timezone.utc) - timedelta(hours=1),
    limit=100,
)

print(len(failures), 'failed calls in the last hour')
```

**Notes**

- If any page fails, the call raises and the rows already fetched are discarded.

Also available in: API [`GET /keys/requests`](https://nextmail.sh/docs/api/reference/api-keys#get-keys-requests); TypeScript [`keys.listAllWorkspaceRequests()`](https://nextmail.sh/docs/sdk/reference/keys#listAllWorkspaceRequests); Ruby [`keys.list_all_workspace_requests`](https://nextmail.sh/docs/ruby/reference/keys#listAllWorkspaceRequests); PHP [`keys->listAllWorkspaceRequests`](https://nextmail.sh/docs/php/reference/keys#listAllWorkspaceRequests); Go [`Keys.ListAllWorkspaceRequests`](https://nextmail.sh/docs/go/reference/keys#listAllWorkspaceRequests); Java [`keys().listAllWorkspaceRequests`](https://nextmail.sh/docs/java/reference/keys#listAllWorkspaceRequests); C# [`Keys.ListAllWorkspaceRequestsAsync`](https://nextmail.sh/docs/csharp/reference/keys#listAllWorkspaceRequests).

### `keys.iterate_workspace_requests()`

Stream the request log of every key one call at a time

```python
def iterate_workspace_requests(
    *,
    limit: int | None = None,
    cursor: str | None = None,
    since: datetime | str | None = None,
    until: datetime | str | None = None,
    failed_only: bool | None = None,
    statuses: Sequence[int] | None = None,
    path: str | None = None,
    key_ids: Sequence[str] | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> Iterator[ApiKeyRequestResource]
```

A generator over `list_workspace_requests` under the same filters, fetching a page only when the one before is drained. Nothing is requested until you start iterating, and breaking out of the loop stops the requests.

Scopes: `keys:read`.

**Parameters**

- `limit` (`int`): Page size per request, 1 to 100. The server defaults to 25.
- `cursor` (`str`): Starts the walk from this cursor instead of the newest row.
- `since` (`datetime | str`): Only rows at or after this instant. A `datetime` is sent as ISO 8601 in UTC, and a string must already be one.
- `until` (`datetime | str`): Only rows before this instant. It has to be later than `since`, or the server answers 400 `invalid_parameter`.
- `failed_only` (`bool`): Only calls answered with a status of 400 or more.
- `statuses` (`Sequence[int]`): Only calls answered with one of these HTTP status codes, such as `[401, 403]`. At most 20, each from 100 to 599, sent comma-separated as `status`.
- `path` (`str`): Only calls to this route, whatever the method, such as `/emails` or `/emails/:id`. It matches the route, not the exact path: an id counts as `:id` and an email address as `:address`. End it with `*` to read every route that starts with it, such as `/emails/*`.
- `key_ids` (`Sequence[str]`): Key ids to read, at most 50, sent comma-separated. Left out, every key the caller can see.
- `api_key` (`str`): Overrides the client API key for every page of this walk.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`Iterator[ApiKeyRequestResource]`, a generator yielding one call per step.

**Example**

```python
from nextmail import nextmail

for call in nextmail.keys.iterate_workspace_requests(failed_only=True):
    if call['status'] >= 500:
        print(call['requestId'], call['keyName'], call['path'])
```

Also available in: API [`GET /keys/requests`](https://nextmail.sh/docs/api/reference/api-keys#get-keys-requests); TypeScript [`keys.iterateWorkspaceRequests()`](https://nextmail.sh/docs/sdk/reference/keys#iterateWorkspaceRequests); Ruby [`keys.iterate_workspace_requests`](https://nextmail.sh/docs/ruby/reference/keys#iterateWorkspaceRequests); PHP [`keys->iterateWorkspaceRequests`](https://nextmail.sh/docs/php/reference/keys#iterateWorkspaceRequests); Go [`Keys.IterateWorkspaceRequests`](https://nextmail.sh/docs/go/reference/keys#iterateWorkspaceRequests); Java [`keys().iterateWorkspaceRequests`](https://nextmail.sh/docs/java/reference/keys#iterateWorkspaceRequests); C# [`Keys.IterateWorkspaceRequestsAsync`](https://nextmail.sh/docs/csharp/reference/keys#iterateWorkspaceRequests).

### `keys.list_workspace_activity()`

List one page of what happened to every key

```python
def list_workspace_activity(
    *,
    limit: int | None = None,
    cursor: str | None = None,
    since: datetime | str | None = None,
    until: datetime | str | None = None,
    key_ids: Sequence[str] | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> Page[ApiKeyActivityResource]
```

Returns one page of the audit log of every key in the workspace, newest first, the Activity tab of Settings, API keys. Every change to a key is a row: `created`, `updated`, `rotated`, `deactivated`, `reactivated`, `revoked` and `deleted`, plus `auth_failed` for every call that presented the key and was refused. `actor` names who made the change, a person as `@username` or a key as `API key <name>` in `label`, and `detail['source']` says where it came from: `console`, `api`, `mcp` or `documentation`.

`key_ids=` narrows it, deleted keys included, and `since=` and `until=` keep a window. A narrowed key reads only the activity of the keys it can see.

Scopes: `keys:read`.

**Parameters**

- `limit` (`int`): Rows per page, a whole number from 1 to 100. The server defaults to 25.
- `cursor` (`str`): The `nextCursor` from the previous page, passed back unchanged. Never build one yourself.
- `since` (`datetime | str`): Only rows at or after this instant. A `datetime` is sent as ISO 8601 in UTC, and a string must already be one.
- `until` (`datetime | str`): Only rows before this instant. It has to be later than `since`, or the server answers 400 `invalid_parameter`.
- `key_ids` (`Sequence[str]`): Key ids to read, at most 50, sent comma-separated. Left out, every key the caller can see.
- `api_key` (`str`): Overrides the client API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`Page[ApiKeyActivityResource]`, a dict with `items`, `hasMore` and `nextCursor`. Each item has `id`, `keyId`, `keyName`, `type`, `createdAt`, `actor` and `detail`.

**Example**

```python
from nextmail import nextmail

page = nextmail.keys.list_workspace_activity(since='2026-09-01T00:00:00Z')

for change in page['items']:
    actor = change['actor']['label'] if change['actor'] else 'unknown'

    print(change['keyName'], change['type'], actor)
```

Also available in: API [`GET /keys/activity`](https://nextmail.sh/docs/api/reference/api-keys#get-keys-activity); TypeScript [`keys.listWorkspaceActivity()`](https://nextmail.sh/docs/sdk/reference/keys#listWorkspaceActivity); Ruby [`keys.list_workspace_activity`](https://nextmail.sh/docs/ruby/reference/keys#listWorkspaceActivity); PHP [`keys->listWorkspaceActivity`](https://nextmail.sh/docs/php/reference/keys#listWorkspaceActivity); Go [`Keys.ListWorkspaceActivity`](https://nextmail.sh/docs/go/reference/keys#listWorkspaceActivity); Java [`keys().listWorkspaceActivity`](https://nextmail.sh/docs/java/reference/keys#listWorkspaceActivity); C# [`Keys.ListWorkspaceActivityAsync`](https://nextmail.sh/docs/csharp/reference/keys#listWorkspaceActivity); CLI [`nextmail keys list-workspace-activity`](https://nextmail.sh/docs/cli/reference/keys#keys-list-workspace-activity).

### `keys.list_all_workspace_activity()`

Collect the audit log of every key into one list

```python
def list_all_workspace_activity(
    *,
    limit: int | None = None,
    cursor: str | None = None,
    since: datetime | str | None = None,
    until: datetime | str | None = None,
    key_ids: Sequence[str] | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> builtins.list[ApiKeyActivityResource]
```

Walks every page of `list_workspace_activity` under the same filters and returns every change in one list, newest first.

Scopes: `keys:read`.

**Parameters**

- `limit` (`int`): Page size for each request, 1 to 100. The server defaults to 25.
- `cursor` (`str`): Starts the walk from this cursor instead of the newest row.
- `since` (`datetime | str`): Only rows at or after this instant. A `datetime` is sent as ISO 8601 in UTC, and a string must already be one.
- `until` (`datetime | str`): Only rows before this instant. It has to be later than `since`, or the server answers 400 `invalid_parameter`.
- `key_ids` (`Sequence[str]`): Key ids to read, at most 50, sent comma-separated. Left out, every key the caller can see.
- `api_key` (`str`): Overrides the client API key for every page of this walk.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`list[ApiKeyActivityResource]`, newest first.

**Example**

```python
from datetime import datetime, timezone

from nextmail import nextmail

changes = nextmail.keys.list_all_workspace_activity(
    since=datetime(2026, 9, 1, tzinfo=timezone.utc)
)

over_api = [change for change in changes if change['detail'].get('source') == 'api']

print(len(over_api), 'changes made over the API')
```

**Notes**

- If any page fails, the call raises and the rows already fetched are discarded.

Also available in: API [`GET /keys/activity`](https://nextmail.sh/docs/api/reference/api-keys#get-keys-activity); TypeScript [`keys.listAllWorkspaceActivity()`](https://nextmail.sh/docs/sdk/reference/keys#listAllWorkspaceActivity); Ruby [`keys.list_all_workspace_activity`](https://nextmail.sh/docs/ruby/reference/keys#listAllWorkspaceActivity); PHP [`keys->listAllWorkspaceActivity`](https://nextmail.sh/docs/php/reference/keys#listAllWorkspaceActivity); Go [`Keys.ListAllWorkspaceActivity`](https://nextmail.sh/docs/go/reference/keys#listAllWorkspaceActivity); Java [`keys().listAllWorkspaceActivity`](https://nextmail.sh/docs/java/reference/keys#listAllWorkspaceActivity); C# [`Keys.ListAllWorkspaceActivityAsync`](https://nextmail.sh/docs/csharp/reference/keys#listAllWorkspaceActivity).

### `keys.iterate_workspace_activity()`

Stream the audit log of every key one change at a time

```python
def iterate_workspace_activity(
    *,
    limit: int | None = None,
    cursor: str | None = None,
    since: datetime | str | None = None,
    until: datetime | str | None = None,
    key_ids: Sequence[str] | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> Iterator[ApiKeyActivityResource]
```

A generator over `list_workspace_activity` under the same filters, fetching a page only when the one before is drained. Nothing is requested until you start iterating, and breaking out of the loop stops the requests.

Scopes: `keys:read`.

**Parameters**

- `limit` (`int`): Page size per request, 1 to 100. The server defaults to 25.
- `cursor` (`str`): Starts the walk from this cursor instead of the newest row.
- `since` (`datetime | str`): Only rows at or after this instant. A `datetime` is sent as ISO 8601 in UTC, and a string must already be one.
- `until` (`datetime | str`): Only rows before this instant. It has to be later than `since`, or the server answers 400 `invalid_parameter`.
- `key_ids` (`Sequence[str]`): Key ids to read, at most 50, sent comma-separated. Left out, every key the caller can see.
- `api_key` (`str`): Overrides the client API key for every page of this walk.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`Iterator[ApiKeyActivityResource]`, a generator yielding one change per step.

**Example**

```python
from nextmail import nextmail

for change in nextmail.keys.iterate_workspace_activity(
    key_ids=['4c1b257a66287fd113bd89d0', '9e0f6b2c1d7a3e584b2c6f10']
):
    if change['type'] == 'auth_failed':
        print(change['keyName'], change['detail'].get('reason'), change['createdAt'])
```

Also available in: API [`GET /keys/activity`](https://nextmail.sh/docs/api/reference/api-keys#get-keys-activity); TypeScript [`keys.iterateWorkspaceActivity()`](https://nextmail.sh/docs/sdk/reference/keys#iterateWorkspaceActivity); Ruby [`keys.iterate_workspace_activity`](https://nextmail.sh/docs/ruby/reference/keys#iterateWorkspaceActivity); PHP [`keys->iterateWorkspaceActivity`](https://nextmail.sh/docs/php/reference/keys#iterateWorkspaceActivity); Go [`Keys.IterateWorkspaceActivity`](https://nextmail.sh/docs/go/reference/keys#iterateWorkspaceActivity); Java [`keys().iterateWorkspaceActivity`](https://nextmail.sh/docs/java/reference/keys#iterateWorkspaceActivity); C# [`Keys.IterateWorkspaceActivityAsync`](https://nextmail.sh/docs/csharp/reference/keys#iterateWorkspaceActivity).

### `keys.stats()`

Read what the keys did inside a window

```python
def stats(
    *,
    since: datetime | str | None = None,
    until: datetime | str | None = None,
    key_ids: Sequence[str] | None = None,
    grain: TrackingGrain | None = None,
    offset_minutes: int | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> ApiKeyStatsResource
```

Returns the numbers behind the Analytics tab of the API keys page: the mail the keys sent and what became of it, the calls refused because a secret was wrong, revoked or expired, the requests they made and how many failed, the routes they called most with the median time each took, and the status codes they got back.

It covers every key you can see, or the ones `key_ids=` names. The window runs from `since=` to `until=`, and left out it is the 30 days before now. `grain=` sets the bucket width of the series and `offset_minutes=` shifts the boundaries so days break where the reader's day does.

Scopes: `keys:read`, `emails:read`.

**Parameters**

- `since` (`datetime | str`): The start of the window, a `datetime` or an ISO 8601 instant. Defaults to 30 days before `until`.
- `until` (`datetime | str`): The end of the window, not included. Defaults to now.
- `key_ids` (`Sequence[str]`): Only these keys, at most 50. Left out, every key you can see.
- `grain` (`TrackingGrain`): Bucket width: `'minute'`, `'hour'` or `'day'`, defaulting to `'day'`.
- `offset_minutes` (`int`): Minutes east of UTC to bucket in, from -840 to 840, defaulting to 0.
- `api_key` (`str`): Overrides the client API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`ApiKeyStatsResource` with the window it covered, `sends`, `rejected`, `requests`, `routes` and `codes`.

**Example**

```python
from nextmail import nextmail

stats = nextmail.keys.stats(grain='day', offset_minutes=60)

print(stats['sends']['totals']['sends'], 'sent since', stats['since'])

for route in stats['routes'][:3]:
    print(route['label'], route['count'], route['medianMs'])
```

**Notes**

- A key narrowed to some domains or addresses only counts the keys whose send scope sits inside its own, and an access token is refused with 403 `owner_only` unless it acts for the owner of the workspace.
- The series are sparse: a bucket with nothing in it has no entry, so a chart must fill the gaps.
- Retried automatically on network failure, since it only reads.

Also available in: API [`GET /keys/stats`](https://nextmail.sh/docs/api/reference/api-keys#get-keys-stats); TypeScript [`keys.stats()`](https://nextmail.sh/docs/sdk/reference/keys#stats); Ruby [`keys.stats`](https://nextmail.sh/docs/ruby/reference/keys#stats); PHP [`keys->stats`](https://nextmail.sh/docs/php/reference/keys#stats); Go [`Keys.Stats`](https://nextmail.sh/docs/go/reference/keys#stats); Java [`keys().stats`](https://nextmail.sh/docs/java/reference/keys#stats); C# [`Keys.StatsAsync`](https://nextmail.sh/docs/csharp/reference/keys#stats); CLI [`nextmail keys stats`](https://nextmail.sh/docs/cli/reference/keys#keys-stats).
