Websocket
Introduction¶
The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
With a WebSocket API, you can receive quotes for all scrips across all Indian Exchanges during the market hours. The feeds are usually classified into two categories, Market Feed and Depth Feed. For every new connection, you will receive an acknowledgment (usually represented as "tk") followed by the exchange feed (usually represented as "tf").
Create Session¶
Create a new session using REST API (Authorization token is required as in the case of other API requests)
|Post|/ws/createWsSession|
Request
Parameters are as below (Payload). Use JSON content type
{"loginType":"API"}
Auth headers as shown below
Request Headers
Authorization: Bearer RC047 Fhrb2rt5G7Hav94X7aDZKObz2DPGVAIEQDoFcybzaeM4B3VmiYA3qbOtb4qUcRFYnHfuRGPRL3N9NZfUCc78Yrb5yFTg46dnlo
FlEUmOHOPzq7zGntAJARqDidhqk2rPDr08hgLqOR7TAnmeb5akhWnXuOyW1FyNAqCMMC82WdgiXI9VdKZRBTn5iMfV4Ur85
9plGDkjSbyV8iMpQAelCcm3l7Bb5DJfMnFnU7zUhhDv1eRsxmyXfBk1qWEprQNz
Response from Rest API
Create Connection¶
Request
{
"susertoken": sha256_encryption( sha256_encryption(session_id)),
"t": "c",
"actid": client_id+ "_API",
"uid": client_id + "_API",
"source": "API"
}
Response from WS
Sending heartbeat¶
The Sending heartbeat through web socket is a way to inform server the client is still active and accepting feeds. This way, server will be able to eliminate inactive or closed client connections from server side which results in better performance. Below are the request parameters to send heartbeat to server.
Request
{
"k": "",
"t": "h"
}
k = Send this as empty for heartbeat request
t = Type of request, 'h' stands for Heartbeat
Send heartbeat once in every 50 seconds.
This will NOT provide any response. Just accepting the message is the indication that your heartbeat request is received and feeds will keep coming. At present, sending heartbeat is not mandatory. However, it is advised to send heartbeat, otherwise the server will close connection after a defined timeframe if no data is being send or received. For example, if user has subscribed for some illiquid stocks which are not traded frequently, the client will not receive any feed for longer periods. This may lead to connection closure from server side. To avoid this, sending the heartbeat will inform the server that the client is still active and receiving feed.
Subscription to Market Data (LTP, Change, OHLC and Volume)¶
Request
{"k":"NFO|54957#MCX|239484","t":"t"}
t = Type of request, t for tick data
k = Params for subscription with pipe ‘|’ delimited tokens and exchange. Token and
exchange should be separated with #
Acknowledgement Response
{"t":"tk","pp":"2","ml":"1","e":"NFO","tk":"54957","ts":"NIFTY28JUL22C16600","ls":"50","ti":"0.05","c":
42.20","lp":"84.00","pc":"99.05","ft":"1658911102","oi":"7606750","o":"37.65","h":"98.00","l":"22.0
0","ap":"61.35","v":"129781850","bp1":"84.00","sp1":"84.20","bq1":"1000","sq1":"300"}
{"t":"tk","pp":"2","ml":"1","e":"MCX","tk":"239484","ts":"CRUDEOIL19SEP22","ls":"100","ti":"1.00","c"
"7522.00","ft":"1658911100","v":"469","oi":"429","bp1":"7564.00","sp1":"7567.00","bq1":"1","sq1":"
"7522.00","ft":"1658911100","v":"469","oi":"429","bp1":"7564.00","sp1":"7567.00","bq1":"1","sq1":"}
Feed Response
{"t":"tf","e":"NFO","tk":"54957","lp":"84.20","pc":"99.53","ft":"1658911102"}
{"t":"tf","e":"NFO","tk":"54957","lp":"84.35","pc":"99.88","ft":"1658911103","v":"129787100","bp1":"
84.30","sp1":"84.50","bq1":"1500","sq1":"350"}
t - type, tf – tick feed, tk - tick acknowledgement
e -exchange
tk - token
lp – LTP (Last traded price)
pc – Percentage change
cv – change value (Absolute change in price)
v – volume
o – open
h – high
l – low
c – close
ap – Average Price
symbol – Symbol Name
Un-Subscription to Market Data (LTP, Change, OHLC and Volume)¶
Request
{"k":"NFO|54957#MCX|239484","t":"u"}
t = Type of request, u for Un-subscription
k = Params for subscription with pipe ‘|’ delimited tokens and exchange. Token and
exchange should be separated with #
Response
No response will be coming for this. Unsubscribed tokens will no longer receive feed
Subscription to Depth data (LTP, Change, OHLC, OI and Volume)¶
Request
{"k":"NFO|54957#MCX|239484","t":"d"}
t = Type of request, t for tick data
k = Params for subscription with pipe ‘|’ delimited tokens and exchange. Token and
exchange should be separated with #
Acknowledgement Response
{"t":"dk","pp":"2","ml":"1","e":"NFO","tk":"54957","ts":"NIFTY28JUL22C16600","ls":"50","ti":"0.05","c":"42.20","lp":"76.40",
"pc":"81.04","uc":"469.90","lc":"0.05","ft":"1658910517","oi
":"7361100","ltq":"50","o":"37.65","h":"98.00","l":"22.00","ap":"60.72","v":"125888500","ltt":"13:58:37","tbq":"965400","ts
q":"980950","bp1":"76.30","sp1":"76.45","bp2":"76.25","sp2":"
76.50","bp3":"76.20","sp3":"76.55","bp4":"76.15","sp4":"76.60","bp5":"76.10","sp5":"76.65","bq1":"50","sq1":"650","bq2":
"2000","sq2":"1400","bq3":"3800","sq3":"2250","bq4":"2000","sq4":"3400","bq5":"7350","sq5":"2250","bo1":"1","so1":"2",
bo2":"9","so2":"8","bo3":"22","so3":"12","bo4":"12","so4":"16","bo5":"17","so5":"7"}
{"t":"dk","pp":"2","ml":"1","e":"MCX","tk":"239484","ts":"CRUDEOIL19SEP22","ls":"100","ti":"1.00","c":"7522.00","ft":"1658
910516","v":"454","oi":"437","ltq":"1","tbq":"144","tsq":"119",
"bp1":"7564.00","sp1":"7567.00","bp2":"7563.00","sp2":"7568.00","bp3":"7562.00","sp3":"7570.00","bp4":"7561.00","sp4":
"7571.00","bp5":"7560.00","sp5":"7572.00","bq1":"1","sq1":"5","bq
2":"1","sq2":"4","bq3":"5","sq3":"3","bq4":"4","sq4":"5","bq5":"3","sq5":"1","bo1":"1","so1":"3","bo2":"1","so2":"2","bo3":
"5","so3":"1","bo4":"4","so4":"4","bo5":"3","so5":"1","52h":"8382.00","52l":"6966.00","lp":"7568.00","pc":"0.61","o":"7479.00",
"h":"7588.00","l":"7479.00","ap":"7536.33","ltt":"13:58:12"}
Feed Response
{"t":"df","e":"NFO","tk":"54957","lp":"76.55","pc":"81.40","ft":"1658910520","ltq":"200"}
{"t":"df","e":"MCX","tk":"239484","ft":"1658910519","tsq":"117","sq1":"3","so1":"1"}
t - type, dk – depth acknowledgement, df - depth feed
e - exchange
tk - token
lp – LTP (Last traded price)
pc – Percentage change
cv – change value (Absolute change in price)
v – volume
o – open
h – high
l – low
c – close
ap – Average Price
ts – Symbol Name
bp1,bp2,bp3,bp4,bp5 - Depth buy price
sp1,sp2,sp3,sp4,sp5 - Depth sell price
bq1,bq2,bq3,bq4,bq5 - Depth buy quantity
sq1,sq2,sq3,sq4,sq5 - Depth sell quantity
bo1,bo2,bo3,bo4,bo5 - Depth buy order
so1,so2,so3,so4,so5 - Depth sell order
oi = open interest
ltq - last traded qty
ltt - last traded time
tbq - total buy qty
tsq - total sell qty
uc - upper circuit
lc - lower circuit
Un-Subscription to Depth data (LTP, Change, OHLC and Volume)¶
Request
{"k":"NFO|54957#MCX|239484","t":"ud"}
t = Type of request, ud for Un-subscription
k = Params for subscription with pipe ‘|’ delimited tokens and exchange. Token and
exchange should be separated with #
Response
No response will be coming for this. Unsubscribed tokens will no longer receive feed
Order Status Feed¶
The Order Status Feed WebSocket API allows clients to receive real-time updates on their order status. The process involves initial authentication to obtain an order token, followed by establishing a WebSocket connection using this token. Clients must maintain the connection by sending regular heartbeats to keep the connection alive.
Authentication
GET open-api/order-notify/ws/createWsToken
Headers
Authorization: Bearer
Response
{
"status": "Ok",
"message": "200",
"result":[
{
"orderToken": "23e16a457b4a035af850f4cf3f8da07e15cd7d7619a13b9d4c17ae33176"
}
]
}
Order token that will be used to establish the Websocket connection
Websocket Connection¶
Step 2: Connect to Websocket
wss://<Base URL>/open-api/order-notify/websocket
Request
After successful connection send order token and user ID to subscribe.
Sample Payload:
{
"orderToken": "<Order_Token>",
"userId": "<User_ID>"
}
Response
Upon successful connection user with start receiving their order feeds.Heartbeat¶
Step 3: Send Heartbeat
Request
To maintain the WebSocket connection, clients must send a heartbeat message every minute.
Notes: Failure to send a heartbeat every minute will result in the connection being closed. Clients must re-establish the connection if it is closed.
Order Status Feed¶
Request
Once connected, clients will receive real-time updates on their order status.
Sample Payload:
{
"t": "om",
"norenordno": "24070600000744",
"uid": "1332014",
"actid": "1332014",
"qty": "1",
"prc": "0.00",
"pcode": "I",
"remarks": "",
"rejreason": "RED:Margin Shortfall:INR 28,030.64 Available:INR 29.36 for C-1332014 [ABFSFREEDOM]",
"prctyp": "MKT",
"ret": "DAY",
"dscqty": "0",
"trantype": "B",
"exch": "NSE",
"tsym": "MRF-EQ",
"status": "REJECTED",
"reporttype": "Rejected"
}
Summary:
- Authenticate: Obtain the
order_tokenby callingGET /order- notify/ws/createWsTokenwith a valid bearer token. - Connect: Establish a WebSocket connection to
wss://<Base URL>/order-notify/websocketand sendorderTokenanduserIdto subscribe. - Heartbeat: Send a heartbeat message every minute to keep the connection alive.
- Receive Updates: Listen for real-time order status updates. Additional Notes: Ensure your WebSocket client can handle reconnections in case of disconnections. Maintain the security of your bearer token and order token at all times. By following the steps outlined above, clients can successfully integrate with the Order Status Feed WebSocket API and receive timely updates on their orders.
Webhook Order Updates¶
Webhook empowers traders to receive real-time updates on order-related events directly from our system. Traders can register their webhook endpoint, and our system will notify them asynchronously whenever there is a relevant change in the order status. Use the methods below to subscribe and unsubscribe for order updates.
Authorization Type: Bearer Token
Subscribe¶
To subscribe to our Order Update Webhook service and start receiving real-time notifications, users can effortlessly register their webhook endpoint using the URL below. Users can subscribe up to 5 URLs per user ID.
URL: <Base URL>/api/webhook/subscribe
Sample Request¶
Unsubscribe¶
To unsubscribe from our Order Update Webhook service and cease receiving notifications, users can easily deactivate the webhook by utilizing the URL below.
URL: <Base URL>/api/webhook/unsubscribe
Sample Request¶
Sample Order Update¶
{
"orderno": "123456789",
"actid": "DEMO123",
"exch": "NSE",
"tsym": "INFY-EQ",
"qty": "1",
"prc": "1594.00",
"prd": "CNC",
"status": "",
"reporttype": "",
"trantype": "",
"prctyp": "",
"ret": "",
"fillshares": "",
"avgprc": "",
"fltm": "",
"flid": "",
"flqty": "",
"flprc": "",
"rejreason": "",
"exchordid": "",
"cancelqty": "",
"remarks": "",
"dscqty": "",
"trgprc": "",
"snonum": "",
"snoordt": "",
"blprc": "",
"bpprc": "",
"trailprc": "",
"exch_tm": "",
"amo": "",
"tm": "",
"ntm": "",
"kidid": "",
"sno_fillid": "",
"pcode": ""
}
| Field | TYPE | Description |
|---|---|---|
| orderno | string | Order number |
| actid | string | Account ID |
| exch | string | Exchange |
| tsym | string | Trading symbol |
| qty | int | Quantity |
| prc | float | Price |
| prd | string | Product type |
| status | string | Order status |
| reporttype | string | Order report type |
| trantype | string | Transaction type (Buy/Sell) |
| prctyp | string | Price type |
| ret | string | Retention type |
| fillshares | int | Filled shares |
| avgprc | float | Average price |
| fltm | string | Filled time |
| flid | string | Filled ID |
| flqty | int | Filled quantity |
| flprc | float | Filled price |
| rejreason | string | Reject reason |
| exchordid | string | Exchange order ID |
| cancelqty | int | Cancelled quantity |
| remarks | string | Remarks |
| dscqty | int | Disclosed quantity |
| trgprc | float | Trigger price |
| snonum | string | SNO order number (BO CO orders only) |
| snoordt | string | SNO order date (BO CO orders only) |
| trailprc | float | Trailing price |
| exch_tm | string | Exchange time |
| amo | string | AMO type |
| no_fillid | string | SNO filled ID (BO CO orders only) |
| pcode | string | Product code |