# Usage history

`GET /v1/activity` answers what your account spent, day by day: one row per day, model and
provider, with its requests, its tokens and its total in DA.

## Request

Send it with a management key: set `OPENDUNES_MANAGEMENT_KEY` to it for this call. An API key
gets a 401. The one field goes in the address, after `?`. [Balance and
charges](/docs/balance-and-charges) says how each charge is made.

| Field | Needed | You send |
|---|---|---|
| `date` | no | one day, `YYYY-MM-DD`, in UTC; absent, the last 30 days |

```python
import json
import os
import urllib.request

MANAGEMENT_KEY = os.environ["OPENDUNES_MANAGEMENT_KEY"]
ADDRESS = "https://opendunes.com/v1/activity"

request = urllib.request.Request(
    ADDRESS,
    headers={"Authorization": "Bearer " + MANAGEMENT_KEY},
)
with urllib.request.urlopen(request) as response:
    rows = json.load(response)["data"]

for row in rows:
    print(row["date"], row["model"], row["requests"], row["usage"])
```

```javascript
const MANAGEMENT_KEY = process.env.OPENDUNES_MANAGEMENT_KEY;
const ADDRESS = "https://opendunes.com/v1/activity";

const response = await fetch(ADDRESS, {
  headers: { Authorization: `Bearer ${MANAGEMENT_KEY}` },
});
const { data } = await response.json();

for (const row of data) {
  console.log(row.date, row.model, row.requests, row.usage);
}
```

```go
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"os"
)

const address = "https://opendunes.com/v1/activity"

func main() {
	managementKey := os.Getenv("OPENDUNES_MANAGEMENT_KEY")

	req, err := http.NewRequest("GET", address, nil)
	if err != nil {
		panic(err)
	}
	req.Header.Set("Authorization", "Bearer "+managementKey)

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()

	var body struct {
		Data []struct {
			Date     string  `json:"date"`
			Model    string  `json:"model"`
			Requests int     `json:"requests"`
			Usage    float64 `json:"usage"`
		} `json:"data"`
	}
	if err := json.NewDecoder(res.Body).Decode(&body); err != nil {
		panic(err)
	}
	for _, row := range body.Data {
		fmt.Println(row.Date, row.Model, row.Requests, row.Usage)
	}
}
```

```bash cURL
ADDRESS="https://opendunes.com/v1/activity"

curl "$ADDRESS" \
  -H "Authorization: Bearer $OPENDUNES_MANAGEMENT_KEY"
```

## Response

`200 OK`. The rows run newest day first; a day with no request has no row.

| Field | Holds |
|---|---|
| `data` | the rows, one per day, model and provider |
| `date` | the day, `YYYY-MM-DD`, in UTC |
| `model` | the model, `lab/model` |
| `model_permaslug` | the same model, under the same name |
| `endpoint_id` | the provider endpoint that served these requests |
| `provider_name` | the provider that served them |
| `prompt_tokens` | every token sent that day, to that model, through that provider |
| `completion_tokens` | every token produced |
| `requests` | how many requests |
| `usage` | what they were charged, in DA |

## What it cost

Free. Reading your usage history is never charged.

## Errors

`error.code` carries the status, and `error.message` says why.

| Status | When |
|---|---|
| 400 | `date` is not a day written `YYYY-MM-DD` |
| 401 | the key is missing, wrong, revoked or expired, or it is an API key |
