# Dhan

Dhan is a new-generation financial services platform that empowers traders and investors to build, automate, and execute their trading strategies. Through **DhanHQ APIs**, users can connect their Dhan account with tools like **OpenAlgo** and create their own trading automation setup — fully self-hosted and secure.

{% embed url="<https://www.youtube.com/watch?v=Gs657PKGgcE>" %}

***

### Overview

Dhan provides two main types of APIs:

| API Type         | Features                                                                                                    | Cost                                   |
| ---------------- | ----------------------------------------------------------------------------------------------------------- | -------------------------------------- |
| **Trading APIs** | Order Placement, Position Management, Portfolio & Funds, Order Postbacks, Statement Reports                 | **Free of Cost**                       |
| **Data APIs**    | Real-time Prices, Historical Data (5 years), 20-Level Market Depth, Option Chain APIs, Expired Options Data | ₹499/month or ₹399/month (₹4,788/year) |

***

### Steps for Integration

#### **Step 1: Log in to Dhan Web Portal**

Visit [web.dhan.co](https://web.dhan.co/) and sign in to your Dhan account.

***

#### **Step 2: Access DhanHQ APIs**

Click on your profile icon at the top-right and select **“Access DhanHQ APIs”**.

From this page, you can:

* Generate API Keys
* Enable TOTP
* Setup Static IP
* Manage Trading & Data APIs

<figure><img src="https://17901342-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmBwEhITzgv0O0fEGIIRN%2Fuploads%2Fcl3ZrS6sahdXldCb4bqd%2Fimage.png?alt=media&#x26;token=a37a29cd-a596-44dd-adc1-f59e0f804422" alt=""><figcaption></figcaption></figure>

***

#### **Step 3: Switch to API Key Mode**

By default, DhanHQ opens in **Access Token** mode.\
Use the toggle on the top-right to switch to **API Key Mode**.

<figure><img src="https://17901342-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmBwEhITzgv0O0fEGIIRN%2Fuploads%2Fv5Fat0tC0iRQcLONciOs%2Fimage.png?alt=media&#x26;token=6cb10cf6-2feb-4249-b4d9-2352e12b2241" alt=""><figcaption></figcaption></figure>

***

#### **Step 4: Generate a New API Key**

1. Under **Generate new API Key**, enter:
   * **Application Name** → e.g. `openalgo`
   * **Redirect URL** → `http://127.0.0.1:5000/dhan/callback`
   * (Optional) **Postback URL** if your system supports order postbacks
2. Click **Generate API Key**.
3. Copy your:
   * **API Key**
   * **API Secret**

<figure><img src="https://17901342-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmBwEhITzgv0O0fEGIIRN%2Fuploads%2F4kSaBTPLSYn8cJtJdj9H%2Fimage.png?alt=media&#x26;token=71777ede-5b64-4016-bea3-e2579ec25a4b" alt=""><figcaption></figcaption></figure>

***

#### **Step 5: Retrieve Client ID**

1. From the same profile menu, select **“My Profile on Dhan.”**
2. Scroll to **Profile Details** and copy your **Client ID**.\
   This will be used as part of your API credentials.

<figure><img src="https://17901342-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmBwEhITzgv0O0fEGIIRN%2Fuploads%2Fh1dJ6RC8ZuA1DbND7N67%2Fimage.png?alt=media&#x26;token=4a32b150-11dd-4d30-9869-e7c74b935fea" alt=""><figcaption></figcaption></figure>

***

#### **Step 6: Enable TOTP (Mandatory)**

* Under **Optional Settings - down below the API Key section**, click **Set-up TOTP.**
* Follow the instructions to configure two-factor authentication for API access.
* This is **mandatory** for all Dhan API users.

***

#### **Step 7: Setup Static IP (Optional / Mandatory from Jan 2026)**

* Click **Static IP Setting** under Optional Settings.
* Add up to two IP addresses (e.g., your server or office IP).
* You can update these every 7 days.
* From **January 2026**, setting at least one **Static IP** will become mandatory.

<figure><img src="https://17901342-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmBwEhITzgv0O0fEGIIRN%2Fuploads%2FGBcU50fom7nbIra12iud%2Fimage.png?alt=media&#x26;token=afd59cd9-84ae-463f-8bb3-26965c13b308" alt=""><figcaption></figcaption></figure>

***

### Environment Configuration

Once your credentials are generated, configure them in your `.env` file:

```env
BROKER_API_KEY = 'your_dhan_clientid:::your_dhan_apikey'
BROKER_API_SECRET = 'your_dhan_apisecret'
REDIRECT_URL = 'http://127.0.0.1:5000/dhan/callback'
```

***

### API Costs Summary

| API Type         | Description                                                       | Cost                                        |
| ---------------- | ----------------------------------------------------------------- | ------------------------------------------- |
| **Trading APIs** | Order, Position, Portfolio, Funds & Report APIs                   | ✅ **Free of Cost**                          |
| **Data APIs**    | Real-time + Historical Data (5 years), Market Depth, Option Chain | 💸 ₹499/month (or ₹399/month billed yearly) |

***

### Best Practices

* Keep your **API Key, Secret, and Client ID private**.
* Do not share credentials with third-party applications.
* Always enable **TOTP** for added security.
* Use **Static IPs** when deploying in production environments.
* **Daily login to OpenAlgo Portal is mandatory.**

***

Integrating with **DhanHQ APIs** unlocks the ability to automate strategies, execute trades, and analyze data directly within your own infrastructure.\
When used with **OpenAlgo**, you can self-host and run your entire algo trading stack — with full control and zero vendor lock-in.

***
