The official NodeJS client for communicating with PaytmMoney Equity API.
PMClient is a set of REST-like APIs that expose many capabilities required to build a complete investment and trading platform. Execute orders in real time, manage user portfolio, stream live market data (WebSockets), and more, with the simple HTTP API collection.
PaytmMoney Technology Pvt Ltd (c) 2022. Licensed under the MIT License.
var PMClient = require('pmClient').PMClient;User needs to create an object of sdk and pass apiKey & apiSecretKey
// Initialize PMClient using apiKey, apiSecret.
pm = PMClient(api_key="your_api_key", api_secret="your_api_secret");
// Initialize PMClient using apiKey, apiSecret & jwt tokens if user has already generated.
pm = PMClient(api_key="your_api_key", api_secret="your_api_secret", access_token="access_token",public_access_token="public_access_token", read_access_token="read_access_token");User needs to call the login method and get the login URL.
// Variable key which merchant/fintech company expects Paytm Money to return with Request Token. This can be string.
pm.get_login_URL(state_key);- User manually executes a login url in the browser and fetches requestToken after validating username, password, OTP and passcode.
- After a successful login user will be provided the request_token in the URL
- Once the request_token is obtained you can generate access_token by calling generate_session
pm.generate_session(request_token="your_request_token")
.then(function(response){
console.log(response)
})
.catch(function(err){
console.log(err);
});- Every Api returns a promise. Hence, then and catch must be used, as shown in generate_session api
.then(function(response){
console.log(response)
})
.catch(function(err){
console.log(err);
});pm.set_access_token("your_access_token")
pm.set_public_access_token("your_public_access_token")
pm.set_read_access_token("your_read_access_token")- Here you can place regular, cover and bracket order.
- For cover order in argument user has to add trigger_price.
- For bracket order in argument user has to add stoploss_value & profit_value.
- If order_type is StopLossMarket(SLM) or StopLoss(SL) trigger price should not be null.
// Regular Order
order = pm.place_order(txn_type, source, exchange, segment, product, security_id, quantity, validity, order_type, price,
off_mkt_flag);// Cover Order
order = pm.place_order(txn_type, source, exchange, segment, product, security_id, quantity, validity, order_type, price,
off_mkt_flag, null, null, trigger_price);// Bracket Order
order = pm.place_order(txn_type, source, exchange, segment, product, security_id, quantity, validity, order_type, price,
off_mkt_flag, profit_value, stoploss_value);// Sell CNC Order
order = pm.place_order(txn_type, source, exchange, segment, product, security_id, quantity, validity, order_type, price,
off_mkt_flag, null, null, null);- Here you can modify orders.
- For cover order in argument user has to add leg_no.
- For bracket order in argument user has to add leg_no & algo_order_no.
- If order_type is StopLossMarket(SLM) or StopLoss(SL) trigger price should not be null.
// Regular Order
order = pm.modify_order(txn_type, source, exchange, segment, product, security_id, quantity, validity, order_type,
price, off_mkt_flag, mkt_type, order_no, serial_no, group_id);// Cover Order
order = pm.modify_order(txn_type, source, exchange, segment, product, security_id, quantity, validity, order_type,
price, off_mkt_flag, mkt_type, order_no, serial_no, group_id, null, leg_no);// Bracket Order
order = pm.modify_order(txn_type, source, exchange, segment, product, security_id, quantity, validity, order_type,
price, off_mkt_flag, mkt_type, order_no, serial_no, group_id, null, leg_no, algo_order_no);- Here you can Cancel Orders
- For cover order in argument user has to add leg_no
- For bracket order in argument user has to add leg_no & algo_order_no
- If order_type is StopLossMarket(SLM) or StopLoss(SL) trigger price should not be null.
// Regular Order
order = pm.cancel_order(txn_type, source, exchange, segment, product, security_id, quantity, validity, order_type,
price, off_mkt_flag, mkt_type, order_no, serial_no, group_id);// Cover Order
order = pm.cancel_order(txn_type, source, exchange, segment, product, security_id, quantity, validity, order_type,
price, off_mkt_flag, mkt_type, order_no, serial_no, group_id, null, leg_no);// Bracket Order
order = pm.cancel_order(txn_type, source, exchange, segment, product, security_id, quantity, validity, order_type,
price, off_mkt_flag, mkt_type, order_no, serial_no, group_id, null, leg_no, algo_order_no);- For converting orders.
- The above details can be generated by TPIN APIs.
// Regular Order
order = pm.convert_order(source, txn_type, exchange, segment, mkt_type, product_from, product_to, quantity,
security_id);- Fetch details of all the order
pm.order_book();- Fetch Trade Details
pm.trade_details(order_no, leg_no, segment);- Get all the positions
pm.position();- Get position detail of specific stock
pm.position_details(security_id, product, exchange);- Get the funds history
pm.funds_summary(config);- Calculate Scrip Margin
pm.scrips_margin(source,margin_list=[]);- Calculate Order Margin
pm.order_margin(source, exchange, segment, security_id, txn_type, quantity, price, product, trigger_price);- Get value of the holdings
pm.holdings_value();- Get holdings data of User
pm.user_holdings_data();- User can filter by file_name
- To get the supported fileName API Doc for fileNames
pm.security_master("file_name");- Fetch user details
pm.get_user_details();pm.generate_tpin();pm.validate_tpin(trade_type, isin_list=[]);- User can get the edis_request_id from the response of validate TPIN API.
pm.status(edis_request_id);- To end session.
pm.logout();- To create a GTT order.
pm.create_gtt(segment, exchange, pml_id, security_id, product_type, set_price, transaction_type, order_type, trigger_type, quantity, trigger_price, limit_price);- To get all GTT or get by pml_id or status.
pm.get_gtt_by_status_or_pml_id(status, pml_id);- To get GTT by Id.
pm.get_gtt(id);- To update GTT by Id.
pm.update_gtt(id, set_price, transaction_type, order_type, trigger_type, quantity, trigger_price, limit_price);- To Delete GTT by Id.
pm.delete_gtt(id);- To get expiry of the GTT.
pm.get_gtt_expiry(pml_id);- To get the aggregate of the GTTs.
pm.get_gtt_aggregate();- To GTT by InstructionId.
pm.get_gtt_by_instruction_id(id);- To create a GTT order.
- Note : transaction_details is a list of objects(key-value pair).
- Refer below sample requestBody
pm.create_gtt_v2(segment, exchange, security_id, product_type, set_price, transaction_type, trigger_type, transaction_details);// Sample requestBody for SINGLE trigger_type
pm.create_gtt_v2(
segment = "E",
exchange = "BSE",
security_id = 500570,
product_type = "C",
set_price = "709.35",
transaction_type = "B",
trigger_type = "SINGLE",
transaction_details = [
{
"trigger_price": "702.25",
"order_type": "MKT",
"limit_price": 0,
"quantity": 1
}
]
)// Sample requestBody for OCO trigger_type
pm.create_gtt_v2(
segment = "E",
exchange = "BSE",
security_id = 500570,
product_type = "C",
set_price = "702.65",
transaction_type = "S",
trigger_type = "OCO",
transaction_details = [
{
"sub_type": "STOPLOSS",
"trigger_price": "695.60",
"order_type": "MKT",
"limit_price": 0,
"quantity": 1
},
{
"sub_type": "TARGET",
"trigger_price": "709.70",
"order_type": "MKT",
"limit_price": 0,
"quantity": 1
}
]
)- To get all GTT or get by pml_id or status.
pm.get_gtt_by_status_or_pml_id_v2(status, pml_id);- To get GTT by Id.
pm.get_gtt_v2(id);- To update GTT by Id.
- Note : transaction_details is a list of objects(key-value pair).
- Refer below sample requestBody
pm.update_gtt_v2(id, set_price, transaction_type, trigger_type, transaction_details);pm.update_gtt_v2(
id=217,
set_price = "8.40",
transaction_type = "S",
trigger_type = "OCO",
transaction_details = [
{
"id": 218, //For OCO only
"sub_type": "STOPLOSS", //For OCO only
"quantity": "2",
"trigger_price": "9.0",
"limit_price": "15.0",
"order_type": "LMT"
},
{
"id": 219, //For OCO only
"sub_type": "TARGET", //For OCO only
"quantity": "2",
"trigger_price": "15.0",
"limit_price": "20",
"order_type": "LMT"
}
]
)- To GTT by InstructionId.
pm.get_gtt_by_instruction_id_v2(id);- To Get Live Price Data via API
pmClient.get_live_market_data("mode", preferences)- To Get Option Chain using type, symbol and expiry (in DD-MM-YYYY format)
pmClient.get_option_chain("type", "symbol", "expiry")- To Get Option Chain Config using symbol
pmClient.get_option_chain_config("symbol")- Get all orders without apiKey filter
pmClient.orders()- Get Charges Info
pmClient.charges_info("brokerage_profile_code", "transaction_type", "product_type", "instrument_type", "exchange", qty, price)- To use websocket client in your project, add below code in a js file -
const LivePriceWebSocket = require("../LivePriceWebSocket.js");
let livePriceWebSocket = new LivePriceWebSocket();
jwt = "your_public_access_token" // enter your public access token here
customerPreferences = []
preference = {
"actionType" : 'ADD', // 'ADD', 'REMOVE'
"modeType" : 'LTP', // 'LTP', 'FULL', 'QUOTE'
"scripType" : 'EQUITY', // 'ETF', 'FUTURE', 'INDEX', 'OPTION', 'EQUITY'
"exchangeType" : 'NSE', // 'BSE', 'NSE'
"scripId" : '3456'
}
// create as many preferences as you like as shown above and append them in customerPreferences array
customerPreferences.push(preference)
// send preferences via websocket once connection is open
livePriceWebSocket.setOnOpenListener(() => {
livePriceWebSocket.subscribe(customerPreferences)
})
// this event gets triggered when connection is closed
livePriceWebSocket.setOnCloseListener((code, reason) => {
console.log(" disconnected Code: " + code + " Reason: " + reason);
})
// this event gets triggered when response is received
livePriceWebSocket.setOnMessageListener((arr) => {
printArray(arr)
})
// this event gets triggered when error occurs
livePriceWebSocket.setOnErrorListener((err) => {
console.log(err)
})
/**
* set this config if reconnect feature is desired
* Set first param as true and second param, the no. of times retry to connect to server shall be made
*/
livePriceWebSocket.setReconnectConfig(true, 5);
// this method is called to create a websocket connection with broadcast server
livePriceWebSocket.connect(jwt) //pass public_access_token
// To explicitly close websocket connection with server, call this method
livePriceWebSocket.disconnect()
// this method prints the response array received
function printArray(arr) {
console.log("data received from server: ");
arr.forEach((obj) => {
let tick = Object.keys(obj)
tick.forEach((key) => {
console.log(key + ":", obj[key])
})
console.log("\n")
})
}