# Analytics

> Retrieve aggregated usage statistics for a specific project — request counts, success/failure rates, and breakdowns by provider and model.

Canonical: https://ai-gateway.sassmaker.com/docs/analytics

## Overview

The analytics endpoint returns aggregated usage statistics for your gateway — request volume, success/failure rates, and breakdowns by provider, model, project, and day. Aggregates are stored in Cloudflare D1. It exposes operational load data, so **all requests require a valid `GATEWAY_API_KEY` Bearer token**; without one the gateway returns `401`.

**Endpoint:** `GET /v1/analytics`

**Authentication:** Required (`GATEWAY_API_KEY`)

## Request

```
GET https://your-gateway.workers.dev/v1/analytics?project_id=proj_abc123
Authorization: Bearer <GATEWAY_API_KEY>
```

### Query Parameters

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `project_id` | string | No | Filter analytics to a single project. Omit for all-project totals. |
| `days` | number | No | Restrict to the last N days (1–365). Omit for all time. |

## Response

```json
{
  "total_requests": 14823,
  "successful_requests": 14201,
  "failed_requests": 622,
  "success_rate": 0.958,
  "providers": {
    "openai": {
      "requests": 6400,
      "successful": 6350,
      "failed": 50
    },
    "anthropic": {
      "requests": 5200,
      "successful": 4980,
      "failed": 220
    }
  },
  "models": {
    "gpt-4o": {
      "requests": 3100,
      "successful": 3080,
      "failed": 20
    }
  }
}
```

### Response Fields

| Field | Type | Description |
|-------|------|-------------|
| `total_requests` | number | Total number of requests handled by the gateway for this project |
| `successful_requests` | number | Requests that returned a 2xx response |
| `failed_requests` | number | Requests that returned a non-2xx response |
| `success_rate` | number | Ratio of successful to total requests (0–1) |
| `providers` | object | Per-provider breakdown of request counts |
| `models` | object | Per-model breakdown of request counts |

## Examples


  
### curl

```bash
curl "https://your-gateway.workers.dev/v1/analytics?project_id=proj_abc123" \
  -H "Authorization: Bearer <GATEWAY_API_KEY>"
```
  
  
### JavaScript

```js
const response = await fetch('https://your-gateway.workers.dev/v1/analytics?project_id=proj_abc123', {
  headers: {
    'Authorization': 'Bearer <GATEWAY_API_KEY>',
  },
});

const stats = await response.json();

console.log(`Total requests: ${stats.total_requests}`);
console.log(`Success rate: ${(stats.success_rate * 100).toFixed(1)}%`);

for (const [provider, data] of Object.entries(stats.providers)) {
  console.log(`${provider}: ${data.requests} requests`);
}
```
