Dhan’s Access Tokens are valid for 24 hours, after which they expire. To keep your scripts or trading bots running smoothly, you can automatically renew the token using the /v2/RenewToken endpoint.
This guide explains how to do it using Google Apps Script, which runs directly in the cloud—no local setup required.
Step 1: Prerequisites
You’ll need:
- A Dhan account (log in at web.dhan.co)
- Your Client ID
- A valid Access Token (generate from Dhan Web > Profile > Access DhanHQ APIs)
Tokens last for 24 hours. This script will help you refresh them automatically before expiry.
Step 2: Open Google Apps Script
- Go to https://script.google.com
- Click New Project
- Rename it, e.g., Dhan Token Manager
Step 3: Add Your Credentials
At the top of your Apps Script project, click:
Extensions → Apps Script → Project Settings → Script Properties
Then add the following key-value pairs:
| Property Name | Value (example) |
|---|---|
dhanClientId |
DHAN123456 |
accesstoken |
your 24-hour Access Token from Dhan Web |
Step 4: Add the Script
Copy and paste the following code:
function renewDhanToken() {
const scriptProperties = PropertiesService.getScriptProperties();
const clientId = scriptProperties.getProperty('dhanClientId');
const accessToken = scriptProperties.getProperty('accesstoken');
const url = 'https://api.dhan.co/v2/RenewToken';
const options = {
method: 'GET',
headers: {
'access-token': accessToken,
'dhanClientId': clientId
},
muteHttpExceptions: true
};
try {
const response = UrlFetchApp.fetch(url, options);
const status = response.getResponseCode();
const text = response.getContentText();
Logger.log(`Status Code: ${status}`);
Logger.log(`Response: ${text}`);
if (status === 200) {
const json = JSON.parse(text);
const newToken = json.token;
if (newToken) {
scriptProperties.setProperty('accesstoken', newToken);
Logger.log('✅ Dhan access token renewed and saved successfully.');
return newToken;
} else {
throw new Error('No token field found in response: ' + text);
}
} else {
throw new Error(`RenewToken failed: ${status} - ${text}`);
}
} catch (error) {
Logger.log(`❌ Error renewing token: ${error}`);
throw error;
}
}
Step 5: Run the Script
- Click the
Run button. - Authorize the script the first time it runs.
- Open View → Logs to see the output.
If successful, you’ll see:
✅ Dhan access token renewed and saved successfully.
The new token automatically replaces the old one in your Script Properties.
Step 6: Automate Token Renewal (Optional)
To keep the token always valid, schedule this script to run daily:
- Go to Triggers → Add Trigger
- Choose
renewDhanTokenas the function - Select “Time-driven” event
- Set it to run every 12 hours
Now your Dhan Access Token will renew itself automatically every day.
Notes & Best Practices
- Each renewal invalidates your old token immediately.
- Only call
/RenewTokenwhen necessary (e.g., max twice a day). - Avoid sharing your
access-tokenorclientIdpublicly. - Check logs regularly if using the script in production.
Example Output (from Logger)
Status Code: 200
Response: {"token":"eyJhbGciOi..."}
✅ Dhan access token renewed and saved successfully.
Why Use Google Apps Script?
Because it’s free, cloud-hosted, and easy to integrate with your spreadsheets, bots, or API scripts — no servers or cron jobs needed. You can even connect it with Google Sheets to manage trading data fetched from Dhan APIs.