# 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="/files/J1pz94RrFsMiM3A1Vpac" 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="/files/YUaL6e2ubkxvSgZOv2OW" 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="/files/QaG2L4sMnhGMQccKaUCU" 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="/files/o6o2dqkw4HVXl4Yu75WR" 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="/files/fHtPdZoISNaHiMGhHHzM" 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.

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.openalgo.in/connect-brokers/brokers/dhan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
