This repository contains a Python SDK generated using the Dhan Trading Platform's OpenAPI (Swagger) specification. It allows seamless interaction with Dhan's order management, portfolio, funds, and market data APIs.
- ✅ Place, modify, and cancel orders
- 📊 Access real-time and historical market data
- 💼 View portfolio holdings and fund details
- 🔐 Secure API access via access tokens
git clone https://github.com/your-username/dhan-python-sdk.git
cd dhan-python-sdk
pip install .
📦 This will install the SDK along with its dependencies.
- Python 3.7+
- Access to Dhan APIs
- Your personal Access Token from Dhan
To authenticate and use the SDK:
from dhan_sdk import Configuration, ApiClient
from dhan_sdk.api.orders_api import OrdersApi
from dhan_sdk.models.place_order_request import PlaceOrderRequest
# Setup configuration with your access token
configuration = Configuration(
host="https://api.dhan.co"
)
configuration.api_key['access-token'] = 'YOUR_ACCESS_TOKEN'
# Initialize API client and Orders API
with ApiClient(configuration) as api_client:
orders_api = OrdersApi(api_client)
# Define order details
order_request = PlaceOrderRequest(
security_id="RELIANCE-EQ",
exchange_segment="NSE",
transaction_type="BUY",
quantity=10,
order_type="MARKET",
price=0,
product_type="INTRA"
)
# Place the order
try:
response = orders_api.place_order(order_request)
print("✅ Order placed successfully:", response)
except Exception as e:
print("❌ Failed to place order:", e)
All API classes are located in the dhan_sdk.api package:
- OrdersApi
- MarketDataApi
- PortfolioApi
- FundsApi
Model classes (request/response schemas) are in dhan_sdk.models.
All API calls require an access-token which can be retrieved from your Dhan Profile Settings → DhanHQ Trading APIs
configuration.api_key['access-token'] = 'YOUR_ACCESS_TOKEN'
Regenerate SDK (if YAML is updated):
openapi-generator generate -i dhan_api.yaml -g python -o dhan_sdk
This SDK is provided under the MIT License.
Pull requests are welcome! For major changes, please open an issue first to discuss what you’d like to change.