dhan-ts
v1.4.1
Published
TypeScript client for Dhan API v2
Maintainers
Readme
dhan-ts
A comprehensive, fully-typed TypeScript/JavaScript library for Dhan's trading API v2
Build powerful trading applications with type-safe access to 15+ API modules and real-time WebSocket feeds.
📚 Documentation • 🚀 Getting Started • 📖 API Reference • ⚡ WebSocket Feeds
✨ Features
- 🎯 Fully Typed - Complete TypeScript support with IntelliSense and compile-time type checking
- ⚡ Real-time Feeds - WebSocket feeds for live market data, order updates, and market depth
- 📦 Comprehensive - 15+ API modules covering orders, portfolio, funds, market data, options, and more
- 🛡️ Production Ready - Battle-tested with automatic reconnection, error handling, and robust architecture
- 🧪 Testing Support - Mock feeds for development and testing without live connections
- 🔄 Auto-Reconnection - Built-in reconnection logic with exponential backoff for WebSocket feeds
- 📊 Market Data - Real-time quotes, historical data, option chains with Greeks, and fundamentals
📦 Installation
npm install dhan-ts🚀 Quick Start
import { DhanHqClient, DhanFeed, DhanEnv } from "dhan-ts";
// Initialize REST API client
const client = new DhanHqClient({
accessToken: process.env.DHAN_ACCESS_TOKEN!,
clientId: process.env.DHAN_CLIENT_ID!,
env: DhanEnv.PROD,
});
// Get your fund limits
const funds = await client.funds.getFundLimit();
console.log("Available Balance:", funds.availabelBalance);
// Place an order
const order = await client.orders.placeOrder({
dhanClientId: process.env.DHAN_CLIENT_ID!,
transactionType: "BUY",
exchangeSegment: "NSE_EQ",
productType: "CNC",
orderType: "LIMIT",
validity: "DAY",
securityId: "1333",
quantity: 1,
price: 1850.0,
afterMarketOrder: false,
});
// Initialize WebSocket feed
const feed = new DhanFeed({
accessToken: process.env.DHAN_ACCESS_TOKEN!,
clientId: process.env.DHAN_CLIENT_ID!,
env: DhanEnv.PROD,
});
// Connect to live feed
await feed.liveFeed.connect();
// Subscribe to instruments
feed.liveFeed.subscribe([[1, "1333"]], "SUBSCRIBE_TICKER");
// Listen for updates
feed.liveFeed.on("data", (data) => {
console.log("Price:", data.lastTradedPrice);
});📚 Documentation
For complete documentation, visit dhan-ts.vercel.app
Quick Links
- Getting Started Guide - Installation and first API call
- Authentication - App-based, partner, and static IP auth
- Orders API - Place, modify, cancel orders
- Portfolio API - Holdings and positions
- Market Data API - LTP, OHLC, historical data
- Live Feed - Real-time market data WebSocket
- Live Order Updates - Real-time order status
- Option Chain - Options with Greeks and IV
- Mock Feeds - Testing without live connections
🎯 API Coverage
REST APIs (15 Modules)
Orders • Portfolio • Funds • Market Data • Super Orders • Forever Orders • Option Chain • Expired Options • Authentication • E-DIS • Statements • Traders Control • Scanner
WebSocket Feeds (6 Types)
Live Feed • Order Updates • Market Depth (20 & 200) • Multi Connection • Mock Feeds
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🔗 Links
⚠️ Disclaimer: This is an unofficial TypeScript client for Dhan API. For official support, please contact Dhan directly.
Made with ❤️ by the community
