# ApiRequest

> The API request log: every write and every failed request made with your keys and sessions (successful reads aren't logged), newest first.

```python
page = noclick.ApiRequest.list_page(method="POST", status="4xx", key="nk_live_ab12")
```

## ApiRequest

```typescript
class ApiRequest
```

One logged API request.

| Attribute | Type | Description |
| --- | --- | --- |
| `id` | `string` |  |
| `actor` | `string | null` | `key`, `session` or `owner`. |
| `keyId` | `string | null` |  |
| `userId` | `string | null` |  |
| `sessionId` | `string | null` |  |
| `method` | `string` |  |
| `route` | `string` | The route template, e.g. `/v1/agents/{agent_id}/messages`. |
| `pathParams` | `JsonObject` |  |
| `status` | `number | null` |  |
| `errorType` | `string | null` |  |
| `durationMs` | `number | null` |  |
| `idempotencyReplayed` | `boolean` |  |
| `operationId` | `string | null` | The operation the request started, if any. |
| `requestId` | `string | null` |  |
| `createdAt` | `Date | null` |  |
| `raw` | `JsonObject` | The response JSON exactly as the API sent it. |

Types:

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

### ApiRequest.list

```typescript
static list(
  options?: ApiRequestFilters & { limit?: number },
): Promise<ApiRequest[]>
```

Every matching request (or the first `limit`).

Parameters:

- `options.method` (`string`)
- `options.status` (`string | number`): A class (`2xx`, `4xx`, `5xx`) or one code.
- `options.errorType` (`string`): An error type such as `spend_cap_exceeded`.
- `options.key` (`string`): A key id or prefix.
- `options.route` (`string`): A route template such as `/v1/policy`.
- `options.since` (`Date | string`)
- `options.until` (`Date | string`)
- `options.limit` (`number`): Return at most this many.
- `options.userFilter` (`UserFilter`): Which users the read covers (`user_id` query), instead of acting as one (`user`).
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: Every matching request, newest first, fetched page by page.

Types:

```typescript
/**
 * Which users a read covers: `ALL_USERS`, `NO_USER`, a `User` or your id for one. Sent as the
 * `user_id` query parameter, never as the `NoClick-User` header, so it filters without acting as the user.
 */
type UserFilter = typeof ALL_USERS | typeof NO_USER | UserRef;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;
```

### ApiRequest.listPage

```typescript
static listPage(
  options?: ApiRequestFilters & PageOptions,
): Promise<Page<ApiRequest>>
```

One page of matching requests; pass `nextCursor` back as `cursor`.

Parameters:

- `options.cursor` (`string | null`): The `next_cursor` of the page before; omit it for the first page.
- `options.limit` (`number`): Return at most this many.
- `options.method` (`string`)
- `options.status` (`string | number`): A class (`2xx`, `4xx`, `5xx`) or one code.
- `options.errorType` (`string`): An error type such as `spend_cap_exceeded`.
- `options.key` (`string`): A key id or prefix.
- `options.route` (`string`): A route template such as `/v1/policy`.
- `options.since` (`Date | string`)
- `options.until` (`Date | string`)
- `options.userFilter` (`UserFilter`): Which users the read covers (`user_id` query), instead of acting as one (`user`).
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: A page whose `data` holds up to `limit` requests (at most 200) and whose `next_cursor`
is `None` on the last page.

Types:

```typescript
/**
 * Which users a read covers: `ALL_USERS`, `NO_USER`, a `User` or your id for one. Sent as the
 * `user_id` query parameter, never as the `NoClick-User` header, so it filters without acting as the user.
 */
type UserFilter = typeof ALL_USERS | typeof NO_USER | UserRef;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;
```
