FastPII Docs
AI Gateway

Gateway Audit

Log, query, export, and analyze FastPII Gateway audit events and dashboard metrics.

Gateway Audit

Audit logging gives you an event-by-event record of what the Gateway saw and what it did. Each audit event can capture the request ID, user ID, workspace, auth mode, provider, model, action, entity types, confidence scores, risk level, latency, and request metadata.

Use audit data for compliance review, operations, incident response, and policy tuning.

For policy design, see Policies. For initial configuration, see Gateway Setup.

What gets logged

An audit event can include fields like:

{
  "request_id": "req_01J9X9BZ47A0P2QK3W4Q9D0M7J",
  "user_id": "user_123",
  "workspace_id": "ws_prod_eu",
  "auth_mode": "api_key",
  "provider": "openai",
  "model": "gpt-4o-mini",
  "action": "MASK",
  "entity_types": ["EMAIL", "PHONE"],
  "confidence_scores": [0.98, 0.91],
  "risk_level": "MEDIUM",
  "country": "CZ",
  "policy_id": "pol_default_mask",
  "policy_name": "Default mask policy",
  "latency_ms": 123.4,
  "detection_latency_ms": 40.1,
  "protection_latency_ms": 83.3,
  "is_streaming": false,
  "request_method": "POST",
  "request_path": "/v1/chat/completions",
  "response_status": 200
}

Risk levels

The Gateway audit API supports these risk levels:

Risk levelMeaning
LOWLow sensitivity or low-impact exposure
MEDIUMModerate sensitivity, should usually be protected
HIGHSensitive personal data, usually needs strong controls
CRITICALStrictly controlled or prohibited data

Log an audit event

Use POST /api/v1/gateway/audit to record a gateway event. The endpoint returns 202 Accepted.

curl

curl -X POST "$FASTPII_BASE_URL/api/v1/gateway/audit" \
  -H "Content-Type: application/json" \
  -H "X-Service-Token: $FASTPII_SERVICE_TOKEN" \
  -d '{
    "request_id": "req_01J9X9BZ47A0P2QK3W4Q9D0M7J",
    "user_id": "user_123",
    "workspace_id": "ws_prod_eu",
    "auth_mode": "api_key",
    "provider": "openai",
    "model": "gpt-4o-mini",
    "action": "MASK",
    "entity_types": ["EMAIL", "PHONE"],
    "confidence_scores": [0.98, 0.91],
    "risk_level": "MEDIUM",
    "country": "CZ",
    "policy_id": "pol_default_mask",
    "policy_name": "Default mask policy",
    "latency_ms": 123.4,
    "detection_latency_ms": 40.1,
    "protection_latency_ms": 83.3,
    "is_streaming": false,
    "request_method": "POST",
    "request_path": "/v1/chat/completions",
    "response_status": 200
  }'

Python

import requests

base_url = "https://gateway.fastpii.com"
service_token = "your-gateway-service-token"

payload = {
    "request_id": "req_01J9X9BZ47A0P2QK3W4Q9D0M7J",
    "user_id": "user_123",
    "workspace_id": "ws_prod_eu",
    "auth_mode": "api_key",
    "provider": "openai",
    "model": "gpt-4o-mini",
    "action": "MASK",
    "entity_types": ["EMAIL", "PHONE"],
    "confidence_scores": [0.98, 0.91],
    "risk_level": "MEDIUM",
    "country": "CZ",
    "policy_id": "pol_default_mask",
    "policy_name": "Default mask policy",
    "latency_ms": 123.4,
    "detection_latency_ms": 40.1,
    "protection_latency_ms": 83.3,
    "is_streaming": False,
    "request_method": "POST",
    "request_path": "/v1/chat/completions",
    "response_status": 200,
}

response = requests.post(
    f"{base_url}/api/v1/gateway/audit",
    headers={
        "X-Service-Token": service_token,
        "Content-Type": "application/json",
    },
    json=payload,
    timeout=30,
)
response.raise_for_status()
print(response.status_code)
print(response.json())

Example response:

