Fetching my portfolio, orders, trades etc. in Google Sheet through Google Apps Script

Hi there!

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)
  • Adding necessary request headers (e.g., Content-Type: application/json)
  • Checking for any required query parameters

Despite these efforts, the error persists.

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.

Thank you for your time and attention.

Hello @Vivek_Raj

Welcome to MadeForTrade community!

Glad to see someone integrating Dhan APIs directly into Google Sheets.

Over here, the endpoint being used is incorrect. It needs to https://api.dhan.co/v2/holdings.

You can check Portfolio Documentation for the same: here.

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) ...
}

And here’s a screenshot of the execution log:

Do you have any other ideas about what might be causing this? Could it be something specific to my Dhan account or setup?

Thanks again for your help!

Best,
Vivek

Hello @Vivek_Raj

Over here, you are using JWT token as bearer token. That is not required. You can simply add access token in the header directly.

Further, do you have any positions/holdings in your account as well?

Hey @Hardik ,

Thanks again for your help!

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’;

// Dhan API endpoint for holdings
const apiUrl = ‘https://api.dhan.co/v2/holdings’;

const options = {
‘method’: ‘get’,
‘headers’: {
‘Authorization’: accessToken,
‘Content-Type’: ‘application/json’
},
‘muteHttpExceptions’: false
};

const response = UrlFetchApp.fetch(apiUrl, options);

// … (rest of the code for processing the response) …
}

Hello @Vivek_Raj

Try with this:

const options = {
    'method': 'get',
    'headers': {
      'Accept': 'application/json',
      'access-token': accessToken
    },
2 Likes

Hi @Hardik

That did work like a charm!!!

A warm hug and a million thanks! :slight_smile:

2 Likes