Tutorial

How to Connect Your Dhan Account to TradeCraft (Free DhanHQ API)

Register the static IP TradeCraft gives you, switch on TOTP in Dhan, and paste three details. Set it up once and it stays connected — every screen, in order.

Written by Geet Ramchandani

Published 6 min read

To connect Dhan to TradeCraft, you add the static IP that TradeCraft gives you to your Dhan account, switch on TOTP in Dhan's API settings, and paste your Client ID, Dhan PIN and TOTP secret into TradeCraft. It takes about 10 minutes, once. After that you never have to log in to Dhan for TradeCraft again: your access renews itself.

Before you start

  • A Dhan account, and the PIN you log in to Dhan with.
  • A laptop or desktop, logged in to web.dhan.co.
  • One sitting of about 10 minutes. TradeCraft holds your IP address for 90 minutes while you finish.
  • No payment. Dhan's Trading APIs are free. You do not need the paid Data APIs.

You will move between two browser tabs, TradeCraft and Dhan Web. Keep both open.

Step 1: Get your static IP from TradeCraft

  1. Log in to TradeCraft and open Brokers in the sidebar.
  2. Under Link a Broker Account, find Dhan and click Connect.
  3. The Register your trading IP window opens with an address. Click the copy button next to it.
The Register your trading IP window for Dhan showing a reserved IP address with a copy button
1Copy the address, and leave this window open.

Step 2: Add the static IP on Dhan

  1. On web.dhan.co, click your Profile.
  2. Select Get Trading & Data APIs. On some accounts this is labelled DhanHQ Trading APIs.
  3. Scroll down and click Add IP.
  4. Paste the IP from Step 1 as your primary IP and save. Leave the secondary IP empty.
Dhan Web profile menu with Get Trading & Data APIs highlighted
2Profile → Get Trading & Data APIs.
Dhan Static IP Setting section showing the saved primary and secondary IP addresses
2Static IP Setting after saving your TradeCraft IP.

Step 3: Note your Client ID

Your Client ID is shown on your Dhan Profile page, under your name. Copy it. You paste it into TradeCraft in Step 5.

Dhan profile page with the Client ID highlighted, other details blurred
3Your Client ID, on the Dhan profile page.

Step 4: Set up TOTP and copy the secret

Stay on the Get Trading & Data APIs page.

  1. Click Setup TOTP.
  2. If prompted, confirm with the OTP Dhan sends to your mobile or email.
  3. Dhan shows a QR code and a text code next to it.
  4. Copy the text code. This is your TOTP secret for TradeCraft.
Dhan Setup TOTP side panel showing the OTP verification screen
4First, verify the OTP Dhan sends to your registered mobile and email.
Dhan Setup TOTP side panel showing the QR code and the setup secret text field
4Then copy the text code shown with the QR. That text — not a 6-digit code — is what TradeCraft needs.

Step 5: Connect in TradeCraft

  1. Go back to the TradeCraft tab. In the Register your trading IP window, click I've registered it.
  2. The Connect Dhan window opens. Fill in:
Field in TradeCraftWhat to paste
Client IDYour Dhan Client ID from Step 3
PINThe PIN you log in to Dhan with
TOTP SecretThe text code from Step 4
  1. Click Connect. You'll see Dhan connected. Its access renews itself — no daily login.
TradeCraft Connect Dhan window with Client ID, PIN and TOTP Secret fields
5Three fields, once.
The whole TradeCraft side, from I've registered it to a connected account.
The whole TradeCraft side, from I've registered it to a connected account.

Dhan does not share your name with TradeCraft, so the account is listed by Client ID. Click the pencil next to it to give it a name, such as Dhan — main.

After you connect

There is nothing to do each day. Dhan sessions last 24 hours, and TradeCraft renews yours on its own. If the account ever shows Session expired, click Reconnect. It renews without asking you for anything.

If you reset TOTP or change your PIN in Dhan, TradeCraft's copy stops working. When the account next shows Session expired, click Reconnect: the renewal fails, TradeCraft asks for your details again, and you enter the new ones.

If something goes wrong

What you seeWhat it meansWhat to do
Could not compute Dhan TOTPWhat you pasted is not a TOTP secretPaste the text code shown with the QR, not a 6-digit code
Dhan token response contained no accessToken, or Invalid TOTPWrong PIN or TOTP secretCheck your PIN; if it repeats, redo Step 4 and paste the latest secret
Client ID is required to set up automatic token refreshThe Client ID field is emptyPaste it from your Dhan profile
Orders rejected with DH-905 Invalid IPThe IP on Dhan does not match TradeCraft's, or was added after you connectedCompare the IPs; if they match, click Reconnect in TradeCraft
DH-902Trading APIs are not active on your accountOpen Get Trading & Data APIs on Dhan and check Trading APIs are enabled
Dhan will not let you change the IPThe 7-day lockWait until the date Dhan shows, then update it

Still stuck? Write to [email protected] with the exact error message. Never include your PIN or TOTP secret.

Key takeaways

  • Dhan's Trading APIs are free — you do not need the paid Data APIs.
  • Add the TradeCraft IP on Dhan first. It is locked for 7 days once set.
  • Paste the text code shown with the TOTP QR, not a 6-digit code.
  • Connect once: after that, your Dhan access renews itself.

Frequently asked questions

Is the Dhan API free?
Dhan's Trading APIs are free, and they are all TradeCraft needs. Dhan's Data APIs are a separate paid subscription (₹499 a month plus taxes) that you do not need for TradeCraft.
Do I have to log in to Dhan every day?
No. Once you have set up TOTP and connected, TradeCraft renews your Dhan access on its own. If the account ever shows as expired, click Reconnect in TradeCraft and it renews without asking you for anything.
Which PIN does TradeCraft ask for?
The PIN you use to log in to Dhan. It is not a trading password, a TPIN or a 6-digit TOTP code.
What is the TOTP secret, and why does TradeCraft need it?
When you open Setup TOTP, Dhan shows a QR code and a text code next to it. That text code is the TOTP secret. TradeCraft uses it to generate the required OTP automatically each day, so you do not have to log in by hand.
Can I change the static IP later?
Only after 7 days. Once an IP is set on Dhan it cannot be modified for the next 7 days, so check it carefully before you save.

Want to try this yourself?

TradeCraft lets you build a strategy, backtest it against historical data, and deploy it on paper before risking a rupee — using your own broker account.

About the author

Geet Ramchandani

Founder & Engineer, TradeCraft

Geet is the founder of TradeCraft, a platform built for retail traders to automate their strategies and mirror trades across broker accounts. With 5+ years of experience trading the markets and hands-on experience with algorithmic trading, he writes about trading systems, execution, backtesting, risk, and what it takes to build them in the real world.