Upstox API Reference
Upstox API Reference
Upstox API is a set of rest APIs that provide data required to build a complete investment and trading platform. Execute orders in real time, manage user
portfolio, stream live market data (using Websocket), and more, with the easy to understand API collection.
All requests are over HTTPS and the requests are sent with the content-type ‘application/json’. Developers have the option of choosing the response type as
JSON or CSV for a few API calls.
To be able to use these APIs you need to create an App in the Developer Console and generate your apiKey and apiSecret . You can use a redirect URL
which will be called after the login flow.
It is highly recommended that you do not embed the apiSecret in your frontend app. Create a remote backend which does the handshake on behalf of the
frontend app. Marking the apiSecret in the frontend app will make your app vulnerable to threats and potential issues.
Create an app
Create an app with the App Type API Trading in the Developer Console. Give your app a name, a description and a redirect URI. This redirect URI is used
after login to pass an authorization code.
Note that if you are distributing your app to the public, please ensure that the redirect URI is stored in your server and not in your client-side application!
API key and secret
Once your app is created, you will get an API KEY and API SECRET. Your API KEY and API SECRET are used to identify the source of the connection and
ensure that it is really you. Your API KEY can be distributed on the client app but your API SECRET should be stored on your backend server.
Installation
To facilitate easy use of the APIs, we have developed a set of libraries for different languages. If you are using one of those programming languages, you can
use the library functions for the calls/functions listed below.
Authorization
curl \
-u {your_api_key}:{your_api_secret} \
-H 'Content-Type: application/json' \
-H 'x-api-key: {your_api_key}' \
-d '{"code" : "{code_from_login_response}", "grant_type" : "authorization_code", "redirect_uri" : "{your_redirect_uri}"}' \
-X POST 'https://api.upstox.com/index/oauth/token'
1. Open your web browser and go to the login URL. Your login URL is https://api.upstox.com/index/dialog/authorize?apiKey={your_api_key}&redirect_uri={y
our_redirect_uri}&response_type=code . On the libraries, you can get this URL by setting the apiKey and then calling the getLoginUri function with the red
irect_uri as a parameter. Note that the redirect_uri used here must match the redirect_uri specified in your App settings on the Developer Console.
2. Once redirected, it opens the Upstox login page where you enter your UCC, password and date of birth to authenticate yourself.
Login authentication screen. Your Year of Birth is your passcode.
3. After authentication it asks for your permission to allow the app to access your credentials.
Clicking Accept will then take you to the redirect URI with the authorization code as a parameter
4. Once you accept the permissions, Upstox redirects you to the redirectUri which was sent in step 1 with a parameter called code . This step should be
done in your backend app server.
5. With the newly generated code , you need to now generate an access token. To get the access token, you need to call the following URL https://api.u
pstox.com/index/oauth/token and pass the following parameters: api_key , redirect_uri , code , grant_type . This call is shown on the right side in the
code tab.
Note, the grant_type value must always be authorization_code . Do not change this value.
Users
Profile
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/index/profile'
Sample response
{
"code":200,
"status":"OK",
"timestamp":"2017-07-18T10:32:02+05:30",
"message":"success",
"data":{
"client_id": "240001",
"name": "Raj Prakash",
"email": "[email protected]",
"phone": "9820098200",
"exchanges_enabled":[
"BSE_EQ",
"NSE_EQ",
"NSE_FO",
"MCX_FO",
"NCD_FO",
"BCD_FO"
],
"products_enabled":[
"OCO",
"D",
"CO",
"I"
]
}
}
This allows to fetch the complete information of the user who is logged in.
RESPONSE ATTRIBUTES
exchanges_enabled string Lists the exchanges to which the user has access
products_enabled array Lists the products types to which the user has access
Balance
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/live/profile/balance'
Sample response
{
"code": 200,
"status": "OK",
"timestamp": "2017-05-15T14:35:57+05:30",
"message": "success",
"data": {
"equity": {
"used_margin": 67.2125,
"unrealized_mtm": 0,
"realized_mtm": 0,
"payin_amount": 0,
"span_margin": 0,
"adhoc_margin": 0,
"notional_cash": 0,
"available_margin": 99320.78,
"exposure_margin": 0
},
"commodity": {
"used_margin": 0,
"unrealized_mtm": 0,
"realized_mtm": 0,
"payin_amount": 0,
"span_margin": 0,
"adhoc_margin": 0,
"notional_cash": 0,
"available_margin": 5200.45,
"exposure_margin": 0
}
}
}
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/live/profile/balance/commodity'
Sample response
{
"code": 200,
"status": "OK",
"timestamp": "2017-05-15T14:35:57+05:30",
"message": "success",
"data": {
"commodity": {
"used_margin": 0,
"unrealized_mtm": 0,
"realized_mtm": 0,
"payin_amount": 0,
"span_margin": 0,
"adhoc_margin": 0,
"notional_cash": 0,
"available_margin": 5200.45,
"exposure_margin": 0
}
}
}
Request Parameter
parameter
security
type
commodity
RESPONSE ATTRIBUTES
Positive values denote the amount blocked into a Open order or position. Negative value denotes the amount being
used_margin number
released.
Positions
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/live/profile/positions'
Sample response
{
"code": 200,
"status": "OK",
"timestamp": "2017-07-27T13:26:33+05:30",
"message": "success",
"data": [
{
"exchange": "NSE_FO",
"product": "D",
"symbol": "NIFTY17AUG10100CE",
"token": 66416,
"buy_amount": 0,
"sell_amount": 0,
"buy_quantity": 0,
"sell_quantity": 0,
"cf_buy_amount": 0,
"cf_sell_amount": 751548.75,
"cf_buy_quantity": 0,
"cf_sell_quantity": 75,
"avg_buy_price": "",
"avg_sell_price": 108.6,
"net_quantity": -75,
"close_price": 108.6,
"last_traded_price": 128,
"realized_profit": "",
"unrealized_profit": -1455.0000000000005,
"cf_avg_price": "72.00"
}
]
}
Fetches the current positions for the user for the current day
RESPONSE ATTRIBUTES
product string Shows if the order was either Intraday, Delivery, CO or OCO
symbol string Shows the trading symbol which could be a combination of symbol name, instrument, expiry date etc
buy_amount number Amount at which the qty is bought during the day
sell_amount number Amount at which the qty is sold during the day
cf_buy_amount number Amount at which the qty was bought in the previous session
attribute datatype description
cf_sell_amount number Amount at which the qty was sold in the previous session
avg_buy_price number Average price at which the day qty was bought. Default is empty string
avg_sell_price number Average price at which the day qty was sold. Default is empty string
net_quantity number Quantity left after nullifying Day and CF buy quantity towards Day and CF sell quantity
realized_profit number Day PnL generated against closed positions. Default is empty string
unrealized_profit number Day PnL generated against open positions. Default is empty string
It indicates the consolidated price across all the carry forward orders. (Note: Corporate actions are not taken into
cf_avg_price number
consideration)
Holdings
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/live/profile/holdings'
Sample response
{
"code": 200,
"status": "OK",
"timestamp": "2017-07-27T13:06:03+05:30",
"message": "success",
"data": [
{
"instrument": [
{
"exchange": "NSE_EQ",
"symbol": "ASHOKLEY",
"token": 212
},
{
"exchange": "BSE_EQ",
"symbol": "ASHOKLEY",
"token": 500477
}
],
"product": "D",
"collateral_type": "WC",
"cnc_used_quantity": 0,
"quantity": 10,
"collateral_qty": 0,
"haircut": 25,
"avg_price": "76"
}
]
}
Fetches the holdings which the user has bought/sold in previous trading sessions
RESPONSE ATTRIBUTES
symbol string Shows the trading symbol which could be a combination of symbol name, instrument, expiry date etc
product string Shows if the order was either Intraday, Delivery, CO or OCO
attribute datatype description
haircut number This is the haircut percentage applied from RMS (applicable incase of collateral)
It indicates the consolidated price across all the orders placed for the scrip. (Note: Corporate actions are not taken into
avg_price number
consideration)
Master Contracts
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/index/master-contract/nse_eq'
Sample response
{
"code": 200,
"status": "OK",
"timestamp": "2017-07-18T10:45:33+05:30",
"message": "success",
"data": [
"exchange,token,parent_token,symbol,name,closing_price,expiry,strike_price,tick_size,lot_size,instrument_type,isin",
"NSE_EQ,16921,,20MICRONS,20 MICRONS LTD,39.7,,,5,1,EQUITY,INE144J01027",
"NSE_EQ,11774,,3IINFOTECH,3I INFOTECH LTD.,4.45,,,5,1,EQUITY,INE748C01020",
"NSE_EQ,474,,3MINDIA,3M INDIA LIMITED,13686.2,,,5,1,EQUITY,INE470A01017",
"NSE_EQ,11868,,63MOONS,63 MOONS TECHNOLOGIES LTD,62.95,,,5,1,EQUITY,INE111B01023",
"NSE_EQ,11058,,8KMILES,8K MILES SOFT SERV LTD,541.6,,,5,1,EQUITY,INE650K01021",
"NSE_EQ,20906,,A2ZINFRA,A2Z INFRA ENGINEERING LTD,43.1,,,5,1,EQUITY,INE619I01012",
"NSE_EQ,11341,,AIFL,ASHAPURA INTI FASHION LTD,401.4,,,5,1,EQUITY,INE428O01016",....
]
}
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/index/master-contract/nse_index'
Sample response
{
"code":200,
"status":"OK",
"timestamp":"2017-07-19T10:46:26+05:30",
"message":"success",
"data":[
"exchange,token,parent_token,symbol,name,closing_price,expiry,strike_price,tick_size,lot_size,instrument_type,isin",
"NSE_INDEX,,,NIFTY_50,Nifty 50,9827.15,,,,,INDEX,",
"NSE_INDEX,,,NIFTY_IT,Nifty IT,10493.15,,,,,INDEX,",
"NSE_INDEX,,,NIFTY_NEXT_50,Nifty Next 50,27277.25,,,,,INDEX,",
"NSE_INDEX,,,NIFTY_BANK,Nifty Bank,24022.05,,,,,INDEX,",
"NSE_INDEX,,,NIFTY_MID100_FREE,Nifty MID100 Free,18216.6,,,,,INDEX,",
"NSE_INDEX,,,NIFTY_500,Nifty 500,8589.8,,,,,INDEX,",...
]
}
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/index/master-contract/nse_eq?token=2885'
Sample response
{
"code":200,
"status":"OK",
"timestamp":"2017-07-18T10:47:32+05:30",
"message":"success",
"data":{
"lower_circuit":1396.6,
"upper_circuit":1706.9,
"instrument_name":"EQUITY",
"gn":-1,
"gd":-1,
"pn":-1,
"pd":-1,
"circuit_limit":"1396.60-1706.90",
"status":"Eligible",
"yearly_low":930,
"yearly_high":1550,
"symbol":"RELIANCE",
"lot_size":1,
"exchange":"NSE_EQ",
"closing_price":1551.75,
"open_interest":"",
"tick_size":5,
"name":"RELIANCE INDUSTRIES LTD",
"token":2885,
"isin":"INE002A01018"
}
}
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/index/master-contract/nse_eq?symbol=infy'
Sample response
{
"code":200,
"status":"OK",
"timestamp":"2017-07-18T10:49:33+05:30",
"message":"success",
"data":{
"lower_circuit":885.65,
"upper_circuit":1082.45,
"instrument_name":"EQUITY",
"gn":-1,
"gd":-1,
"pn":-1,
"pd":-1,
"circuit_limit":"885.65-1082.45",
"status":"Eligible",
"yearly_low":901,
"yearly_high":1196.05,
"symbol":"INFY",
"lot_size":1,
"exchange":"NSE_EQ",
"closing_price":984.05,
"open_interest":"",
"tick_size":5,
"name":"INFOSYS LIMITED",
"token":1594,
"isin":"INE009A01021"
}
}
getMasterContract allows to download master contract as csv. The master contract contains all necessary information about all available contracts. Gets all
contracts or filter contracts by exchange segment. You can also get the details on a single scrip by sending the either symbol or token. Sending the exchange
is mandatory.
Request Parameter
parameter description
symbol optional Trading symbol which could be a combination of symbol name, instrument, expiry date etc
For BSE and NSE enter the scrip name as RELIANCE , AXISBANK etc
For NSE futures enter the symbol as NIFTY17JUNFUT where NIFTY is the scrip name, 17 [YY] is the year, JUN [MMM] is the month name, FUT is the
instrument
For NSE monthly options enter the symbol as NIFTY17JUN9500CE where NIFTY is the scrip name, 17[YY] is the year, JUN [MMM] is the month name,
9500 is the strike price and CE is the option type
For NSE weekly options it should be BANKNIFTY1750423200CE where BANKNIFTY is the scrip name, 17[YY] is the year, 5 [M] is the month, 04 [DD] is the
day, 23200 is the strike price and CE is the option type. The format of month will be different for October, November and December. It will show like this
BANKNIFTY17111623200CE where 11 is the month and 16 is the day.
Orders
Orders History
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/live/orders'
Sample response
{
"code": 200,
"status": "OK",
"timestamp": "2017-03-28T17:01:02+05:30",
"message": "success",
"data": [
{
"exchange": "NSE_EQ",
"token": 2885,
"symbol": "RELIANCE",
"product": "I",
"order_type": "L",
"duration": "DAY",
"price": 1080,
"trigger_price": 0,
"quantity": 1,
"disclosed_quantity": 0,
"transaction_type": "B",
"average_price": 1080,
"traded_quantity": 1,
"message": "",
"exchange_order_id": "1100000000006972",
"parent_order_id": "NA",
"order_id": "170328000000030",
"exchange_time": "28-Mar-2017 12:42:42",
"time_in_micro": "1490697859120172",
"status": "complete",
"is_amo": false,
"valid_date": "--",
"order_request_id": "1"
}
]
}
This API provides the list of orders placed by the user. The orders placed by the user is transient for a day and is cleared by the end of the trading session.
This API returns all states of the orders, namely, open, pending, and filled ones.
RESPONSE ATTRIBUTES
symbol string Shows the trading symbol which could be a combination of symbol name, instrument, expiry date etc
product string Shows if the order was either Intraday, Delivery, CoverOrder or OneCancelsOther
attribute datatype description
DAY
duration string
IOC
GTD
trigger_price number If the order was a stop loss order then the trigger price set is mentioned here
disclosed_quantity number The quantity that should be disclosed in the market depth
transaction_type string Indicates whether the order was a buy or sell order
traded_quantity string The total quantity traded from this particular order
message string Indicates the reason when any order is rejected, not modified or cancelled
exchange_order_id string Unique order ID assigned by the exchange for the order placed
parent_order_id string In case the order is part of the second or third leg of a CO or OCO, the parent order ID is indicated here
order_id string Unique order ID assigned internally for the order placed
exchange_time string User readable time at which the order was placed or updated
time_in_micro string Time in microseconds (Unix Epoch) at which the order was placed or updated
status string Indicates the current status of the order. Valid order status’ are outlined in the table below
valid_date string If the order is GTD (Good-Till-Date), this shows the valid date
order_request_id string Apart from 1st order it shows the count of how many requests were sent
Orders Details
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/live/orders/{order_id}'
Sample response
{
"code": 200,
"status": "OK",
"timestamp": "2017-08-03T15:16:38+05:30",
"message": "success",
"data": [
{
"exchange": "NSE_EQ",
"token": 3045,
"symbol": "SBIN",
"product": "I",
"order_type": "M",
"duration": "DAY",
"price": 0,
"trigger_price": 0,
"quantity": 1,
"disclosed_quantity": 0,
"transaction_type": "B",
"average_price": 299.4,
"traded_quantity": 1,
"message": "",
"exchange_order_id": "1100000000062530",
"parent_order_id": "NA",
"order_id": "170803000000038",
"exchange_time": "03-Aug-2017 15:03:42",
"time_in_micro": "1501752667641223",
"status": "complete",
"is_amo": false,
"valid_date": "",
"fill_leg": "111",
"order_request_id": "1",
"report": "",
"text": ""
},
{
"exchange": "NSE_EQ",
"token": 3045,
"symbol": "SBIN",
"product": "I",
"order_type": "M",
"duration": "DAY",
"price": 0,
"trigger_price": 0,
"quantity": 1,
"disclosed_quantity": 1,
"transaction_type": "B",
"average_price": 0,
"traded_quantity": 0,
"message": "",
"exchange_order_id": "1100000000062530",
"parent_order_id": "NA",
"order_id": "170803000000038",
"exchange_time": "03-Aug-2017 15:03:42",
"time_in_micro": "1501752667639709",
"status": "open",
"is_amo": false,
"valid_date": "",
"fill_leg": "0",
"order_request_id": "1",
"report": "",
"text": ""
},
{
"exchange": "NSE_EQ",
"token": 3045,
"symbol": "SBIN",
"product": "I",
"order_type": "M",
"duration": "DAY",
"price": 0,
"trigger_price": 0,
"quantity": 1,
"disclosed_quantity": 1,
"transaction_type": "B",
"average_price": 0,
"traded_quantity": 0,
"message": "",
"exchange_order_id": "",
"parent_order_id": "NA",
"order_id": "170803000000038",
"exchange_time": "",
"time_in_micro": "1501752667600392",
"status": "open pending",
"is_amo": false,
"valid_date": "",
"fill_leg": "0",
"order_request_id": "1",
"report": "",
"text": ""
},
{
"exchange": "NSE_EQ",
"token": 3045,
"symbol": "SBIN",
"product": "I",
"order_type": "M",
"duration": "DAY",
"price": 0,
"trigger_price": 0,
"quantity": 1,
"disclosed_quantity": 1,
"transaction_type": "B",
"average_price": 0,
"traded_quantity": 0,
"message": "",
"exchange_order_id": "",
"parent_order_id": "NA",
"order_id": "170803000000038",
"exchange_time": "",
"time_in_micro": "1501752667308275",
"status": "validation pending",
"is_amo": false,
"valid_date": "",
"fill_leg": "0",
"order_request_id": "1",
"report": "",
"text": ""
},
{
"exchange": "NSE_EQ",
"token": 3045,
"symbol": "SBIN",
"product": "I",
"order_type": "M",
"duration": "DAY",
"price": 0,
"trigger_price": 0,
"quantity": 1,
"disclosed_quantity": 1,
"transaction_type": "B",
"average_price": 0,
"traded_quantity": 0,
"message": "",
"exchange_order_id": "",
"parent_order_id": "NA",
"order_id": "170803000000038",
"exchange_time": "",
"time_in_micro": "1501752667303641",
"status": "put order req received",
"is_amo": false,
"valid_date": "",
"fill_leg": "0",
"order_request_id": "1",
"report": "",
"text": ""
}
]
}
This API provides the details of the particular order the user has placed. The orders placed by the user is transient for a day and is cleared by the end of the
trading session. This API returns all states of the orders, namely, open, pending, and filled ones.
REQUEST PARAMETERS
parameters
RESPONSE ATTRIBUTES
symbol string Shows the trading symbol which could be a combination of symbol name, instrument, expiry date etc
product string Shows if the order was either Intraday, Delivery, CoverOrder or OneCancelsOther
DAY
duration string
IOC
GTD
trigger_price number If the order was a stop loss order then the trigger price set is mentioned here
disclosed_quantity number The quantity that should be disclosed in the market depth
transaction_type string Indicates whether the order was a buy or sell order
traded_quantity string The total quantity traded from this particular order
message string Indicates the reason when any order is rejected, not modified or cancelled
exchange_order_id string Unique order ID assigned by the exchange for the order placed
parent_order_id string In case the order is part of the second or third leg of a CO or OCO, the parent order ID is indicated here
order_id string Unique order ID assigned internally for the order placed
exchange_time string User readable time at which the order was placed or updated
time_in_micro string Time in microseconds (Unix Epoch) at which the order was placed or updated
status string Indicates the current status of the order. Valid order status’ are outlined in the table below
valid_date string If the order is GTD (Good-Till-Date), this shows the valid date
order_request_id string Apart from 1st order it shows the count of how many requests were sent
ORDER STATUS
put order req received Order was received by our order management system successfully
validation pending Will get sent to the exchange after validating risk management rules
open pending Order was received by exchange and awaiting open confirmation
open Order is open having order type as limit. Can be modified or cancelled
trigger pending Order is open having order type as SL or SL-M. Can be modified or cancelled
rejected Order was rejected. Please check the message field for rejection reason
modify validation pending Will get sent to the exchange after validating risk management rules
not modified Modification was rejected. Please check the message field for rejection reason
not cancelled Cancellation request was rejected. Please check the message field for rejection reason
after market order req received AMO was received by our order management system successfully. Can be modified or cancelled
modify after market order req received AMO modification was sucessful
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/live/trade-book'
Sample response
{
"code": 200,
"status": "OK",
"timestamp": "2017-08-03T15:24:06+05:30",
"message": "success",
"data": [
{
"exchange": "NSE_EQ",
"token": 3045,
"symbol": "SBIN",
"product": "I",
"order_type": "M",
"transaction_type": "B",
"traded_quantity": 1,
"exchange_order_id": "1100000000062530",
"order_id": "170803000000038",
"exchange_time": "03-Aug-2017 15:03:42",
"time_in_micro": "1501752667641223",
"traded_price": 299.4,
"trade_id": "50091502"
},
{
"exchange": "NSE_EQ",
"token": 3045,
"symbol": "SBIN",
"product": "I",
"order_type": "M",
"transaction_type": "B",
"traded_quantity": 1,
"exchange_order_id": "1100000000068410",
"order_id": "170803000000040",
"exchange_time": "03-Aug-2017 15:23:01",
"time_in_micro": "1501753826450445",
"traded_price": 294.55,
"trade_id": "50091780"
},
{
"exchange": "NSE_EQ",
"token": 694,
"symbol": "CIPLA",
"product": "I",
"order_type": "M",
"transaction_type": "B",
"traded_quantity": 1,
"exchange_order_id": "1000000000054988",
"order_id": "170803000000041",
"exchange_time": "03-Aug-2017 15:23:15",
"time_in_micro": "1501753840617453",
"traded_price": 559.7,
"trade_id": "75686"
}
]
}
RESPONSE ATTRIBUTES
symbol string Shows the trading symbol which could be a combination of symbol name, instrument, expiry date etc
product string Shows if the order was either Intraday, Delivery, CO or OCO
attribute datatype description
transaction_type string Indicates whether the order was a buy or sell order
traded_quantity string The total quantity traded from this particular order
exchange_order_id string Unique order ID assigned by the exchange for the order placed
order_id string Unique order ID assigned internally for the order placed
time_in_micro string Time in microseconds (Unix Epoch) when the trade occurred
Trade History
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/live/orders/{order_id}/trades'
Sample response
{
"code": 200,
"status": "OK",
"timestamp": "2017-05-15T14:48:52+05:30",
"message": "success",
"data": [
{
"exchange": "NSE_EQ",
"token": 2885,
"symbol": "RELIANCE",
"product": "I",
"order_type": "M",
"transaction_type": "B",
"traded_quantity": 1,
"exchange_order_id": "1200000003336976",
"order_id": "170515000047270",
"exchange_time": "15-May-2017 13:56:31",
"time_in_micro": "1494836825761723",
"traded_price": 0,
"trade_id": "51345784",
}
]
}
REQUEST PARAMETERS
parameters
RESPONSE ATTRIBUTES
symbol string Shows the trading symbol which could be a combination of symbol name, instrument, expiry date etc
product string Shows if the order was either Intraday, Delivery, CO or OCO
transaction_type string Indicates whether the order was a buy or sell order
traded_quantity string The total quantity traded from this particular order
exchange_order_id string Unique order ID assigned by the exchange for the order placed
order_id string Unique order ID assigned internally for the order placed
time_in_micro string Time in microseconds (Unix Epoch) when the trade occurred
Place Order
curl \
-H 'Content-type: application/json' \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-d '{order_object}' \
-X POST 'https://api.upstox.com/live/orders'
REQUEST PARAMETERS
parameters description
symbol required Shows the trading symbol which could be a combination of symbol name, instrument, expiry date etc
product required Shows if the order was either Intraday, Delivery, CO or OCO
trigger_price optional If the order is a stop loss order then the trigger price set is mentioned here
disclosed_quantity optional The quantity that should be disclosed in the market depth
stoploss optional Set a difference from the entry price for stoploss. Use in case the product is OCO
squareoff optional Set a difference from the entry price for profit. Use in case the product is OCO
trailing_ticks optional Indicates the number of ticks if the order placed was a trailing order. se in case the product is OCO
Modify Order
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-d '{order_object}' \
-X PUT 'https://api.upstox.com/live/orders/{order_id}'
Modify the order by updating the order quantity. For modification order_id is mandatory. With order_id you need to send the optional parameter which needs to
be modified. In case the optional paramters aren’t sent, the default will be considered from the original order. Following are those:
REQUEST PARAMETERS
parameters description
order_id required the order id for which the order must be modified
trigger_price optional If the order was a stop loss order then the trigger price set is mentioned here
disclosed_quantity optional The quantity that should be disclosed in the market depth
Cancel Order
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X DELETE 'https://api.upstox.com/live/orders/{list of comma separated open order(s) ID}'
{
"code": 200,
"status": "OK",
"timestamp": "2017-08-31T06:06:00+00:00",
"message": "success",
"data": "Cancellation sent for [170831000041785,170831000041799,170831000041776,170831000041769]"
}
REQUEST PARAMETERS
parameters description
order_id required comma separated order ID assigned internally for the order placed
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X DELETE 'https://api.upstox.com/live/orders'
{
"code": 200,
"status": "OK",
"timestamp": "2017-08-31T06:03:07+00:00",
"message": "Sent cancellation requests for all open order(s)",
"data": "170831000041521,170831000041591,170831000041539,170831000041553"
}
Feed
Live Feed
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/live/feed/now/{exchange}/{symbol}/{type}'
#If you request for LTP, the following will be the response
{
"code": 200,
"status": "OK",
"timestamp": "2017-05-15T16:45:12+05:30",
"message": "feed",
"data": {
"timestamp": 1494843956000,
"exchange": "NSE_EQ",
"symbol": "RELIANCE",
"ltp": 1344.1,
"close": 1350.45
}
}
#If you request for Full, the following will be the response for indexes
{
"code":200,
"status":"OK",
"timestamp":"2017-07-19T11:11:32+05:30",
"message":"feed",
"data":{
"timestamp":1454498203000,
"exchange":"NSE_INDEX",
"symbol":"NIFTY_100",
"ltp":7452.05,
"open":7482.5,
"high":7510.4,
"low":7441.5,
"close":7452.05,
"yearly_high":9105.4,
"yearly_low":7340.95
}
}
#If you request for Full, the following will be the response for other scrips
{
"code": 200,
"status": "OK",
"timestamp": "2017-05-15T15:10:42+05:30",
"message": "feed",
"data": {
"timestamp": 1494837991000,
"exchange": "NSE_FO",
"symbol": "NIFTY17MAYFUT",
"ltp": 9446.8,
"open": 9448,
"high": 9454.4,
"low": 9437,
"close": 9414.8,
"vtt": 2802525,
"atp": 9446.71,
"oi": 21256725,
"spot_price": 9442.15,
"total_buy_qty": 830925,
"total_sell_qty": 858375,
"lower_circuit": 8473.35,
"upper_circuit": 10356.3,
"bids": [
{
"quantity": 900,
"price": 9446.7,
"orders": 2
},
{
"quantity": 75,
"price": 9446.65,
"orders": 1
},
{
"quantity": 1050,
"price": 9446.6,
"orders": 4
},
{
"quantity": 150,
"price": 9446.55,
"orders": 1
},
{
"quantity": 75,
"price": 9446.1,
"orders": 1
}
],
"asks": [
{
"quantity": 450,
"price": 9447,
"orders": 1
},
{
"quantity": 450,
"price": 9447.3,
"orders": 1
},
{
"quantity": 900,
"price": 9447.4,
"orders": 1
},
{
"quantity": 75,
"price": 9447.45,
"orders": 1
},
{
"quantity": 75,
"price": 9447.55,
"orders": 1
}
],
"ltt": 1494837991000
}
}
attribute description
symbol Shows the trading symbol which could be a combination of symbol name, instrument, expiry date etc
There are two ways in which you can request for feeds.
RESPONSE ATTRIBUTES
For LTP
close string Close is the close price of the symbol from the previous session of trading
open string It will show the open price of the trading session
high string This will show the high price of the trading session
low string This will show the low price of the trading session
close string Close is the close price of the symbol from the previous session of trading
yearly_high number Indicates the highest value of the index in the current year
yearly_low number Indicates the lowest value of the index in the current year
open string It will show the open price of the trading session
high string This will show the high price of the trading session
low string This will show the low price of the trading session
close string Close is the close price of the symbol from the previous session of trading
atp number It stands for average traded price for the entire quantity traded in the exchange
oi number OI stands for open interest. It is applicable on future and option segments
spot_price number It means the underlying price of a stock. It is applicable on future and option segments
total_buy_qty string This show the total nos of bid quantity available for trading
total_sell_qty string This show the total nos of ask quantity available for trading
ask orders string Number of orders requesting bids ask at this price
Subscribe
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/live/feed/sub/{type}/{exchange}/?symbol={list of comma separated symbols}'
attribute description
Comma seperated list of trading symbols which could be a combination of symbol name, instrument, expiry date etc. Note: The symbols
symbol
should not contain any spaces eg. ‘reliance,mrf,niftymay17fut’
There are two ways in which you can request for feeds.
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/live/feed/unsub/{type}/{exchange}?symbol={list of comma seperated symbols}'
Unsubscribe for the live data feed for the instruments chosen
attribute description
Comma seperated list of trading symbols which could be a combination of symbol name, instrument, expiry date etc. Note: The symbols
symbol
should not contain any spaces eg. ‘reliance,mrf,niftymay17fut’
There are two ways in which you can request for feeds.
Historical
OHLC
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/historical/ohlc/{exchange}/{symbol}?interval={interval}&start_date={start_date}&end_date={end_date}&format={format}'
# Sample response
{
"code": 200,
"status": "OK",
"timestamp": "",
"message": "ohlc",
"data": [
{
"timestamp": 1485973800000,
"open": 1050,
"high": 1050.8,
"low": 1038.25,
"close": 1043.5,
"volume": 2079908,
"cp": 1045.55
}
]
}
attribute description
attribute description
symbol required The symbol of the instrument. This must match exactly as given in the master contract csv. eg:NIFTY17MARFUT
Default: 1day
Allowed Values:
1MINUTE
5MINUTE
10MINUTE
60MINUTE
1DAY
1WEEK
1MONTH
Default: today
end_date optional Date format: DD-MM-YYYY
eg: 01-02-2017
csv
format optional
json (default)
TradeButton
TradeButton is a JS plugin that lets you add Upstox branded order placement buttons to your webpage. The buttons can be preconfigured to place a certain
order, or dynamically assigned orders.
Apart from the branded buttons, the TradeButton functionality can be invoked on any custom button or link on the page. More details on it below.
Getting Started
Include the following scripts at the end of body.
BRANDED BUTTONS
The orders linked to each button must be added to the upstoxTradesConfig object under the correct button-name . To add more orders to static buttons after
page load use UpstoxButton.addOrderTobutton .
CUSTOM BUTTONS
You can also configure custom buttons and hyperlinks to function as TradeButtons. Add the attributes upstox-tradebutton and button-name , and assign a
unique buttonName .
API
The plugin also includes an extensive API to add/manage TradeButtons dynamically on your page. The following methods are available as part of the API:
CREATING A BUTTON
Parameter Remarks
buttonName String - this will be assigned as an attribute to the TradeButton, must be unique in the page
type String - ( 'tradebutton' / 'a' / 'button' ) - specify the type of button, defaults to 'tradebutton'
Returns Remarks
Use the addOrder() method present on the created TradeButton object for adding orders to it.
Parameter Remarks
Returns Remarks
UPSTOXBUTTON.BUTTONEXISTS( <BUTTONNAME> )
Parameter Remarks
buttonName String
Returns Remarks
In order to earn a share of the brokerage generated by your TradeButtons, it is necessary to include either your API Key or Referral code as shown to the right.
Each order associated with a TradeButton, is available in upstoxTradesConfig object. The format of every order must be as shown to the right
Param Exp
quantity The quantity to trade (Must be a multiple of lot size for the scrip)
price The price at which the order should be placed (To be set as 0 for market orders)
orderType Supported types are limit ( 'l' ), market ( 'm' ), Stop-Loss Limit ( 'sl' ), Stop-Loss Market ( 'sl-m' )
Webhook (Postback)
Sample response
{
exchange: 'NSE_EQ',
token: 2885,
symbol: 'RELIANCE',
product: 'I',
order_type: 'L',
duration: 'DAY',
price: 0,
trigger_price: 0,
quantity: 1,
disclosed_quantity: 0,
transaction_type: 'B',
average_price: 0,
traded_quantity: 0,
message: '',
exchange_order_id: '1200000001405029',
parent_order_id: 'NA',
order_id: '170516000022221',
exchange_time: '16-May-2017 10:16:54',
time_in_micro: '1494910048512349',
status: 'open',
is_amo: false,
valid_date: '',
order_request_id: '2',
checksum : '500ba8b198d21f7ecea08aa2eb7da4b0'
}
The Postback API sends a POST request with order update as JSON payload to the registered postback_url of your app when an order’s status changes. This
enables you to get real-time updates to your orders.
RESPONSE
Response is same as order update. One more field is added in this response as checksum.
CHECKSUM
Checksum is the md5 hash of ( order_id + time_in_micro + api_secret ). Every time you receive an update, you should compute this checksum at your end
and match it with the one in the payload.
Note: The md5 hash ensures that you can verify the authenticity of the message. Only Upstox API can generate the correct checksum. We use MD5 to
compute the checksum. You can find MD5 implementations in your programming language of choice.
The JSON payload is posted as a raw HTTP POST body. You will have to read the raw body and then validate it.
Postback API works even when you are not logged in.
Websocket
The Upstox API websocket channel can be used to receive various types of push updates from the Upstox server. These updates include:
Order Update
Trade Update (Fill Report)
Quote Update
Connection
The connectSocket function in the libraries will create an always-on socket connection to the Upstox API servers. If you are using cURL, please be sure to
pass your your_api_key and your_access_token as parameters in the URL.
Socket-Params
curl \
-H 'authorization: Bearer {your_access_token}' \
-H 'x-api-key: {your_api_key}' \
-X GET 'https://api.upstox.com/live/socket-params/'
In our quest to make our socket connection more stable or rather avoiding disconnection, we tried altering some connection parameter at the client level from
the server so both ends of the network have good knowledge of each other. Developers using their own WebSocket library can use these parameters to make
their sockets less prone to disconnections, these are generic parameters which can be found on most of the WebSocket libraries.
parameter description
keepalive It forces the underlying TCP connection to remain alive even if there is no transfer of packets for a specific period of time.
keepaliveInterval the interval till which the underlying TCP connection will try to make the connection alive.
Nagle algorithm buffers the packet to a certain extent before sending them in a burst, it reduces the total number of packets by
disableNagleAlgorithm increasing their size but at times it makes the networks silent. This is not good for live feeds so we should disable it in libraries
that provide it
autoAcceptConnections This parameter is specific to the Web Socket library of NodeJS SDK
keepaliveGracePeriod This parameter is specific to the Web Socket library of NodeJS SDK
parameter description
Web sockets send ping pong messages back and forth between server and client to make the connection alive. This parameter
pythonPingInterval
tells the number of seconds after which the client should send ping message
pythonPingTimeout This time determines the amount of time the server should wait for the pong message before disconnecting the socket
For the techies who are using a different Web socket library to connect to us should use these parameters. You should fetch these values and use them in
your Web socket before connecting.
Note: The above change is applicable to third party libraries. Our Python and NodeJS SDK’s are built in with this update.
Disconnection
Upstox API libraries allow you to disconnect the socket with a method
Events
ORDER UPDATE
The order update event comes with the message value of order_update
The response for the order update event is the same as an individual response in the order history command
TRADE UPDATE
The trade update event comes with the message value of fill_report
The response for the trade update event is the same as an individual response in the trade history command
QUOTE UPDATE
Quote updates come as binary values. You need to first decode the binary into a string. Each quote update is separated by the semi-colon ; character. Within
each quote update, each field is separated by commas ,
ON DISCONNECT EVENT
ON ERROR EVENT
Errors
405 Method Not Allowed – You tried to access a resource with an invalid method
410 Gone – The resource requested has been removed from our servers
429 Too Many Requests – You’re requesting too many resources! Slow down!
500 Internal Server Error – We had a problem with our server. Try again later.
503 Service Unavailable – We’re temporarily offline for maintenance. Please try again later.
Release notes
Change Link
Added support to fetch details for individual order using order ID Read more