# Renewing Expired Tokens

Access Tokens and ID Tokens are valid for 1 hour. To generate new tokens, you need to use the Refresh Token.

## Renewing Tokens using the SDK

{% hint style="success" %}
Cotter's SDK generally handles this for you. Check the availability below.
{% endhint %}

{% tabs %}
{% tab title="React Native" %}
✅ Cotter's JavaScript SDK (from npm) **automatically renews your `access_token` and `id_token`** whenever there is a valid `refresh_token` in storage.&#x20;

This renewal is called when you call Cotter's Get Access Token function.

```javascript
cotter.tokenHandler.getAccessToken();
```

{% endtab %}

{% tab title="JavaScript (from npm)" %}
✅ Cotter's JavaScript SDK (from npm) **automatically renews your `access_token` and `id_token`** whenever there is a valid `refresh_token` in storage.

This renewal is called when you call [Cotter's Get Access Token function](/sdk-reference/web/getting-access-token-and-logged-in-user-info.md).

```javascript
cotter.tokenHandler.getAccessToken();
```

{% endtab %}

{% tab title="Flutter" %}
✅ Cotter's Flutter SDK **automatically renews your `access_token` and `id_token`** whenever there is a valid `refresh_token` in storage.

This renewal is called when you call [Cotter's Get Access Token function](/sdk-reference/flutter/getting-oauth-tokens.md).

```dart
var accessToken = await cotter.getAccessToken();
```

{% endtab %}

{% tab title="Other SDKs (coming soon)" %}
We'll add support for Android and iOS soon 😉. Stay tuned!
{% endtab %}
{% endtabs %}

## Renewing Tokens using Refresh Token

If Cotter's SDK doesn't support auto renewal, or if you you need to renew the tokens manually, you can make an HTTP request to Cotter's Server to renew the tokens using a `refresh_token`.

```javascript
curl -XPOST \
-H 'API_KEY_ID: <YOUR API KEY ID>' \
-H "Content-type: application/json" \
-d '{
  "grant_type": "refresh_token",
  "refresh_token": "<REFRESH_TOKEN>"
}' 'https://www.cotter.app/api/v0/token/<YOUR API KEY ID>'
```

## Get Token using Refresh Token

<mark style="color:green;">`POST`</mark> `https://www.cotter.app/api/v0/token/<YOUR API KEY ID>`

Getting new `access_token` and `id_token` using `refresh_token`

#### Path Parameters

| Name         | Type   | Description      |
| ------------ | ------ | ---------------- |
| API\_KEY\_ID | string | You `API_KEY_ID` |

#### Headers

| Name         | Type   | Description       |
| ------------ | ------ | ----------------- |
| Content-type | string | application/json  |
| API\_KEY\_ID | string | Your `API_KEY_ID` |

#### Request Body

| Name           | Type   | Description                   |
| -------------- | ------ | ----------------------------- |
| grant\_type    | string | Grant type is `refresh_token` |
| refresh\_token | string | Your `refresh_token`          |

{% tabs %}
{% tab title="200 Returns a new access\_token and id\_token. Does not return a new refresh\_token" %}

```javascript
{
  "access_token": "eyJhbGciOiJFU...",
  "auth_method": "OTP",
  "expires_in": 3600, // expiry in seconds
  "id_token": "eyJhbGciOiJFUzI1N...",
  "refresh_token": "235:s3kjlwkg035...",  // NEW refresh token
  "token_type": "Bearer"
}
```

{% endtab %}
{% endtabs %}

### Refresh Token Rotation

{% hint style="warning" %}
Note that **this does invalidates the old `refresh_token` and return a new `refresh_token`**
{% endhint %}

## **Renewing Expired Refresh Token**

When your `refresh_token` is expired, you have to re-authenticate your user. If you're using Trusted Devices and the current device is a trusted device, you can simply request authentication **silently, in the background by Signing in with Device.**


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cotter.app/getting-access-token/renewing-expired-tokens.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
