Help Needed with Dhan Ledger and Trade History APIs Integration through JavaScript

Hi everyone,

I’m facing a persistent issue while trying to fetch my Dhan ledger data into Google Sheets using GAS (Google Apps Script).

I keep encountering errors despite following the API documentation and trying various solutions.

I’ve confirmed that I have ledger entries within the specified date range in my Dhan account.

Below is the code I’m using, and below the code is the Execution log screenshot.

Thanks in advance for your help!

function fetchDhanLedger() {
  const accessToken = 'My_Access_Token'; // Replace with your actual Access Token
  const ledgerApiUrl = 'https://api.dhan.co/v2/ledger';
  const fromDate = '2024-10-01'; // Replace with your desired start date
  const toDate = '2024-10-08'; // Replace with your desired end date

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('Statement');
  sheet.clearContents();

  const apiUrlWithParams = `${ledgerApiUrl}?from_date=${fromDate}&to_date=${toDate}`;

  const options = {
    'method': 'get',
    'headers': {
      'Accept': 'application/json',
      'access-token': accessToken
    },
    'muteHttpExceptions': true
  };

  try {
    const ledgerResponse = UrlFetchApp.fetch(apiUrlWithParams, options);
    Logger.log("Ledger Response: " + ledgerResponse.getContentText());

    const ledgerData = JSON.parse(ledgerResponse.getContentText());

    // Check if ledgerData is an array
    if (!Array.isArray(ledgerData)) {
      Logger.log("Error: Ledger data is not an array. Response: " + JSON.stringify(ledgerData));
      return; // Exit the function if it's not an array
    }

    let row = 1;
    for (const entry of ledgerData) {
      sheet.getRange(row, 1).setValue(entry.dhanClientId);
      sheet.getRange(row, 2).setValue(entry.narration);
      sheet.getRange(row, 3).setValue(entry.voucherdate);
      sheet.getRange(row, 4).setValue(entry.exchange);
      sheet.getRange(row, 5).setValue(entry.voucherdesc);
      sheet.getRange(row, 6).setValue(entry.vouchernumber);
      sheet.getRange(row, 7).setValue(entry.debit);
      sheet.getRange(row, 8).setValue(entry.credit);
      sheet.getRange(row, 9).setValue(entry.runbal);
      row++;
    }

  } catch (error) {
    Logger.log("Error parsing JSON response: " + error);
  }
}

Faced same error even in sandbox @Hardik .

In my case, v2 endpoint gave same error, whereas v1 endpoint worked flawless for same account, same dataset, in dhan sandbox.

So seems like problem is with v2 endpoint given in documentation

1 Like

Hello @Vivek_Raj @TraderX

I tried replicating this with my credentials but couldn’t. I even checked logs on requests here, it is showing success.

Can you try once again, and let me know if you are still getting this error here.

Yes, still getting the error with v2 Ledger Report API…

{ “errorType”: “Order_Error”, “errorCode”: “DH-906”, “errorMessage”: “Incorrect request for order and cannot be processed”}

Error: ledgerData.data is not an array or is undefined. Check the API response.

v1 Ledger Report API is working just fine,

1 Like

Hello @Vivek_Raj

Can you please DM your client ID and the parameters you are passing on the API? Will check logs here, as the same couldn’t be replicated at my end.