{
  "id": "evt_01J9X9C6AK6SRF5NXAK2J2J58W",
  "timestamp": "2026-07-10T10:22:17.321000Z",
  "request_id": "req_01J9X9BZ47A0P2QK3W4Q9D0M7J",
  "user_id": "user_123",
  "workspace_id": "ws_prod_eu",
  "auth_mode": "api_key",
  "provider": "openai",
  "model": "gpt-4o-mini",
  "action": "MASK",
  "entity_types": ["EMAIL", "PHONE"],
  "confidence_scores": [0.98, 0.91],
  "risk_level": "MEDIUM",
  "country": "CZ",
  "policy_id": "pol_default_mask",
  "policy_name": "Default mask policy",
  "latency_ms": 123.4,
  "detection_latency_ms": 40.1,
  "protection_latency_ms": 83.3,
  "is_streaming": false,
  "request_method": "POST",
  "request_path": "/v1/chat/completions",
  "response_status": 200
}

Query audit events

Use GET /api/v1/gateway/audit to list events by workspace. The API supports filters for user_id, action, provider, risk_level, plus pagination with page and page_size.

curl

curl -X GET "$FASTPII_BASE_URL/api/v1/gateway/audit?workspace_id=$FASTPII_WORKSPACE_ID&user_id=user_123&action=MASK&provider=openai&risk_level=MEDIUM&page=1&page_size=50" \
  -H "X-Service-Token: $FASTPII_SERVICE_TOKEN"

Python

import requests

workspace_id = "ws_prod_eu"

response = requests.get(
    f"{base_url}/api/v1/gateway/audit",
    params={
        "workspace_id": workspace_id,
        "user_id": "user_123",
        "action": "MASK",
        "provider": "openai",
        "risk_level": "MEDIUM",
        "page": 1,
        "page_size": 50,
    },
    headers={"X-Service-Token": service_token},
    timeout=30,
)
response.raise_for_status()
print(response.json())

Example response:

{
  "events": [
    {
      "id": "evt_01J9X9C6AK6SRF5NXAK2J2J58W",
      "timestamp": "2026-07-10T10:22:17.321000Z",
      "request_id": "req_01J9X9BZ47A0P2QK3W4Q9D0M7J",
      "user_id": "user_123",
      "workspace_id": "ws_prod_eu",
      "auth_mode": "api_key",
      "provider": "openai",
      "model": "gpt-4o-mini",
      "action": "MASK",
      "entity_types": ["EMAIL", "PHONE"],
      "confidence_scores": [0.98, 0.91],
      "risk_level": "MEDIUM",
      "country": "CZ",
      "policy_id": "pol_default_mask",
      "policy_name": "Default mask policy",
      "latency_ms": 123.4,
      "detection_latency_ms": 40.1,
      "protection_latency_ms": 83.3,
      "is_streaming": false,
      "request_method": "POST",
      "request_path": "/v1/chat/completions",
      "response_status": 200
    }
  ],
  "total": 1,
  "page": 1,
  "page_size": 50
}

Get a single event

Use GET /api/v1/gateway/audit/{event_id} when you need the full record for one event.

curl

curl -X GET "$FASTPII_BASE_URL/api/v1/gateway/audit/evt_01J9X9C6AK6SRF5NXAK2J2J58W" \
  -H "X-Service-Token: $FASTPII_SERVICE_TOKEN"

Python

import requests

event_id = "evt_01J9X9C6AK6SRF5NXAK2J2J58W"

response = requests.get(
    f"{base_url}/api/v1/gateway/audit/{event_id}",
    headers={"X-Service-Token": service_token},
    timeout=30,
)
response.raise_for_status()
print(response.json())

Export audit events

Use GET /api/v1/gateway/audit/export to export events in JSON or CSV. Supported filters include start_date, end_date, action, provider, and risk_level.

Export as JSON

curl -X GET "$FASTPII_BASE_URL/api/v1/gateway/audit/export?workspace_id=$FASTPII_WORKSPACE_ID&format=json&start_date=2026-07-01T00:00:00&end_date=2026-07-10T23:59:59&action=MASK&provider=openai&risk_level=MEDIUM" \
  -H "X-Service-Token: $FASTPII_SERVICE_TOKEN"
