API key rotation
Replace an API keyAPI keyThe key you create and send with every request that calls a model.More on API keys → with a new one without dropping a request.
- Create the new key. It is shown once: copy it now.
- Move your apps to it.
- Check the new key spends.
- Pause the old key.
- Revoke the old key.
- Rotate from a script.
Rotate on a schedule you set, and at once when a key may have leaked.
Keep a leaked key running while your apps move to the new one. Anyone holding it spends your balanceBalanceWhat you can spend, in DA.More on Balance and charges → until it stops.
Revoke it first, in step 5, then create its replacement. Your apps get a 401 until they carry the new key.
1. Create the new key
In the app, open API Keys and press Create API key. The key is shown once: copy it now.
Give it the old key's name, cap and cap reset. A cap belongs to one key, so the new key starts on its own. API keys sets out each setting.
An account holds at most 5 keys, paused keys counted, unless raised. With 5, revoke a key you no longer use before you create the new one.
2. Move your apps to it
Put the new key everywhere the old one is set: each server's OPENDUNES_API_KEY, your deploy secrets, your .env files. Restart what reads the key only when it starts.
Both keys work while you move, and both spend the same balance. Nothing is dropped: a request carries one key or the other.
3. Check the new key spends
GET /v1/key answers the key that sends it, with what it has spent. Run the sample with OPENDUNES_API_KEY set to the new key: a usage above 0 means your apps send it.
import json
import os
import urllib.request
API_KEY = os.environ["OPENDUNES_API_KEY"]
ADDRESS = "https://opendunes.com/v1/key"
request = urllib.request.Request(
ADDRESS,
headers={"Authorization": "Bearer " + API_KEY},
)
with urllib.request.urlopen(request) as response:
key = json.load(response)["data"]
print("spent:", key["usage"], "today:", key["usage_daily"])Then run it twice with the old key, some minutes apart. When its usage no longer moves, nothing sends it. Reading a key is free.
4. Pause the old key
Open the old key in API Keys and choose Pause this key. Every request it sends gets a 401 from then on.
If an app you forgot starts failing, choose Resume this key, move that app, and pause the key again. Keep it paused until nothing has failed for as long as your slowest job takes to run.
5. Revoke the old key
Open it in API Keys and choose Revoke this key. A revoked key never works again: every request it sends gets a 401.
6. Rotate from a script
With a management keyManagement keyThe key that runs your account through the API: the balance, the usage history, your keys, guardrails, observability. It never calls a model.More on API keys →, the Keys routes run the same rotation without the app. Set OPENDUNES_MANAGEMENT_KEY to it for these calls: an API key gets a 401.
The sample finds the oldest active key named NAME, creates its replacement with the same name, cap, cap reset and storage, and prints the new secret, once.
import json
import os
import urllib.request
MANAGEMENT_KEY = os.environ["OPENDUNES_MANAGEMENT_KEY"]
ADDRESS = "https://opendunes.com/v1/keys"
NAME = "Souk Bot"
HEADERS = {
"Authorization": "Bearer " + MANAGEMENT_KEY,
"Content-Type": "application/json",
}
def call(method, body=None):
data = json.dumps(body).encode() if body is not None else None
request = urllib.request.Request(
ADDRESS, data=data, headers=HEADERS, method=method
)
with urllib.request.urlopen(request) as response:
return json.load(response)
old = next(k for k in call("GET")["data"] if k["name"] == NAME)
created = call("POST", {
"name": NAME,
"limit": old["limit"],
"limit_reset": old["limit_reset"],
"storage": old["storage"],
})
print("old key:", old["hash"])
print("new key:", created["data"]["hash"])
print("secret, shown once:", created["key"])Steps 2 and 3 stay the same. Then the old key's hash pauses it, and deletes it:
| Route | Body | Does |
|---|---|---|
PATCH /v1/keys/{hash} | {"disabled": true} | pauses the key; {"disabled": false} lets it run again |
DELETE /v1/keys/{hash} | none | deletes the key; a request sent with it gets a 401 from then on |
Every key route is free.
Last modified on