> This page is for APIs.

> Fetch clean Markdown by appending `.md` to any page URL under https://signalwire.com/docs or requesting it with the HTTP header `Accept: text/markdown`. The root index at https://signalwire.com/docs/llms.txt lists the available documentation indexes.

# Regenerate API token

POST https://%7BYour_Space_Name%7D.signalwire.com/api/project/tokens/{token_id}/regenerate

Replaces a project API token's value and returns the new value. Use it to rotate a credential on a schedule, after it has been exposed, or when its value has been lost. The token keeps its `id`, `name`, and `permissions`, so there is nothing to re-enter; to change those, use [Update API token](/docs/apis/rest/project-tokens/update-token).

The previous value stops working immediately, on every API that accepts a project API token. Anything still using it receives `401 Unauthorized` until you give it the new value.

The response is the only place the new value is returned; store it securely. If you lose it, regenerate the token again.

The new value always uses the current token format: 42 characters beginning with `swapi_`. A token issued in the legacy format, 50 characters beginning with `PT`, changes format and length when regenerated, so update anything that validates or stores token values by length or prefix.

You can regenerate the token you are authenticating with. The request succeeds, but the value you sent is no longer valid once it does, so save the new value from the response before making any further request.

A token the caller cannot reach returns `404 Not Found`. Use [List API tokens](/docs/apis/rest/project-tokens/list-tokens) to find the `id` of each token you can manage.

#### Permissions

With a project API token, the token must have the following scope(s) enabled to make a successful request: _Management_.

With a [Personal access token](/docs/apis/authorization#personal-access-tokens) there are no scopes. The token holder must be an owner or admin of the space, and the request reaches only the projects the holder is enabled for, plus their subprojects.

[Learn more about API scopes](/docs/platform/your-signalwire-api-space).

Reference: https://signalwire.com/docs/apis/rest/project-tokens/regenerate-token

## Authentication

- `Authorization` header (basic auth, required) — SignalWire Basic Authentication using Project ID and API Token. The client sends HTTP requests with the Authorization header containing the word Basic followed by a space and a base64-encoded string of project_id:token. The project ID will be used as the username and the API token as the password. Example: ``` Authorization: Basic base64(project_id:token) ```
- `Authorization` header (basic auth, required) — Personal access token authentication for space-wide administration. Send HTTP Basic auth with an empty username and the Personal access token as the password. A Personal access token carries your own authority rather than a project's: it is created from your user menu in the Dashboard, is prefixed `pat_`, and has no scopes. The holder must be an owner or admin of the space named by the subdomain, and the token acts only on that space. Example: ``` Authorization: Basic base64(:pat_...) ```

## Request

### Path parameters

- `token_id` (string, required) — The unique identifier of the token to regenerate.

## Response

### 200

The request has succeeded.

- `id` (string, required) — The ID of the API Token.
- `name` (string, required, nullable) — The name of the API Token. `null` when the token has no name.
- `permissions` (list of enum, required) — The permissions enabled for this token.
  - Allowed values: `calling`, `chat`, `datasphere`, `fax`, `management`, `messaging`, `numbers`, `pubsub`, `storage`, `tasking`, `video`
- `last_used_at` (datetime, required, nullable) — The date and time the token last authenticated a request. `null` if it never has.
- `revoked_at` (datetime, required, nullable) — The date and time SignalWire revoked the token's value, for example after detecting that the value was exposed. `null` if it has not been revoked. A revoked token keeps its `id` and still appears in [List API tokens](/docs/apis/rest/project-tokens/list-tokens), but its previous value no longer authenticates and no replacement value has been issued to you. To get a working value, [regenerate the token](/docs/apis/rest/project-tokens/regenerate-token), which also clears `revoked_at`.
- `token` (string, required) — The API token that can be used along with the project ID for basic authentication. It is returned only when the token is created or regenerated and cannot be retrieved again; store it securely.

## Errors

### 401 Unauthorized Error

Access is unauthorized.

- `error` (enum, required)
  - Allowed values: `Unauthorized`

### 404 Not Found Error

The server cannot find the requested resource.

- `error` (enum, required)
  - Allowed values: `Not Found`

### 500 Internal Server Error

An internal server error occurred.

- `error` (enum, required)
  - Allowed values: `Internal Server Error`

## Examples

**Response**

```json
{
  "id": "ea14556a-984f-11ee-b9d1-0242ac120002",
  "name": "John Doe's Token",
  "permissions": [
    "calling",
    "fax",
    "messaging"
  ],
  "last_used_at": "2024-05-06T12:20:00Z",
  "revoked_at": null,
  "token": "swapi_0123456789ab0123456789ab0123456789ab"
}
```

**SDK Code**

```python
import requests

url = "https://{your_space_name}.signalwire.com/api/project/tokens/token_id/regenerate"

response = requests.post(url, auth=("<project_id>", "<api_token>"))

print(response.json())
```

```javascript
const url = 'https://{your_space_name}.signalwire.com/api/project/tokens/token_id/regenerate';
const credentials = btoa("<project_id>:<api_token>");

const options = {method: 'POST', headers: {Authorization: `Basic ${credentials}`}};

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://{your_space_name}.signalwire.com/api/project/tokens/token_id/regenerate"

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

	req.SetBasicAuth("<project_id>", "<api_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://{your_space_name}.signalwire.com/api/project/tokens/token_id/regenerate")

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

request = Net::HTTP::Post.new(url)
request.basic_auth("<project_id>", "<api_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://{your_space_name}.signalwire.com/api/project/tokens/token_id/regenerate")
  .basicAuth("<project_id>", "<api_token>")
  .asString();
```

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

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://{your_space_name}.signalwire.com/api/project/tokens/token_id/regenerate', [
  'headers' => [
  ],
    'auth' => ['<project_id>', '<api_token>'],
]);

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

```csharp
using RestSharp;
using RestSharp.Authenticators;

var client = new RestClient("https://{your_space_name}.signalwire.com/api/project/tokens/token_id/regenerate");
client.Authenticator = new HttpBasicAuthenticator("<project_id>", "<api_token>");
var request = new RestRequest(Method.POST);

IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let credentials = Data("<project_id>:<api_token>".utf8).base64EncodedString()

let headers = ["Authorization": "Basic \(credentials)"]

let request = NSMutableURLRequest(url: NSURL(string: "https://{your_space_name}.signalwire.com/api/project/tokens/token_id/regenerate")! 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()
```