API access token generation

Hii @RajivK , we are happy to share that the Dhan Algos feature is now live! Your valuable input helped shape this feature, and we’d love to know your experience with it.

For more details, please visit the link below: Introducing DhanHQ Algos – Choose, Deploy & Monitor Expert-Built Algo Strategies on the Go

The post doesn’t mention anything about fully automatic browser based logic step during access token generation. I have successfully setup TOTP but can’t seem to bypass the manual step for browser login to generate tokenId from TOTP authentication. I am using java to consume APIs, so is it possible to completely automate browser based login step?

Hey @Tilted_Gamer

Welcome to MadeForTrade community!

While it is possible to automatically do the browser login step, you might have to write the code to open the browser automatically and fill in the necessary sections.
Also, there is a another way as well which is added as part of access token itself.

So what I gather is somehow I need to automate the browser login step, but before I do that can you please let me know if Dhan team is currently working on any new API which they are planning to launch in near future that will eliminate the browser login step (which is currently our responsibility to automate) and replace that with a API call which directly accepts TOTP code to generate tokenId?

1 Like

correct like upstox having separate upstox autologin api

@Tradehull_Imran

I am currently integrating the Dhan API for automated trading and encountered an issue in the authentication workflow related to the TOTP verification and consent generation stage. I would appreciate your assistance in understanding and resolving the problem.

Environment Details

Client Platform: Python 3.12
Automation Framework: Playwright (browser automation)
TOTP Generation: pyotp library
Operating System: Windows 10
API Version: Dhan API v2 authentication flow

The login automation script is designed to perform the following sequence:

  1. Generate consent URL through the API.

  2. Navigate to the Dhan login page.

  3. Enter mobile number and verify using TOTP.

  4. Enter PIN for final authentication.

  5. Capture authorization code and exchange it for an access token.

All environment variables including client_id, mobile number, TOTP secret, and PIN are correctly configured and validated before execution.

Observed Issue

During execution, the script repeatedly fails at the consent generation stage. The logs show the following response:

errorCode: CONSENT_LIMIT_EXCEED
errorMessage: Consent limit Exceeded

Below is the relevant portion of the execution logs:

2026-03-07 00:56:30 | Loaded env | client_id masked mobileno masked
2026-03-07 00:56:32 | LoginAttempt Starting attempt 1/2
2026-03-07 00:56:35 | GenerateConsent HTTP POST attempt 1/3
2026-03-07 00:56:37 | WARNING Consent limit exceeded Waiting 65 seconds before retry
2026-03-07 00:57:42 | GenerateConsent retry
2026-03-07 00:57:43 | WARNING Consent limit exceeded Waiting again
2026-03-07 00:58:48 | ERROR consentAppId missing payload errorCode CONSENT_LIMIT_EXCEED
2026-03-07 00:58:49 | Login failed reason consent_url_generation_failed

Because the consent URL is not generated, the authentication process cannot proceed further, and the access token is never created.

Troubleshooting Already Performed

To ensure the issue was not related to the local implementation, I performed the following checks:

  1. Verified that the client_id and API configuration are correct.

  2. Confirmed that the TOTP secret is valid and generating correct codes.

  3. Tested multiple login attempts with sufficient delay between requests.

  4. Implemented retry logic and exponential backoff for the consent API.

  5. Ensured that the script is not generating parallel authentication requests.

  6. Confirmed that the API payload format matches the documentation.

Despite these checks, the API consistently returns the consent limit exceeded response.

Clarifications Requested

Could you please help clarify the following points:

  1. What is the exact rate limit or quota for the consent generation API endpoint?

  2. Is the consent limit calculated per client_id, per user account, or per IP address?

  3. How long should we wait before retrying once the consent limit is exceeded?

  4. Is there any recommended authentication flow for automated trading systems using the Dhan API?

  5. Are there any recent changes in the authentication or consent generation process that might affect automated login scripts?

Impact

This issue is preventing automated access token generation, which blocks the ability to run API based trading automation workflows.

If there are recommended best practices or alternative authentication mechanisms for automated systems, I would appreciate your guidance.

Please let me know if you require additional logs or debugging information from my side.

Thank you for your support.

TOTP based login has been enabled via API. Dependency installation issue with latest Dhan 3.2.0 - Tradehull release on PyPI - #7 by DSingh

Then why I am getting the error of consent generation API endpoint I have followed all the correct steps
I think this issue is from Dhan side.

Can you take a closer look and guide me what I am doing incorrectly ?

@Dpwd Now you don’t need any “browser” automation at all. Simply follow steps as mentioned in the link. Dependency installation issue with latest Dhan 3.2.0 - Tradehull release on PyPI - #7 by DSingh

Earlier I used to do the same using Puppeteer in Node.js but that’s no longer needed.

If the access token was generated first using web, you can indefinitely renew it (create new access token) from API prior to its expiry each day. :backhand_index_pointing_down:

@DSingh

Thank you the issue is resolved.
I find your responses very valuable.

Are you from Dhan itself ?

1 Like

No. Kindly check links in my profile.