Browse docs
Docs / Dhan Setup

Dhan

Dhan is the newest broker integration (DhanHQ API v2). It supports two ways to get an access token: an automatic redirect login (like Kite/Breeze) via an API Key, or a manual copy-paste for a quick start / fallback.

Prerequisites

  • A Dhan trading account.
  • API access enabled on that account (Dhan enables this by default for most accounts — check My Profile → Access DhanHQ APIs on web.dhan.co if you don't see a token option).

  • Your Client ID (shown on your Dhan profile) — needed for both flows below.

  • A Data API subscription on your Dhan account — see the warning below. Without it, login itself still works, but every quote/historical-data call fails, which blocks sim runs and live strategies entirely (Trading API calls like margins/positions/orders are unaffected).

Reference: DhanHQ API v2 documentation — https://dhanhq.co/docs/v2/

⚠️ Data API Subscription Required (separate from Trading API access)

Dhan gates market data (quotes, LTP, historical candles) behind a separate subscription/activation from order placement. An account with working Trading API access (margins/positions/orders all succeed, login passes the contract check) can still have Data API calls fail on every request with:

HTTP Status 451. User has not subscribed to Data APIs or does not have
access to Trading APIs. Kindly subscribe to Data APIs to be able to fetch Data.

If you see this — in a failed sim run, or DhanAdapter.ltp/historical_data errors in the App Event Log — it's not a login/token problem, and re-logging in will not fix it. Check My Profile → Access DhanHQ APIs on web.dhan.co for a Data API subscription/activation option, or contact Dhan support. Since every strategy needs live quotes to operate, this is effectively required before Dhan is usable for anything beyond checking margins/ positions.

Like Kite and Breeze, this redirects you to Dhan to log in and sends you back to this app automatically — no daily copy-paste.

One-time setup

  1. Log in to https://web.dhan.coMy ProfileAccess DhanHQ APIs.
  2. Switch the toggle from Access Token to API Key.
  3. Click Generate new API Key and fill in: - Application Name — your own value. - Redirect URL — set this to http://127.0.0.1:5000/dhan_callback (use the host/port you actually run the app on — this must exactly match the server that's actually listening, confirmed live 2026-07-19: a redirect landing on a port nothing is listening on wasted a long debugging session that looked like a token-validity bug but was really just a dead connection). - Postback URL — leave blank (unrelated to login; it's for real-time order-update webhooks, which this app doesn't use).
  4. Click Generate API Key. This gives you an API Key and API Secret, valid for 12 months — unlike the 24-hour access token below, you only do this step occasionally.

One-time credential save, then daily login

On the Auth page, in the Dhan card, saving credentials and starting login are two separate steps:

  1. Under Credentials: enter your Client ID (optional — Dhan identifies your account during the browser login step), API Key, and API Secret, then click Save Credentials. This only persists them — no request to Dhan yet, so you can confirm the save succeeded before triggering a redirect.
  2. Under Login: click Start Dhan Login. Dhan's login page opens; complete login. You're redirected back and the access token is captured automatically.
  3. Click Test Dhan Connection to confirm.

Your API Key/Secret stay saved — click Start Dhan Login each morning to refresh the token without re-entering anything (only re-run step 1 if the Key/Secret themselves change).

Dhan caps this at 25 login attempts per day per account, and only the most recently issued token stays active — starting a new login invalidates any token from an older one.

Option B: Manual Access Token Paste — fallback

If your Redirect URL isn't registered yet, or you'd rather not set up an API Key, use this instead. It requires copy-pasting a fresh token every day.

  1. Log in to https://web.dhan.coMy ProfileAccess DhanHQ APIs (Access Token toggle, not API Key).
  2. Generate an access token — a JWT, valid for 24 hours.
  3. On the Auth page, in the Dhan card, click Paste access token manually, enter your Client ID and the token, and click Save Dhan Access Token.

Either way, the token is validated immediately via the API contract check before it's stored, and credentials are kept in the OS keychain.

The badge in the top-right navbar changes from ✕ Dhan (red) to ✓ Dhan (green) once a fresh token is saved.

Active vs Stale

Unlike Kite and Breeze — whose tokens die at the end of the trading day — a Dhan token is valid for 24 hours from the moment it was issued. The Active/Stale badge on the Auth page reads that token's own expiry, so a token generated at 4pm yesterday still shows Active until 4pm today. It flips to Stale only when the token has actually expired.

⚠️ Static IP Requirement (mandatory, not optional)

SEBI rules require Dhan's order-placement APIs to see requests only from a whitelisted IP address — this is enforced server-side by Dhan, not just a recommendation. Register your IP on web.dhan.co before relying on Dhan for live trading.

If your IP changes (common on dynamic home broadband), order placement will start failing silently until the new IP is re-registered with Dhan. Quotes, positions, and margins will keep working — only order placement is IP-gated — so a broken IP whitelist can go unnoticed until an order actually needs to go out. If you rely on Dhan for live trading, confirm your ISP connection has a stable IP, or re-check the whitelist after any router/ISP change.

API Contract Check

Like Kite and Breeze, a Dhan token is only saved if it passes the API contract check — the app calls the read-only endpoints it depends on (margins, positions, order list) before persisting the token. Unlike Breeze, an empty book is a plain empty list from Dhan (no special-case error string to work around), so a fresh login before your first order of the day behaves normally.

Known Limitations

  • Only current, spec-based strategies (built via Strategy Builder/Composer) support Dhan — those go through the app's shared simulation data path, which handles Dhan's string-based security IDs correctly. The older family of hand-written strategy scripts special-case Kite/Breeze directly and aren't offered to users, so this isn't a practical limitation.

  • Option Scanner treats Kite/Breeze/Dhan as equal, standalone brokers (since 2026-08-21) — the page's "Broker" dropdown picks the one broker used for instruments, spot, OI, and LTPs for that whole scan; Kite is no longer required to use the scanner at all. One real gap remains in the Flow (OI Analysis) tab: its "previous trading day's close" OI baseline needs historical_data(oi=True), which Dhan's adapter now forwards to /charts/historical but hasn't been confirmed against a live F&O response (no funded Data API account has exercised it yet) — if the response doesn't carry OI the way expected, Flow silently falls back to today's first scan as the baseline instead of erroring, same as it always did before Dhan was wired in.

Applies to ThetaPrime v0.35.4 · Page last updated 2026-08-22