
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
smartapi-typescript
Advanced tools
A robust TypeScript client library for Angel One's SmartAPI trading platform. Perform stock market trading operations including authentication, order placement, portfolio management, and real-time market data access.
npm install smartapi-typescript
or with yarn:
yarn add smartapi-typescript
or with pnpm:
pnpm add smartapi-typescript
import { SmartAPI } from 'smartapi-typescript';
// Initialize SmartAPI client
const smartApi = new SmartAPI({
apiKey: 'YOUR_API_KEY',
clientId: 'YOUR_CLIENT_ID',
debug: true // Set to false in production
});
// Login with password (for accounts without 2FA)
async function login() {
try {
const loginResponse = await smartApi.login('YOUR_PASSWORD');
console.log('Login successful:', loginResponse.status);
// Get user profile
const profile = await smartApi.getProfile();
console.log('User profile:', profile);
} catch (error) {
console.error('Login failed:', error);
}
}
login();
If you have two-factor authentication enabled on your Angel One account:
import { SmartAPI } from 'smartapi-typescript';
const smartApi = new SmartAPI({
apiKey: 'YOUR_API_KEY',
clientId: 'YOUR_CLIENT_ID'
});
async function loginWithTotp() {
try {
// You'll need to generate this code from your authenticator app
const totp = '123456'; // Replace with your current TOTP code
const loginResponse = await smartApi.login('YOUR_PASSWORD', totp);
console.log('Login successful:', loginResponse.status);
} catch (error) {
console.error('Login failed:', error);
}
}
loginWithTotp();
For automated systems, you can set up automatic TOTP generation using your TOTP secret:
import { SmartAPI } from 'smartapi-typescript';
// Initialize with your TOTP secret
const smartApi = new SmartAPI({
apiKey: 'YOUR_API_KEY',
clientId: 'YOUR_CLIENT_ID',
totpSecret: 'YOUR_TOTP_SECRET' // The secret key used to generate TOTP codes
});
async function loginWithAutoTotp() {
try {
// No TOTP code needed - will be generated automatically
const loginResponse = await smartApi.login('YOUR_PASSWORD');
console.log('Login with auto TOTP successful:', loginResponse.status);
} catch (error) {
console.error('Login failed:', error);
}
}
loginWithAutoTotp();
Note: You receive the TOTP secret only once when setting up 2FA with Angel One. Make sure to store this securely. Generate totp secret by going https://smartapi.angelbroking.com/enable-totp
// Get LTP for a single instrument
const ltpData = await smartApi.getLTP({
exchange: 'NSE',
tradingSymbol: 'RELIANCE-EQ',
symbolToken: '2885'
});
// Get multiple LTP values
const multiLtpData = await smartApi.getMultiLTP([
{
exchange: 'NSE',
tradingSymbol: 'RELIANCE-EQ',
symbolToken: '2885'
},
{
exchange: 'NSE',
tradingSymbol: 'INFY-EQ',
symbolToken: '1594'
}
]);
const historicalData = await smartApi.getHistoricalData({
exchange: 'NSE',
symbolToken: '2885',
interval: 'ONE_DAY',
fromDate: '2023-04-01 09:15',
toDate: '2023-04-30 15:30'
});
// Place a regular order
const orderResponse = await smartApi.placeOrder({
variety: 'NORMAL',
tradingSymbol: 'RELIANCE-EQ',
symbolToken: '2885',
transactionType: 'BUY',
exchange: 'NSE',
orderType: 'LIMIT',
productType: 'INTRADAY',
duration: 'DAY',
price: '2100',
squareoff: '0',
stoploss: '0',
quantity: '1'
});
// Place a bracket order
const bracketOrderResponse = await smartApi.placeBracketOrder({
tradingSymbol: 'RELIANCE-EQ',
symbolToken: '2885',
transactionType: 'BUY',
exchange: 'NSE',
orderType: 'LIMIT',
productType: 'INTRADAY',
duration: 'DAY',
price: '2100',
squareoff: '10',
stoploss: '5',
quantity: '1'
});
// Place a cover order
const coverOrderResponse = await smartApi.placeCoverOrder({
tradingSymbol: 'RELIANCE-EQ',
symbolToken: '2885',
transactionType: 'BUY',
exchange: 'NSE',
orderType: 'LIMIT',
productType: 'INTRADAY',
duration: 'DAY',
price: '2100',
coverPrice: '2050',
quantity: '1'
});
// Modify an existing order
const modifyResponse = await smartApi.modifyOrder({
variety: 'NORMAL',
orderType: 'LIMIT',
orderID: '230428000000072',
price: '2110',
quantity: '1'
});
// Cancel an order
const cancelResponse = await smartApi.cancelOrder({
variety: 'NORMAL',
orderID: '230428000000072'
});
// Get order book
const orderBook = await smartApi.getOrderBook();
// Get trade book
const tradeBook = await smartApi.getTradeBook();
// Get current positions
const positions = await smartApi.getPositions();
// Get holdings
const holdings = await smartApi.getHoldings();
// Get all holdings including pledged holdings
const allHoldings = await smartApi.getAllHoldings();
// Get funds and margins
const funds = await smartApi.getFunds();
// Convert position (for example from INTRADAY to DELIVERY)
const convertResponse = await smartApi.convertPosition({
exchange: 'NSE',
tradingSymbol: 'RELIANCE-EQ',
symbolToken: '2885',
transactionType: 'BUY',
positionType: 'DAY',
quantityToConvert: '1',
fromProductType: 'INTRADAY',
toProductType: 'DELIVERY'
});
import { SmartAPI, SmartWebSocket } from 'smartapi-typescript';
// First authenticate with SmartAPI
const smartApi = new SmartAPI({
apiKey: 'YOUR_API_KEY',
clientId: 'YOUR_CLIENT_ID'
});
// Login
await smartApi.login('YOUR_PASSWORD', 'YOUR_TOTP');
// Initialize WebSocket
const ws = new SmartWebSocket({
feedToken: smartApi.auth.getFeedToken(),
clientId: 'YOUR_CLIENT_ID',
apiKey: 'YOUR_API_KEY',
debug: true
});
// Connect to WebSocket
ws.connect()
.then(() => {
// Subscribe to tokens
ws.subscribe([
{ action: 'SUBSCRIBE', feedType: 'LTP', scriptToken: '2885' },
{ action: 'SUBSCRIBE', feedType: 'DEPTH', scriptToken: '1594' }
]);
})
.catch(err => {
console.error('WebSocket connection error:', err);
});
// Handle messages
ws.on('message', message => {
console.log('WebSocket message:', message);
});
// Handle connection close
ws.on('close', (code, reason) => {
console.log('WebSocket closed:', code, reason);
});
// Handle errors
ws.on('error', error => {
console.error('WebSocket error:', error);
});
// Disconnect when done
// ws.disconnect();
// Create a GTT rule
const gttCreateResponse = await smartApi.createGTT({
tradingSymbol: 'RELIANCE-EQ',
symbolToken: '2885',
exchange: 'NSE',
productType: 'DELIVERY',
transactionType: 'BUY',
price: '2100',
quantity: '1',
triggerType: 'SINGLE',
triggerPrice: '2090',
limitPrice: '2100'
});
// Modify a GTT rule
const gttModifyResponse = await smartApi.modifyGTT({
id: '123456',
tradingSymbol: 'RELIANCE-EQ',
symbolToken: '2885',
exchange: 'NSE',
productType: 'DELIVERY',
transactionType: 'BUY',
price: '2110',
quantity: '1',
triggerType: 'SINGLE',
triggerPrice: '2100',
limitPrice: '2110'
});
// Cancel a GTT rule
const gttCancelResponse = await smartApi.cancelGTT({
id: '123456',
symbolToken: '2885',
exchange: 'NSE'
});
// Get GTT rule details
const gttDetails = await smartApi.getGTTDetails({ id: '123456' });
// Get list of GTT rules
const gttList = await smartApi.getGTTList();
The library uses a consistent error handling pattern:
try {
const response = await smartApi.placeOrder({
// Order parameters
});
if (response.status) {
console.log('Order placed successfully:', response.data);
} else {
console.log('Order placement failed:', response.message);
}
} catch (error) {
console.error('An error occurred:', error);
}
The library handles token refresh automatically. You can also manually manage tokens:
// Initialize with existing tokens
const smartApi = new SmartAPI({
apiKey: 'YOUR_API_KEY',
jwtToken: 'YOUR_JWT_TOKEN',
refreshToken: 'YOUR_REFRESH_TOKEN'
});
// Generate a new session with tokens
const sessionResponse = await smartApi.generateSession();
FAQs
TypeScript library for Angel One SmartAPI broker API
We found that smartapi-typescript demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.