> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.getdial.ai/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.getdial.ai/_mcp/server.

# Rotate an API key

POST https://api.getdial.ai/api/v1/account/api-keys/{id}/rotate

Replaces the secret of an API key with a newly generated one. The old value stops working immediately; there is no grace period, so update every place that uses the key right after rotating it. The key keeps its `id`, `label`, and role, and signing in afterwards returns the new value.
The new key is returned once, in `apiKey`. Store it: later reads only show a `preview`.
Owner keys can rotate any owner key on the account. A member key can rotate only that member's own key. Find a key's `id` with [Get the current account](#operation/getAccount); the key making the request is marked `current: true`.

Reference: https://docs.getdial.ai/api-reference/rest-api/account/rotate-api-key

## Authentication

- `Authorization` header (bearer token, required) — Your Dial API key, sent as `Authorization: Bearer sk_live_...`

## Request

### Path parameters

- `id` (string, required)

## Response

### 200

The key was rotated. `apiKey` holds the new value and is shown only here.

- `id` (string, required)
- `apiKey` (string, required) — The new key value. Returned only in this response; store it now.
- `preview` (string, required) — First characters of the new key, as later reads will show it.
- `createdAt` (datetime, required) — When the key was first created. Rotation does not change it.
- `rotatedAt` (datetime, required) — When this rotation happened.
- `label` (string, optional, nullable)

## Errors

### 401 Unauthorized Error

Missing or invalid API key.

- `error` (ErrorError, optional) — An error message, or a validation-error object for 400 responses.

### 403 Forbidden Error

The key belongs to someone else on the account (a member rotating another person's key, or an owner rotating a member's key; remove the member instead).

- `error` (ErrorError, optional) — An error message, or a validation-error object for 400 responses.

### 404 Not Found Error

The requested resource was not found on this account.

- `error` (ErrorError, optional) — An error message, or a validation-error object for 400 responses.

## Types

### ErrorError

An error message, or a validation-error object for 400 responses.

- string
- map from string to any

## Examples

**Response**

```json
{
  "id": "string",
  "apiKey": "sk_live_Zx9...",
  "preview": "sk_live_Zx9abc...",
  "createdAt": "2024-01-15T09:30:00Z",
  "rotatedAt": "2024-01-15T09:30:00Z",
  "label": "string"
}
```

**SDK Code**

```python
import requests

url = "https://api.getdial.ai/api/v1/account/api-keys/id/rotate"

headers = {"Authorization": "Bearer <token>"}

response = requests.post(url, headers=headers)

print(response.json())
```

```javascript
const url = 'https://api.getdial.ai/api/v1/account/api-keys/id/rotate';
const options = {method: 'POST', headers: {Authorization: 'Bearer <token>'}};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go
package main

import (
	"fmt"
	"net/http"
	"io"
)

func main() {

	url := "https://api.getdial.ai/api/v1/account/api-keys/id/rotate"

	req, _ := http.NewRequest("POST", url, nil)

	req.Header.Add("Authorization", "Bearer <token>")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby
require 'uri'
require 'net/http'

url = URI("https://api.getdial.ai/api/v1/account/api-keys/id/rotate")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'

response = http.request(request)
puts response.read_body
```

```java
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.getdial.ai/api/v1/account/api-keys/id/rotate")
  .header("Authorization", "Bearer <token>")
  .asString();
```

```php
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.getdial.ai/api/v1/account/api-keys/id/rotate', [
  'headers' => [
    'Authorization' => 'Bearer <token>',
  ],
]);

echo $response->getBody();
```

```csharp
using RestSharp;

var client = new RestClient("https://api.getdial.ai/api/v1/account/api-keys/id/rotate");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer <token>");
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = ["Authorization": "Bearer <token>"]

let request = NSMutableURLRequest(url: NSURL(string: "https://api.getdial.ai/api/v1/account/api-keys/id/rotate")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```