> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mcphub.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Audit

> Query and manage PostgreSQL configuration and access audit events.

All audit endpoints require an authenticated **administrator** and operate only when MCPHub is running in PostgreSQL database mode. When `routing.skipAuth` is enabled for local development, unauthenticated requests inherit the synthetic admin identity (`actorVia: "skip-auth"`), as documented in [Authentication](https://docs.mcphub.app/features/authentication.md) — never enable `skipAuth` in production.

> [card] [GET /api/audit/available](#check-audit-availability)
>
> Check whether configuration audit logging is enabled.

> [card] [GET /api/audit](#query-audit-events)
>
> List audit events using keyset cursor pagination and metadata filters.

> [card] [DELETE /api/audit/cleanup](#clean-up-expired-audit-events)
>
> Prune audit events older than the configured or specified retention window.

***

### Check Audit Availability

Checks whether PostgreSQL audit logging is active for the current deployment.

* **Endpoint**: `/api/audit/available`
* **Method**: `GET`
* **Access**: Admin only (`403` for non-admin callers)
* **Success Response**:
  ```json theme={null}
  {
    "success": true,
    "data": {
      "available": true
    }
  }
  ```

***

### Query Audit Events

Returns configuration and access audit events ordered from newest to oldest `(occurredAt DESC, id DESC)` using keyset cursor pagination. Returns `404` when running in file mode.

* **Endpoint**: `/api/audit`
* **Method**: `GET`
* **Access**: Admin only
* **Query Parameters**:
  * `limit` (optional, integer `1`–`100`, default `50`): Number of events per page.
  * `cursor` (optional, opaque string): `pagination.nextCursor` returned by a previous response.
  * `actor` (optional, string): Exact `actorUsername` match.
  * `actionPrefix` (optional, string): Prefix filter on `action` (e.g. `server.`, `group.`, `key.`, `config.`).
  * `targetType` (optional, enum): One of `server`, `group`, `bearer_key`, `user`, `system_config`, `template`.
  * `targetId` (optional, string): Exact `targetId` match.
  * `startDate` (optional, ISO 8601 date string): Inclusive lower bound for `occurredAt`.
  * `endDate` (optional, ISO 8601 date string): Inclusive upper bound for `occurredAt`.
* **Success Response**:
  ```json theme={null}
  {
    "success": true,
    "data": [
      {
        "id": "42",
        "occurredAt": "2026-12-03T10:00:00.123Z",
        "actorUsername": "admin",
        "actorVia": "jwt",
        "actorKeyId": null,
        "action": "server.update",
        "targetType": "server",
        "targetId": "srv1",
        "outcome": "success",
        "changedFields": ["description", "enabled"],
        "detail": null,
        "ip": "127.0.0.1",
        "requestId": "req-9b1deb4d"
      }
    ],
    "pagination": {
      "limit": 50,
      "hasMore": false,
      "nextCursor": null
    }
  }
  ```

***

### Clean Up Expired Audit Events

Deletes audit events older than the configured retention period (`system_config.audit.retentionDays`, default `365`) or an explicit `days` override. Uses a PostgreSQL transaction advisory lock and batched deletes so multi-replica deployments do not contend on cleanup.

* **Endpoint**: `/api/audit/cleanup`
* **Method**: `DELETE`
* **Access**: Admin only
* **Query Parameters**:
  * `days` (optional, integer `1`–`3650`): Override the configured `audit.retentionDays` for this cleanup run.
* **Success Response**:
  ```json theme={null}
  {
    "success": true,
    "data": {
      "deletedCount": 120,
      "retentionDays": 365,
      "skipped": false
    }
  }
  ```
* **Conflict Response (`409`)**: Returned when another worker already holds the cleanup advisory lock (`data.skipped === true`, `deletedCount === 0`).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.