import requests

response = requests.get(
    f"{base_url}/api/v1/gateway/audit/export",
    params={
        "workspace_id": workspace_id,
        "format": "json",
        "start_date": "2026-07-01T00:00:00",
        "end_date": "2026-07-10T23:59:59",
        "action": "MASK",
        "provider": "openai",
        "risk_level": "MEDIUM",
    },
    headers={"X-Service-Token": service_token},
    timeout=60,
)
response.raise_for_status()
print(response.json())

Example JSON response:

[
  {
    "id": "evt_01J9X9C6AK6SRF5NXAK2J2J58W",
    "timestamp": "2026-07-10T10:22:17.321000Z",
    "request_id": "req_01J9X9BZ47A0P2QK3W4Q9D0M7J",
    "user_id": "user_123",
    "workspace_id": "ws_prod_eu",
    "auth_mode": "api_key",
    "provider": "openai",
    "model": "gpt-4o-mini",
    "action": "MASK",
    "entity_types": ["EMAIL", "PHONE"],
    "confidence_scores": [0.98, 0.91],
    "risk_level": "MEDIUM",
    "country": "CZ",
    "policy_id": "pol_default_mask",
    "policy_name": "Default mask policy",
    "latency_ms": 123.4,
    "detection_latency_ms": 40.1,
    "protection_latency_ms": 83.3,
    "is_streaming": false,
    "request_method": "POST",
    "request_path": "/v1/chat/completions",
    "response_status": 200
  }
]

Export as CSV

curl -X GET "$FASTPII_BASE_URL/api/v1/gateway/audit/export?workspace_id=$FASTPII_WORKSPACE_ID&format=csv&start_date=2026-07-01T00:00:00&end_date=2026-07-10T23:59:59" \
  -H "X-Service-Token: $FASTPII_SERVICE_TOKEN" \
  -o gateway-audit-export.csv
import requests

response = requests.get(
    f"{base_url}/api/v1/gateway/audit/export",
    params={
        "workspace_id": workspace_id,
        "format": "csv",
        "start_date": "2026-07-01T00:00:00",
        "end_date": "2026-07-10T23:59:59",
    },
    headers={"X-Service-Token": service_token},
    timeout=60,
)
response.raise_for_status()

with open("gateway-audit-export.csv", "wb") as f:
    f.write(response.content)

Example CSV output:

id,timestamp,request_id,user_id,workspace_id,auth_mode,provider,model,action,entity_types,confidence_scores,risk_level,country,policy_id,policy_name,latency_ms,detection_latency_ms,protection_latency_ms,is_streaming,request_method,request_path,response_status
evt_01J9X9C6AK6SRF5NXAK2J2J58W,2026-07-10T10:22:17.321000Z,req_01J9X9BZ47A0P2QK3W4Q9D0M7J,user_123,ws_prod_eu,api_key,openai,gpt-4o-mini,MASK,EMAIL;PHONE,0.98;0.91,MEDIUM,CZ,pol_default_mask,Default mask policy,123.4,40.1,83.3,False,POST,/v1/chat/completions,200

Dashboard metrics

The dashboard endpoints summarize audit data for a workspace.

Time periods

The metrics and violation trends endpoints accept these periods:

PeriodMeaning
7dLast 7 days
30dLast 30 days
90dLast 90 days

Get summary metrics

Use GET /api/v1/gateway/dashboard?workspace_id=...&period=30d.

curl

curl -X GET "$FASTPII_BASE_URL/api/v1/gateway/dashboard?workspace_id=$FASTPII_WORKSPACE_ID&period=30d" \
  -H "X-Service-Token: $FASTPII_SERVICE_TOKEN"

Python

import requests

response = requests.get(
    f"{base_url}/api/v1/gateway/dashboard",
    params={"workspace_id": workspace_id, "period": "30d"},
    headers={"X-Service-Token": service_token},
    timeout=30,
)
response.raise_for_status()
print(response.json())

