I am having trouble accessing my portfolio data using the Dhan Trading APIs through Google Apps Script.
I cannot fetch my portfolio data and consistently receive a “404 Not Found” error.
I have already thoroughly reviewed the (Introduction - DhanHQ Ver 2.0 / API Document), and tried several troubleshooting steps, including:
Verifying the accuracy of my API credentials (Access Token)
Ensuring the correct API endpoint is being used (https://api.dhan.co/v2/portfolio)
The screenshot below shows the code along with the execution log, which includes the detailed error message and other relevant information. (For security reasons, I have put My_Access_Token as a placeholder both in the code snippet and execution log.)
I would be grateful if anyone could have any insight into it.
Hi @Hardik
Thanks for chiming in and suggesting using the /v2/holdings endpoint! I really appreciate you taking the time to help.
Unfortunately, I’m still running into an error, but it’s different.
This time, it is a 400 Bad Request with the message “Missing required fields, bad values for parameters, etc.” I double-checked the documentation, and it seems like this endpoint shouldn’t need any parameters for a simple GET request.
Here’s the code I’m using in Google Apps Script:
JavaScript
function fetchDhanPortfolio() {
// Access Token hidden
const accessToken = 'MY_NEW_ACCESS_TOKEN';
// Dhan API endpoint for holdings
const apiUrl = 'https://api.dhan.co/v2/holdings';
const options = {
'method': 'get',
'headers': {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
'muteHttpExceptions': false
};
const response = UrlFetchApp.fetch(apiUrl, options);
// ... (rest of the code for processing the response) ...
}
I tried removing the Bearer prefix as you suggested and also added the Content-Type header, but I’m still getting the same “DH-905” error (Please see the revised code and Execution Log below).
I’m starting to think it might be something specific to my Dhan account or setup. I’ve double-checked everything on my end and can’t seem to find any issues.
Could it be related to any account-specific settings or permissions? I’ve already verified my Dhan account, and I do have active holdings and positions in my Dhan account.
I’ve also reached out to Dhan support, but I haven’t heard back from them yet.
Any other ideas you might have would be greatly appreciated!
Thanks again,
Vivek
function fetchDhanPortfolio() {
// Access Token
const accessToken = ‘MY_NEW_ACCESS_TOKEN’;