Skip to content

Commit b3e776f

Browse files
authored
Document the API key rotate endpoint (#413)
Replace the manual create-then-delete rotation steps with the rotate endpoint, covering days_to_expire, the expire_in_days grace window, and the related troubleshooting cases.
1 parent ab3c134 commit b3e776f

1 file changed

Lines changed: 75 additions & 8 deletions

File tree

info/api-keys.mdx

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,85 @@ func main() {
212212

213213
## Rotate a key
214214

215-
Rotate by creating the replacement first, then deleting the old key after your workload has switched over.
215+
`rotate` issues a replacement key in a single call and keeps the old key working for a short grace period, so your workload can switch over without downtime. The new key copies the rotated key's name and project scope, and—like create—Kernel returns the plaintext `key` only once.
216216

217-
1. Create a new API key with the same scope.
218-
2. Store the new plaintext key in your secret manager.
219-
3. Deploy or restart the workload that uses `KERNEL_API_KEY`.
220-
4. Verify the workload can call Kernel.
221-
5. Delete the old API key.
217+
Two optional parameters control the timing:
218+
219+
- `days_to_expire` sets the new key's lifetime in days (`1`-`3650`). Omit it to give the new key the same lifetime the rotated key originally had, or to never expire if the old key never did.
220+
- `expire_in_days` sets how long the old key keeps working before it expires. Use `0` to revoke it immediately, or omit it for the default 7-day grace window. The old key stops authenticating automatically once the window passes—you don't need to delete it.
221+
222+
<CodeGroup>
223+
```typescript TypeScript
224+
const rotated = await kernel.apiKeys.rotate('key_01jwv4tn5m8k3q2v7x9p0a1bc2', {
225+
days_to_expire: 30,
226+
expire_in_days: 7,
227+
});
228+
229+
console.log(rotated.key); // Save this value now. Kernel won't show it again.
230+
console.log(rotated.id, rotated.masked_key);
231+
```
232+
233+
```python Python
234+
import os
235+
from kernel import Kernel
236+
237+
client = Kernel(api_key=os.environ["KERNEL_API_KEY"])
238+
239+
rotated = client.api_keys.rotate(
240+
"key_01jwv4tn5m8k3q2v7x9p0a1bc2",
241+
days_to_expire=30,
242+
expire_in_days=7,
243+
)
244+
245+
print(rotated.key) # Save this value now. Kernel won't show it again.
246+
print(rotated.id, rotated.masked_key)
247+
```
248+
249+
```go Go
250+
package main
251+
252+
import (
253+
"context"
254+
"fmt"
255+
256+
"github.com/kernel/kernel-go-sdk"
257+
)
258+
259+
func main() {
260+
ctx := context.Background()
261+
client := kernel.NewClient()
262+
263+
rotated, err := client.APIKeys.Rotate(
264+
ctx,
265+
"key_01jwv4tn5m8k3q2v7x9p0a1bc2",
266+
kernel.APIKeyRotateParams{
267+
DaysToExpire: kernel.Int(30),
268+
ExpireInDays: kernel.Int(7),
269+
},
270+
)
271+
if err != nil {
272+
panic(err)
273+
}
274+
275+
fmt.Println(rotated.Key) // Save this value now. Kernel won't show it again.
276+
fmt.Println(rotated.ID, rotated.MaskedKey)
277+
}
278+
```
279+
</CodeGroup>
280+
281+
After you rotate a key:
282+
283+
1. Store the new plaintext key in your secret manager.
284+
2. Deploy or restart the workload that uses `KERNEL_API_KEY`.
285+
3. Verify the workload can call Kernel before the grace window ends.
286+
287+
To cut over immediately instead of using a grace window, pass `expire_in_days: 0` so the old key stops working as soon as the new one is issued.
222288

223289
## Troubleshooting
224290

225291
| Error | What it means | What to do |
226292
| --- | --- | --- |
227-
| `400 Bad Request` | The name is missing, `days_to_expire` is outside `1`-`3650`, or `project_id` is empty. | Send a name, choose a valid expiry, or omit `project_id` for an org-scoped key. |
293+
| `400 Bad Request` | The name is missing, `days_to_expire` is outside `1`-`3650`, `expire_in_days` is outside `0`-`3650`, or `project_id` is empty. | Send a name, choose a valid expiry, or omit `project_id` for an org-scoped key. |
294+
| `400 Bad Request` (rotate) | `days_to_expire` is shorter than `expire_in_days`, so the new key would expire before the old key's grace window ends. | Raise `days_to_expire` or lower `expire_in_days`. |
228295
| `401 Unauthorized` | Kernel couldn't authenticate the request. | Set a valid `KERNEL_API_KEY`. |
229-
| `404 Not Found` | The project doesn't exist or the caller can't access it. | Check the project ID. If you're using a project-scoped key, create keys only for that same project. |
296+
| `404 Not Found` | The project or API key doesn't exist, or the caller can't access it. | Check the ID. If you're using a project-scoped key, you can only rotate keys in that same project. |

0 commit comments

Comments
 (0)