SmartAPI JavaScript Client SDK

1.0.27 · active · verified Sun Apr 19

The `smartapi-javascript` package provides a client-side SDK for programmatic interaction with the SmartAPI platform, an initiative by Angel One (formerly Angel Broking) for the Indian stock market. It offers a comprehensive suite of functionalities, including user authentication and session management, fetching profile and real-time margin (RMS) details, executing various order types (place, modify, cancel, order book, trade book), managing portfolio holdings and positions, converting positions, and handling Good Till Triggered (GTT) orders. Additionally, it provides access to historical candle data and includes WebSocket functionality for real-time market updates. The current stable version is 1.0.27. As an official client library for a specific financial service, its release cadence typically aligns with updates to the SmartAPI platform itself. A key differentiator is its direct and tailored integration with Angel One's trading ecosystem, simplifying development for users targeting this platform. The library currently emphasizes CommonJS `require` syntax for module imports, as demonstrated in its official documentation.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the SmartAPI client, securely generate a user session with credentials (client code, password, TOTP), and then use the authenticated session to fetch the user's profile details. It also includes commented-out examples for various trading operations like placing orders and retrieving the order book, along with a custom session expiry hook to manage re-authentication.

const { SmartAPI } = require('smartapi-javascript');

// IMPORTANT: Replace with your actual credentials. For production, use environment variables.
const API_KEY = process.env.SMARTAPI_API_KEY || 'smartapi_key';
const CLIENT_CODE = process.env.SMARTAPI_CLIENT_CODE || 'YOUR_CLIENT_CODE';
const PASSWORD = process.env.SMARTAPI_PASSWORD || 'YOUR_PASSWORD';
const TOTP_SECRET = process.env.SMARTAPI_TOTP_SECRET || 'YOUR_TOTP_VALUE'; // If using TOTP

let smart_api = new SmartAPI({
	api_key: API_KEY
});

// Function to handle session expiry, can be used to re-authenticate or log out
function customSessionHook() {
	console.log('SmartAPI session expired. Please re-authenticate.');
	// Implement re-authentication logic here or notify the user.
}

smart_api.setSessionExpiryHook(customSessionHook);

smart_api
	.generateSession(CLIENT_CODE, PASSWORD, TOTP_SECRET)
	.then(async (data) => {
		console.log('Session generated successfully:', data);
		// Now you can make authenticated API calls
		const profile = await smart_api.getProfile();
		console.log('User Profile:', profile);

		// Example: Place a mock order (UNCOMMENT AND ADJUST FOR REAL TRADING)
		/*
		const placeOrderResponse = await smart_api.placeOrder({
		    "variety": "NORMAL",
		    "tradingsymbol": "SBIN-EQ",
		    "symboltoken": "3045",
		    "transactiontype": "BUY",
		    "exchange": "NSE",
		    "ordertype": "LIMIT",	
		    "producttype": "INTRADAY",
		    "duration": "DAY",
		    "price": "19500",
		    "squareoff": "0",
		    "stoploss": "0",	
		    "quantity": "1"
		});
		console.log('Order Placement Response:', placeOrderResponse);
		*/

		// Example: Get Order Book
		// const orderBook = await smart_api.getOrderBook();
		// console.log('Order Book:', orderBook);

	})
	.catch((ex) => {
		console.error('Error during SmartAPI session generation or API call:', ex);
		if (ex && ex.response && ex.response.data) {
			console.error('API Error Details:', ex.response.data);
		}
	});

view raw JSON →