> For the complete documentation index, see [llms.txt](https://docs.iproyal.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.iproyal.com/scraping-tools/web-scraper-api-beta/api/token-regenerate.md).

# Token Regenerate

Replaces your API token with a new one.

```
POST /v1/token/regenerate
```

{% hint style="warning" %}
Your current token stops working as soon as this request succeeds. There is no grace period. New requests with the old token fail with `401 invalid_token`.
{% endhint %}

### Request

No body. Authenticate with the token you want to replace.

{% tabs %}
{% tab title="cURL" %}

```bash
curl -X POST https://scrape.iproyal.com/v1/token/regenerate \
  -H "Authorization: Bearer $WEB_SCRAPER_API_TOKEN"
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php

$ch = curl_init('https://scrape.iproyal.com/v1/token/regenerate');

curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('WEB_SCRAPER_API_TOKEN'),
    ],
]);

$body = json_decode(curl_exec($ch), true);

echo $body['api_token'] . PHP_EOL;
```

{% endtab %}

{% tab title="Python" %}

```python
import os

import requests

response = requests.post(
    "https://scrape.iproyal.com/v1/token/regenerate",
    headers={"Authorization": f"Bearer {os.environ['WEB_SCRAPER_API_TOKEN']}"},
    timeout=30,
)
response.raise_for_status()

print(response.json()["api_token"])
```

{% endtab %}

{% tab title="Node.js" %}

```javascript
const response = await fetch('https://scrape.iproyal.com/v1/token/regenerate', {
  method: 'POST',
  headers: { Authorization: `Bearer ${process.env.WEB_SCRAPER_API_TOKEN}` },
  signal: AbortSignal.timeout(30_000),
});

const { api_token: newToken } = await response.json();

console.log(newToken);
```

{% endtab %}

{% tab title="Java" %}

```java
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;

public class RegenerateTokenExample {
    public static void main(String[] args) throws Exception {
        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("https://scrape.iproyal.com/v1/token/regenerate"))
            .header("Authorization", "Bearer " + System.getenv("WEB_SCRAPER_API_TOKEN"))
            .timeout(Duration.ofSeconds(30))
            .POST(HttpRequest.BodyPublishers.noBody())
            .build();

        HttpResponse<String> response = HttpClient.newHttpClient()
            .send(request, HttpResponse.BodyHandlers.ofString());
        String newToken = new ObjectMapper().readTree(response.body()).get("api_token").asText();

        System.out.println(newToken);
    }
}
```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"time"
)

func main() {
	req, _ := http.NewRequest(http.MethodPost, "https://scrape.iproyal.com/v1/token/regenerate", nil)
	req.Header.Set("Authorization", "Bearer "+os.Getenv("WEB_SCRAPER_API_TOKEN"))

	client := &http.Client{Timeout: 30 * time.Second}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("request failed:", err)
		return
	}
	defer resp.Body.Close()

	var body struct {
		APIToken string `json:"api_token"`
	}
	if err := json.NewDecoder(resp.Body).Decode(&body); err != nil || body.APIToken == "" {
		fmt.Println("unexpected response, HTTP", resp.StatusCode)
		return
	}

	fmt.Println(body.APIToken)
}
```

{% endtab %}

{% tab title="C#" %}

```csharp
using System.Net.Http.Headers;
using System.Text.Json;

using var client = new HttpClient { Timeout = TimeSpan.FromSeconds(30) };
client.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("WEB_SCRAPER_API_TOKEN"));

var response = await client.PostAsync("https://scrape.iproyal.com/v1/token/regenerate", null);
response.EnsureSuccessStatusCode();

using var body = JsonDocument.Parse(await response.Content.ReadAsStringAsync());

Console.WriteLine(body.RootElement.GetProperty("api_token").GetString());
```

{% endtab %}
{% endtabs %}

### Response

```json
{
  "api_token": "033d1ef78e0c7abb0130892b2471ef951e379f38378c3a5ff6b7649caeae"
}
```

| Field       | Type   | Description        |
| ----------- | ------ | ------------------ |
| `api_token` | string | Your new API token |

### After You Regenerate

1. Save the new token in your secrets storage.
2. Update every application that uses the old token.
3. Restart long-running jobs so they read the new value.

Your credit balance, limits, and usage history stay the same. Requests already running when you regenerate are not affected.

You can also regenerate your token in the IPRoyal dashboard. If a token leaks, regenerate it right away.

### Errors

| HTTP Status | `error.code`                              |
| ----------- | ----------------------------------------- |
| `401`       | `invalid_token`                           |
| `403`       | `account_suspended` or `account_disabled` |

A suspended or disabled account cannot regenerate its token. Contact IPRoyal Support in that case.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.iproyal.com/scraping-tools/web-scraper-api-beta/api/token-regenerate.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
