dhanapi-go
Unofficial Dhan v2 SDK for Go. It provides api, instruments, and ws
packages. The examples package contains reusable examples for the common
flows.
client := api.NewClient(dhanClientID, apiKey, apiSecret)
loginURL, err := client.GenerateLoginURL(ctx)
// After Dhan redirects with tokenId:
session, err := client.GenerateSession(ctx, tokenID)
// Persist session yourself, then restore it when required:
client.SetSession(session)
Instrument master:
err := instruments.CheckAndDownload(ctx, "data/instruments.csv")
items, err := instruments.Load("data/instruments.csv")
matches := instruments.FindInstruments(instruments.Filter{
Exchange: "NSE",
TradingSymbolContains: "NIFTY",
})
Live feed mode is selected once for the client. Reconnects restore active
subscriptions automatically.
feed := ws.NewWSClient(clientID, session.AccessToken, ws.ModeQuote)
feed.OnTickData(func(tick ws.TickData) {
// Consume tick quickly so the feed read loop is not blocked.
})
err := feed.Connect(ctx)
err = feed.Subscribe([]ws.Instrument{{
ExchangeSegment: "NSE_EQ",
SecurityID: "1333",
}})
SetDebug(true) prints complete request and response data, including secrets
and access tokens. Enable it only in a safe local environment.