---
title: "Rotate an API key"
description: "New secret shown once."
canonical_url: "https://twominutebilling.com/help/developers/api-reference/organization/rotate-an-api-key"
---
# Rotate an API key

> New secret shown once.

<api-operation method="POST" path="/v1/api_keys/{id}/rotate">
<template v-slot:docs="">

Creates a new key with the same name, mode and scope, and returns its secret once. The old key keeps working for `overlap_seconds`, then expires; its expiry never moves later than it already was. A retry with the same Idempotency-Key does not create a second key and does not return the secret again (409 `response_not_replayable`). A revoked or expired key cannot be rotated (400).

## Path parameters

<table>
<thead>
  <tr>
    <th>
      Parameter
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Required
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        id
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <strong>
        yes
      </strong>
    </td>
    
    <td>
      The api key's id.
    </td>
  </tr>
</tbody>
</table>

## Headers

<table>
<thead>
  <tr>
    <th>
      Parameter
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Required
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        Idempotency-Key
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      no
    </td>
    
    <td>
      Safely retry writes — repeats return the original response. If a write FAILED after it had already taken effect, that failure is what repeats: the key is spent, and a new attempt needs a new key.
    </td>
  </tr>
</tbody>
</table>

## Body

The request body is optional.

<table>
<thead>
  <tr>
    <th>
      Field
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Required
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        overlap_seconds
      </code>
    </td>
    
    <td>
      <code>
        integer
      </code>
    </td>
    
    <td>
      no
    </td>
    
    <td>
      How long the old key keeps working, in seconds: 0 (stop now) to 604800 (7 days). Defaults to <code>
        86400
      </code>
      
      .
    </td>
  </tr>
</tbody>
</table>

## Response

<table>
<thead>
  <tr>
    <th>
      Field
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        object
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      The object type.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        id
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      The api key's id (key_…).
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        name
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      The key's name.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        prefix
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      The first characters of the key, so you can recognize it. The full key is shown only once, when you create it.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        livemode
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        live
      </code>
      
       for a live key, <code>
        test
      </code>
      
       for a test key. One of <code>
        test
      </code>
      
      , <code>
        live
      </code>
      
      , <code>
        all
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        scope
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      What the key can do: <code>
        read
      </code>
      
       or <code>
        write
      </code>
      
      . One of <code>
        read
      </code>
      
      , <code>
        write
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        secret
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Returned once at creation only.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        last_used_at
      </code>
    </td>
    
    <td>
      <code>
        string · ISO-8601
      </code>
    </td>
    
    <td>
      When the key was last used (ISO-8601 UTC), or null.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        created_at
      </code>
    </td>
    
    <td>
      <code>
        string · ISO-8601
      </code>
    </td>
    
    <td>
      When the api key was created (ISO-8601 UTC).
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        revoked_at
      </code>
    </td>
    
    <td>
      <code>
        string · ISO-8601
      </code>
    </td>
    
    <td>
      When the key was revoked (ISO-8601 UTC), or null.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        expires_at
      </code>
    </td>
    
    <td>
      <code>
        string · ISO-8601
      </code>
    </td>
    
    <td>
      When the key stops working (ISO-8601 UTC), or null if it never expires.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        permissions
      </code>
    </td>
    
    <td>
      <code>
        object
      </code>
    </td>
    
    <td>
      The key's permission map. Null: the key works by its scope.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        previous_key
      </code>
    </td>
    
    <td>
      <code>
        object
      </code>
    </td>
    
    <td>
      The rotated key, with the expiry the rotation gave it.
    </td>
  </tr>
</tbody>
</table>

## Status codes

<table>
<thead>
  <tr>
    <th>
      Status
    </th>
    
    <th>
      Meaning
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        201
      </code>
    </td>
    
    <td>
      The new key, with its secret, and the old key with its new expiry
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        400
      </code>
    </td>
    
    <td>
      Invalid overlap, or the key is revoked or expired
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        404
      </code>
    </td>
    
    <td>
      No such API key
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        409
      </code>
    </td>
    
    <td>
      Idempotency-Key conflict — the key is already in flight, or was reused with a different request payload.
    </td>
  </tr>
</tbody>
</table>
</template>

<template v-slot:request="">

```bash [Request]
curl -X POST "$TMBILLING_URL/v1/api_keys/key_XXXXXXXXXXXX01/rotate" \
  -H "Authorization: Bearer $TMBILLING_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{
    "overlap_seconds": 86400
  }'
```

</template>

<template v-slot:response="">
<code-group>

```json [201 Created]
{
  "object": "api_key",
  "id": "key_XXXXXXXXXXXX01",
  "name": "Production billing sync",
  "prefix": "sk_test_AbCd",
  "livemode": "test",
  "scope": "read",
  "secret": "whsec_XXXXXXXXXXXX01",
  "last_used_at": "2026-03-14T09:30:00Z",
  "created_at": "2026-03-14T09:30:00Z",
  "revoked_at": "2026-03-14T09:30:00Z",
  "expires_at": "2026-04-14T09:30:00Z",
  "permissions": {},
  "previous_key": {
    "object": "api_key",
    "id": "key_XXXXXXXXXXXX01",
    "name": "Production billing sync",
    "prefix": "sk_test_AbCd",
    "livemode": "test",
    "scope": "read",
    "secret": "whsec_XXXXXXXXXXXX01",
    "last_used_at": "2026-03-14T09:30:00Z",
    "created_at": "2026-03-14T09:30:00Z",
    "revoked_at": "2026-03-14T09:30:00Z",
    "expires_at": "2026-04-14T09:30:00Z",
    "permissions": {}
  }
}
```

```json [400 Bad Request]
{
  "error": {
    "type": "invalid_request_error",
    "message": "Invalid overlap, or the key is revoked or expired"
  }
}
```

```json [404 Not Found]
{
  "error": {
    "type": "invalid_request_error",
    "message": "No such API key"
  }
}
```

```json [409 Conflict]
{
  "error": {
    "type": "idempotency_error",
    "message": "Idempotency-Key conflict — the key is already in flight, or was reused with a different request payload."
  }
}
```

</code-group>
</template>
</api-operation>


## Sitemap

See the full [sitemap](https://twominutebilling.com/sitemap.md) for all pages.
