I have multiple strategies that I’m planning to run across different machines/instances. Each instance requires its own live market data feed.
The API documentation mentions that up to 5 WebSocket connections are allowed, which should work well for my use case. However, I’m confused about how authentication should be handled across multiple independent machines.
Since these machines are completely independent, sharing a token between them could be difficult and would introduce additional complexity, such as securely distributing the token and handling token refresh/expiry across instances.
If each machine authenticates independently, how does that work given the following limitation in the documentation?
“User can generate up to 25 consentAppIds in a day. Each consentAppId stays active until a tokenId is generated for it. However, at any given point in time, only one token will be generated.”
My use case is to run multiple independent strategy instances, potentially with each instance maintaining its own WebSocket connection. Ideally, I would prefer each machine to handle its own authentication rather than depending on a central authentication service or another machine to distribute tokens.
The documentation also mentions multiple authentication mechanisms. Which authentication method is recommended for this type of multi-instance setup?
Specifically, I would like to understand:
- Can multiple machines authenticate independently using the same account?
- If yes, can they each generate their own valid token simultaneously, or does generating a new token invalidate the previous one?
- Which authentication method is preferred for running multiple independent strategy instances in production?
I’d also like to understand how other users typically handle this architecture when running multiple strategies across multiple machines/instances.
I had a similar problem. What I did was set up a small VM at home running a Flask server that polls the Dhan APIs and caches the data in Redis. It then exposes another API that reads the latest data and serves it to the endpoint. This way, all the consumers (strategies) that need data can just query the local API.
Hi @stanly_thomas ,
-
Yes. Multiple machines can authenticate using the same Dhan account, subject to the overall limit of 5 concurrent Market Feed WebSocket connections per user.
-
Yes. Multiple access tokens can coexist, and renewing one token does not invalidate another active token. The 25 consentAppId/day limit applies to the consent-based authentication flow.
-
For independent/headless strategy instances, TOTP-based Access Token Generation is the simpler approach, allowing each instance to manage its own token.
Note: If the instances also place orders, Dhan’s static-IP whitelisting requirements apply separately.
1 Like
HI @Tradehull_Imran, I tried the TOTP based access token mechanism from 2 machines. Following are my observations
- Once authenticated from one machine, if I try to authenticate again within 2 minutes from another machine, I get the message: “Token can be generated once every 2 minutes.”
- If I wait for 2 minutes and authenticate from the second machine, a new token is generated successfully, but it invalidates the token that was generated on the first machine.
I know that there is an option to create multiple access tokens for multiple apps from the Dhan dashboard, and we can use the Renew Access Token API to generate a fresh token.
However, the renewal API requires an active/valid token. This may not always work reliably because the access token can become invalid after 24 hours. For example, if a token expires or an instance is restarted after the token has already expired, there would be no valid token available to renew it.
I understand that we could implement workarounds, such as ensuring that the token is renewed before expiry or using a centralized token-management mechanism. However, since my strategy instances are independent and may run on separate machines, I’m looking for a more straightforward and reliable approach.
Is there a directly supported way to authenticate multiple independent instances and obtain fresh access tokens without requiring an existing active token for renewal?
For my use case, I want each instance to be able to independently recover and authenticate itself, even after a restart or token expiry, without depending on another instance or manually sharing/distributing tokens.
Hi @stanly_thomas ,
Yes, that is correct. Dhan currently allows up to 5 active access tokens simultaneously for the same Dhan account/Client ID, so you can generate separate tokens and use them on different systems/instances independently.
So for your use case, you can create a separate access token for each machine/instance instead of generating a new TOTP token each time, which may affect the previously generated token. Each token will have its own validity and can be managed/revoked separately.