Rotate an API key
Create the new key first, switch your services over, check the old key has gone quiet, then revoke it.
API keys don't expire, so you replace them on your own schedule: on a regular cycle, when someone with access leaves, or straight away if a key may have leaked. Keys are created and revoked in the dashboard, and the switch happens in your own systems. Done in the right order, nobody notices.
You need the Manager role to create and revoke keys.
1. Create the new key
- Go to SettingsChannelsAPI and choose Add API.
- Name the key after the same service plus a version, such as
Booking app backend 2. - Under What this key can do, choose the same levels as the old key. Open the old key in another tab to compare.
- Choose Create key.
- Copy the key and put it straight into your secret manager. It's shown only once.
Both keys now work side by side. Each one is a separate credential for the same workspace, with its own rate-limit budget.
2. Switch your services over
Deploy the new key next to the old one, then move each service across. Reading the key from configuration makes this a config change rather than a code change:
CAREFLOW_API_KEY="sk_live_xxx" # old key, still in use
CAREFLOW_API_KEY_NEXT="sk_live_yyy" # new key, deployed
Before a service starts using the new key, check it with a real request:
const res = await fetch("https://api.trycareflow.com/v1/conversations/messages", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.CAREFLOW_API_KEY_NEXT}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
assistantId: process.env.CAREFLOW_AGENT_ID,
externalId: "key-rotation-check",
message: "What time is check-out?",
}),
});
const body = await res.json();
if (body.code !== "SUCCESS") throw new Error(`new key failed: ${body.message}`);
This is a real message, so it shows in your Inbox as a short conversation. If the key can't send messages, check it with a read it's allowed to make instead, such as GET /assistants for a key with agent access. A 403 with insufficient_scope means the new key is missing some of the old key's access.
Once every service reads the new key, rename CAREFLOW_API_KEY_NEXT back to CAREFLOW_API_KEY at your next deploy, or leave it if that's simpler.
3. Check the old key has gone quiet
- Go to SettingsChannelsAPI.
- Find the old key and read its Last used date.
Careflow updates Last used each time a key is accepted. Once every service is on the new key, the old key's date stops moving. Give it a full business day, plus the gap between your least frequent jobs, before you revoke. If the date is still moving, a service you missed is still using it.
4. Revoke the old key
- Go to SettingsChannelsAPI.
- Open the old key and choose Revoke, then confirm.
The key stops working immediately. Any request that still uses it gets 401 with invalid or revoked API key, which is the same answer as a key that never existed. If you see that error after revoking, point the service that sent it at the new key.
If a key has leaked
Reverse the order. Revoke the leaked key first, then create a new one and deploy it. Requests fail in between, but the leaked key can't be used. Then check your Inbox for conversations you don't recognise.
Where to go next
- API keys covers creating and revoking keys in the dashboard.
- Authentication covers how keys are sent and checked.