# Export audit events (/docs/developer/audit-events)

Use `GET /v0/audit-events` to export your organization's retained audit events. The API returns the newest events first.

## At a glance [#at-a-glance]

| | |
| --- | --- |
| Endpoint | `GET /v0/audit-events` |
| Key profile | **Audit log (read only)** |
| Scope | `audit:read` |
| Page size | Up to 50 events |
| End of export | `nextCursor` is `null` |
| MCP tool calls | Available for 30 days |

## Make your first request [#make-your-first-request]

Ask a Super Admin to create an **Audit log (read only)** key in **Settings → Connections → Developer access → API keys**.

Run this block in Bash or Zsh. Paste the key at the prompt. If you have a staging key, set the URL to `https://api.staging.getsift.ai`.

```bash
export SIFT_API_BASE='https://api.getsift.ai'
printf 'Sift Audit log key: '
IFS= read -rs SIFT_AUDIT_API_KEY
printf '\n'
export SIFT_AUDIT_API_KEY
```

In the same terminal, request the first page with all event types.

```bash
curl --fail-with-body --get "$SIFT_API_BASE/v0/audit-events" \
  --header "X-API-Key: $SIFT_AUDIT_API_KEY"
```

## Read the response [#read-the-response]

HTTP 200 returns `data` and `nextCursor`. If `data` is empty and a cursor is present, continue to the next page. If the cursor is `null`, you have reached the end.

```json
{
  "success": true,
  "data": [
    {
      "id": "audit_evt_example",
      "type": "EXPORT_DOWNLOADED",
      "actorId": "user_example",
      "actorName": "Taylor",
      "targetId": null,
      "targetName": "people.csv",
      "metadata": { "format": "csv" },
      "createdAt": "2026-11-24T08:56:30.000Z",
      "viaAgent": null
    }
  ],
  "nextCursor": null
}
```

`id` identifies the event. `actorId` and `actorName` identify the actor when known. `targetId` and `targetName` identify the object when known. `viaAgent` identifies the agent surface when present. The shape of `metadata` depends on the event type. Keep unfamiliar event types and metadata fields so your export can accept new events.

## Filter events [#filter-events]

Add one `--data-urlencode` argument to the cURL command per filter. For example, add `--data-urlencode 'types=USER_SIGNED_IN'` to read sign-in events.

| Query parameter | Use |
| --- | --- |
| `from`, `to` | Inclusive bounds on `createdAt`. Use ISO 8601 timestamps with a time zone. |
| `types` | Comma-separated event types. Omit it to return every type. |
| `actorId` | Restrict results to the ID of one actor. |
| `cursor` | Pass the previous response's `nextCursor` unchanged. URL-encode it as a query parameter. |

## Export to a file [#export-to-a-file]

Save this as `export-audit.py`. It writes one JSON event per line. It fixes the end time when it starts and follows cursors through empty pages.

```python
import json
import os
from datetime import datetime, timezone
from urllib.parse import urlencode
from urllib.request import Request, urlopen

base_url = os.environ.get("SIFT_API_BASE", "https://api.getsift.ai").rstrip("/")
headers = {"X-API-Key": os.environ["SIFT_AUDIT_API_KEY"]}
bounds = {
    "to": os.environ.get("SIFT_AUDIT_TO")
    or datetime.now(timezone.utc).isoformat(timespec="milliseconds")
}
if os.environ.get("SIFT_AUDIT_FROM"):
    bounds["from"] = os.environ["SIFT_AUDIT_FROM"]

cursor = None
while True:
    query = dict(bounds)
    if cursor is not None:
        query["cursor"] = cursor
    request = Request(
        f"{base_url}/v0/audit-events?{urlencode(query)}",
        headers=headers,
    )
    with urlopen(request, timeout=60) as response:
        page = json.load(response)

    for event in page["data"]:
        print(json.dumps(event))

    next_cursor = page["nextCursor"]
    if next_cursor is None:
        break
    if next_cursor == cursor:
        raise RuntimeError("The audit cursor did not advance")
    cursor = next_cursor
```

```bash
python3 export-audit.py > audit.ndjson.part \
  && mv audit.ndjson.part audit.ndjson \
  && wc -l audit.ndjson
```

When the command succeeds, it saves `audit.ndjson` in your current directory and prints the event count. If a page fails, the partial output stays in `audit.ndjson.part`.

To make an export repeatable, set its UTC time window before the first run. Replace the example dates with your own.

```bash
export SIFT_AUDIT_FROM='2026-11-01T00:00:00Z'
export SIFT_AUDIT_TO='2026-11-02T00:00:00Z'
```

If a page fails, run the export command again with the same time bounds. The script starts at the first page. To export from the start of retained history, leave `SIFT_AUDIT_FROM` unset.

## Poll for changes [#poll-for-changes]

1. For a historical export, set `to` to a fixed UTC time. Omit `from` to start with all retained history.
2. Store each event by `id`. Keep the filters fixed and send each `nextCursor` as `cursor` on the next request.
3. Stop when `nextCursor` is `null`. Keep paging when `data` is empty and a cursor is present.
4. For the next poll, overlap the previous time window. Use a new `to` value and restart without a cursor. Deduplicate by `id`. Advance the saved time only after all pages succeed.

Some events arrive after their `createdAt` time. Replay the affected window after an outage or a historical import. The export contains events that Sift recorded and still retains.

## Handle errors [#handle-errors]

| HTTP status | What to check |
| --- | --- |
| `400` | Check the timestamps and pass the cursor from the last successful page unchanged. |
| `401` | Check whether the key is present, valid, and still active. |
| `403` | Check that the key has `audit:read`. The Reporting key has a different scope. |
| `500` | If you page in your own client, retry the same page with the same cursor and time bounds. Increase the wait between attempts. |

## Related [#related]

<Cards>
  <Card href="/docs/developer/api" title="REST API">Set up an API client.</Card>
  <Card href="/docs/developer/api-keys" title="API keys">Create, rotate, and revoke keys.</Card>
</Cards>
