SmartAPI Javascript Client SDK
Installation
npm i smartapi-javascript
Getting started with API
let { SmartAPI, WebSocket,WebSocketV2 } = require('smartapi-javascript');
let smart_api = new SmartAPI({
api_key: 'smartapi_key',
});
smart_api
.generateSession('CLIENT_CODE', 'PASSWORD', 'TOTP')
.then((data) => {
return smart_api.getProfile();
})
.then((data) => {
})
.catch((ex) => {
});
smart_api.setSessionExpiryHook(customSessionHook);
function customSessionHook() {
console.log('User loggedout');
}
Getting started with SmartAPI Websocket's
########################### Socket Sample Code Starts Here ###########################
let web_socket = new WebSocket({
client_code: "CLIENT_CODE",
feed_token: "FEED_TOKEN"
});
web_socket.connect()
.then(() => {
web_socket.runScript("SCRIPT", "TASK")
setTimeout(function () {
web_socket.close()
}, 3000)
})
web_socket.on('tick', receiveTick)
function receiveTick(data) {
console.log("receiveTick:::::", data)
}
########################### Socket Sample Code Ends Here ###########################
########################### Socket Sample Code Starts Here ###########################
let web_socket = new WebSocketClient({
clientcode: "CLIENT_CODE",
jwttoken: "jwt_token",
apikey: "smartapi_key",
feedtype: "order_feed",
});
web_socket.connect()
.then(() => {
web_socket.fetchData("ACTION_TYPE", "FEED_TYPE");
setTimeout(function () {
web_socket.close()
}, 60000)
});
web_socket.on('tick', receiveTick);
function receiveTick(data) {
console.log("receiveTick:::::", data);
}
########################### Socket Sample Code Ends Here ###########################
let web_socket = new WebSocketV2({
jwttoken: 'JWT_TOKEN',
apikey: 'API_KEY',
clientcode: 'Client_code',
feedtype: 'FEED_TYPE',
});
web_socket.connect().then((res) => {
let json_req = {
correlationID: 'abcde12345',
action: 1,
params: {
mode: 4,
tokenList: [
{
exchangeType: 1,
tokens: ['1232'],
},
],
},
};
web_socket.fetchData(json_req);
web_socket.on('tick', receiveTick);
function receiveTick(data) {
console.log('receiveTick:::::', data);
}
});