DhanHQ - Official Python Client

raw JSON →
2.2.0 verified Fri May 01 auth: no python

The official Python client for communicating with the DhanHQ API, version 2.2.0. Provides access to trading, order management, market data, and portfolio endpoints for the Dhan stock trading platform. Release cadence is irregular.

pip install dhanhq
error ModuleNotFoundError: No module named 'dhanhq'
cause The package is not installed or installed in a different environment.
fix
Run pip install dhanhq in your Python environment.
error AttributeError: module 'dhanhq' has no attribute 'Dhanhq'
cause Incorrect import path, often due to using deprecated import style.
fix
Use from dhanhq import Dhanhq (not from dhanhq.dhanhq import Dhanhq).
error dhanhq.exceptions.InvalidTokenException: Invalid access token
cause The access token is expired, malformed, or does not match the client ID.
fix
Generate a new access token via DhanHQ API and ensure it is passed correctly. Check that client_id and access_token are both valid.
gotcha The access token must be generated via DhanHQ's API flow (not a password). The token is short-lived (typically 1 day) and must be refreshed. Hardcoding tokens will cause auth failures.
fix Implement token refresh logic using the refresh_token endpoint or generate a new token before each trading session.
deprecated The old import `from dhanhq.dhanhq import Dhanhq` is deprecated. Use `from dhanhq import Dhanhq` instead.
fix Change import to `from dhanhq import Dhanhq`.
breaking In version 2.0.0, the `Order` class was removed. All order functions are now methods on the `Dhanhq` client instance.
fix Use `client.place_order(...)` instead of `Order.place_order(...)`.

Initialize the client with your credentials and fetch fund limits.

from dhanhq import Dhanhq

client_id = os.environ.get('DHAN_CLIENT_ID', '')
access_token = os.environ.get('DHAN_ACCESS_TOKEN', '')

client = Dhanhq(client_id, access_token)
# Get account balance
funds = client.get_fund_limits()
print(funds)