Example response:

{
  "total_requests": 12450,
  "protected_requests": 9830,
  "blocked_requests": 315,
  "masked_requests": 9515,
  "allowed_requests": 2620,
  "period_start": "2026-06-10T00:00:00Z",
  "period_end": "2026-07-10T23:59:59Z"
}

Use GET /api/v1/gateway/dashboard/violations?workspace_id=...&period=30d.

curl

curl -X GET "$FASTPII_BASE_URL/api/v1/gateway/dashboard/violations?workspace_id=$FASTPII_WORKSPACE_ID&period=30d" \
  -H "X-Service-Token: $FASTPII_SERVICE_TOKEN"

Python

import requests

response = requests.get(
    f"{base_url}/api/v1/gateway/dashboard/violations",
    params={"workspace_id": workspace_id, "period": "30d"},
    headers={"X-Service-Token": service_token},
    timeout=30,
)
response.raise_for_status()
print(response.json())

Example response:

{
  "period": "30d",
  "trends": [
    {
      "date": "2026-07-08",
      "blocked": 11,
      "masked": 304,
      "warned": 27,
      "allowed": 82
    },
    {
      "date": "2026-07-09",
      "blocked": 9,
      "masked": 289,
      "warned": 21,
      "allowed": 95
    }
  ]
}

Get risk distribution

Use GET /api/v1/gateway/dashboard/risk?workspace_id=....

curl

curl -X GET "$FASTPII_BASE_URL/api/v1/gateway/dashboard/risk?workspace_id=$FASTPII_WORKSPACE_ID" \
  -H "X-Service-Token: $FASTPII_SERVICE_TOKEN"

Python

import requests

response = requests.get(
    f"{base_url}/api/v1/gateway/dashboard/risk",
    params={"workspace_id": workspace_id},
    headers={"X-Service-Token": service_token},
    timeout=30,
)
response.raise_for_status()
print(response.json())

Example response:

{
  "risk_distribution": [
    {
      "entity_type": "EMAIL",
      "count": 4210,
      "risk_level": "MEDIUM"
    },
    {
      "entity_type": "CREDIT_CARD",
      "count": 87,
      "risk_level": "CRITICAL"
    }
  ],
  "by_provider": {
    "openai": {
      "MEDIUM": 3900,
      "CRITICAL": 80
    },
    "anthropic": {
      "MEDIUM": 310,
      "CRITICAL": 7
    }
  },
  "by_model": {
    "gpt-4o-mini": {
      "MEDIUM": 3700,
      "CRITICAL": 60
    },
    "claude-3-5-sonnet": {
      "MEDIUM": 510,
      "CRITICAL": 27
    }
  }
}

Get provider usage

Use GET /api/v1/gateway/dashboard/providers?workspace_id=....

curl

curl -X GET "$FASTPII_BASE_URL/api/v1/gateway/dashboard/providers?workspace_id=$FASTPII_WORKSPACE_ID" \
  -H "X-Service-Token: $FASTPII_SERVICE_TOKEN"

Python

import requests

response = requests.get(
    f"{base_url}/api/v1/gateway/dashboard/providers",
    params={"workspace_id": workspace_id},
    headers={"X-Service-Token": service_token},
    timeout=30,
)
response.raise_for_status()
print(response.json())

Example response:

{
  "providers": [
    {
      "provider": "openai",
      "model": "gpt-4o-mini",
      "request_count": 9200,
      "blocked_count": 210,
      "masked_count": 7150
    },
    {
      "provider": "anthropic",
      "model": "claude-3-5-sonnet",
      "request_count": 3250,
      "blocked_count": 105,
      "masked_count": 2365
    }
  ],
  "total_requests": 12450
}

Operational tips

  • Export JSON when you want to feed events into downstream systems
  • Export CSV for manual review, spreadsheet analysis, or audit handoff
  • Check violation trends after policy changes to confirm the expected effect
  • Review provider usage to spot drift between primary and fallback routes
  • Revisit risk distributions often, especially after adding new detectors or models

On this page