# Your account through a management key

Run your account from a script: read the balance, then manage your keys.

1. [Make a management key](#1-make-a-management-key).
2. [Read your balance](#2-read-your-balance).
3. [List and create API keys](#3-list-and-create-api-keys).
4. [See what else it runs](#4-see-what-else-it-runs).

## 1. Make a management key

Create one in your dashboard, under *Settings* then *Management keys*. Its secret shows once: put it in the environment as `OPENDUNES_MANAGEMENT_KEY` and keep it out of the code.

<div class="buttons">
<a href="/dashboard/settings/management-keys" class="button">Management keys</a>
</div>

A management key runs the account and never calls a model. An API key is the reverse: it calls models and gets a 401 on every account route in this guide. Keep one of each, so a script that reads the balance cannot spend it. The two compared: [API keys](/docs/api-keys#a-management-key).

<div class="watch-out">
<p>A management key's expiry is fixed when you create it, and it cannot be changed after. Choose the span with the script in mind, and make a new key before the old one runs out.</p>
</div>

## 2. Read your balance

`GET /v1/credits` answers two totals in DA: what came into your wallet, and what requests spent. This sample prints what is left, as Souk Bot's owner in Oran would check it each morning.

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

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

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

left = totals["total_credits"] - totals["total_usage"]
print("DA left:", left)
```

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

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

console.log("DA left:", data.total_credits - data.total_usage);
```

```go
package main

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

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

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 {
			TotalCredits float64 `json:"total_credits"`
			TotalUsage   float64 `json:"total_usage"`
		} `json:"data"`
	}
	if err := json.NewDecoder(res.Body).Decode(&body); err != nil {
		panic(err)
	}
	left := body.Data.TotalCredits - body.Data.TotalUsage
	fmt.Println("DA left:", left)
}
```

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

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

The answer holds `total_credits` and `total_usage` under `data`, both in DA. Your balance is the first minus the second. Reading it is free, however often a script does.

<div class="good-to-know">
<p>Every account route is free to call. Running a model is what is billed, and a management key cannot.</p>
</div>

## 3. List and create API keys

The same key manages your API keys. `GET /v1/keys` lists them, 100 to a page, oldest first. `POST /v1/keys` creates one from a `name`, and answers its secret once, in `key`.

| Do | Send |
|---|---|
| list keys | `GET /v1/keys`, with `include_disabled=true` to list paused keys too |
| create a key | `POST /v1/keys` with a `name`, and if you want one a cap, a reset and an expiry |
| pause or change a key | `PATCH /v1/keys/{hash}` |
| delete a key | `DELETE /v1/keys/{hash}`: a request sent with it gets a 401 from then on |

The fields of each route, with a sample: [Keys](/docs/reference/keys).

<div class="do">
<p>Create one API key for each app, with its own cap, so a runaway loop in one app stops at its cap and the others keep working.</p>
</div>

<div class="dont">
<p>Don't put the management key in a browser page or a mobile app. It can create and delete keys; anyone who reads it runs your account.</p>
</div>

## 4. See what else it runs

| To | Use | Reference |
|---|---|---|
| read the balance | `GET /v1/credits` | [Balance](/docs/reference/credits) |
| read the usage history | `GET /v1/activity` | [Usage history](/docs/reference/activity) |
| manage API keys | `/v1/keys` | [Keys](/docs/reference/keys) |
| write rules for what a request may do | `/v1/guardrails` | [Guardrail rules](/docs/reference/guardrails) |
| send traces to your own tool | `/v1/observability/destinations` | [Trace destinations](/docs/reference/observability) |
| share a wallet | `/v1/workspaces` | [Workspaces](/docs/reference/workspaces) |

## Errors

The body says why. What to do on every status: [Errors and retries](/docs/errors-and-retries).

| Status | Means |
|---|---|
| 401 | the management key is missing, wrong, revoked or expired, or it is an API key |
| 404 | no key, rule, destination or workspace of yours has that address |
