# Get New Access Token Using Existing Refresh Token

Endpoint Name: `getaccesstokenfromrefreshtoken`

Request type : `POST`

Authorization : Bearer Token as header

<mark style="color:red;">Note: Please call this endpoint at certain interval before the token expiration time, 10 mins will be an appropriate amount of  time as per our implementation. An expired access token can give 401 unauthorized error.</mark>

Endpoint Details: The `getaccesstokenfromrefreshtoken` endpoint will be used to get access token from refresh token.

<table data-header-hidden><thead><tr><th width="116"></th><th></th></tr></thead><tbody><tr><td>Method</td><td>URL</td></tr><tr><td>POST</td><td><code>URL/access/klearstack/getaccesstokenfromrefreshtoken</code></td></tr></tbody></table>

| Parameters     | Description                                                          | Required or Optional |
| -------------- | -------------------------------------------------------------------- | -------------------- |
| refresh\_token | Refresh token must be passed in request body to get new access token | Required             |
| company\_name  | Name of the company for which the user is logging in                 | Required             |

## Request Code samples

### Shell

* Request Body

```sh
curl --location POST URL/access/klearstack/getaccesstokenfromrefreshtoken \
  --header "Authorization: Bearer YOUR_EXISTING_ACCESS_TOKEN" \
  --form "refresh_token=Replace_Refresh_Token_Here" \
  --form "company_name=Replace_Company_Name_Here"
```

### Python

* Request Body

```python
import requests

url = "URL/access/klearstack/getaccesstokenfromrefreshtoken"
headers = {
    "Authorization": "Bearer YOUR_EXISTING_ACCESS_TOKEN",
    "Content-Type": "application/x-www-form-urlencoded"
}
data = {
    'refresh_token': 'Replace_Refresh_Token_Here',
    'company_name': 'Replace_Company_Name_Here'
}

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

print(response.json())
```

### Java

* Request Body

```java
import okhttp3.*;

import java.io.IOException;

public class ApiRequestExample {

    public static void main(String[] args) {
        OkHttpClient client = new OkHttpClient();

        // Form data (refresh_token, company_name)
        RequestBody formBody = new FormBody.Builder()
        .add("refresh_token", "Replace_Refresh_Token_Here")
        .add("company_name", "Replace_Company_Name_Here")
        .build();

        Request request = new Request.Builder()
        .url("URL/access/klearstack/getaccesstokenfromrefreshtoken")
        .post(formBody)
        .addHeader("Authorization", "Bearer YOUR_EXISTING_ACCESS_TOKEN")
        .addHeader("Content-Type", "application/x-www-form-urlencoded")
        .build();

        // Execute the request and handle the response
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                System.out.println("Response: " + response.body().string());
            } else {
                System.out.println("Error: " + response.body().string());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
```

### Node.js

* Request Body

```javascript
var request = require('request');

const form_data = {
  'refresh_token': 'Replace_Refresh_Token_Here',
  'company_name': 'Replace_Company_Name_Here'
};

const options = {
  url: 'URL/access/klearstack/getaccesstokenfromrefreshtoken',
  form: form_data,
  headers: {
    'Authorization': 'Bearer YOUR_EXISTING_ACCESS_TOKEN',
    'Content-Type': 'application/x-www-form-urlencoded'
  }
};

request.post(options, function(err, httpResponse, body) {
  if (err) {
    console.error('Request failed:', err);
    return;
  }

  console.log('Response:', body);
});
```

### JavaScript (XMLHttpRequest)

* Request Body

```javascript
var data = new FormData();
data.append('refresh_token', 'Replace_Refresh_Token_Here');
data.append('company_name', 'Replace_Company_Name_Here');

var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function () {
    if (this.readyState === this.DONE) {
        console.log(this.responseText);
    }
});

xhr.open("POST", "URL/access/klearstack/getaccesstokenfromrefreshtoken");
xhr.setRequestHeader("Authorization", "Bearer YOUR_EXISTING_ACCESS_TOKEN");
xhr.send(data);
```

### API Response

<table data-header-hidden><thead><tr><th width="138"></th><th></th></tr></thead><tbody><tr><td>Status code</td><td>Example Response</td></tr><tr><td>200</td><td><p><code>{</code></p><p><code>"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjU2NTc2MDM4LCJpYTY1NzU5MTgsImp0aSI6IjQwMWZlYmJhODg4NDQ1MmRhN2Q3ZTA5ZjEwZmYwOWFkIiwidXNlcl9pZCI6MjZ9.sjU6iVbRRp7dTDt-l4rj7K7tKpPpaL7yskjstAUn2Zs",</code></p><p><code>"release_version": “7.8.9”</code></p><p><code>}</code></p></td></tr><tr><td>500</td><td>One or Many Required parameters from company name must be missing, E.g error “refresh_token”,</td></tr></tbody></table>
