Skip to main content
Glama
ssonthal

Zerodha Trading Bot - MCP Server

by ssonthal
basic-usage.js3.3 kB
import { ZerodhaClient } from '../src/zerodha-client.js'; import dotenv from 'dotenv'; dotenv.config(); async function basicUsage() { console.log('🚀 Zerodha Trading Bot - Basic Usage Example\n'); // Initialize the client const client = new ZerodhaClient( process.env.ZERODHA_API_KEY, process.env.ZERODHA_API_SECRET, process.env.ZERODHA_REDIRECT_URI ); try { // Step 1: Get login URL console.log('1. Getting login URL...'); const loginURL = client.getLoginURL(); console.log(' Login URL:', loginURL); console.log(' Visit this URL to authenticate with Zerodha\n'); // Step 2: If access token is available, demonstrate API calls if (process.env.ZERODHA_ACCESS_TOKEN) { console.log('2. Testing API calls with access token...\n'); client.setAccessToken(process.env.ZERODHA_ACCESS_TOKEN); // Get user profile console.log(' 📊 Getting user profile...'); const profile = await client.getUserProfile(); console.log(` User: ${profile.name} (${profile.user_id})`); console.log(` Email: ${profile.email}`); console.log(` Broker: ${profile.broker}\n`); // Get account balance console.log(' 💰 Getting margins...'); const margins = await client.getMargins(); console.log(` Equity: ₹${margins.equity}`); console.log(` Available Balance: ₹${margins.available.balance}\n`); // Get holdings console.log(' 📈 Getting holdings...'); const holdings = await client.getHoldings(); if (holdings.length > 0) { console.log(` Found ${holdings.length} holdings:`); holdings.forEach(holding => { console.log(` ${holding.tradingsymbol}: ${holding.quantity} shares`); }); } else { console.log(' No holdings found'); } console.log(''); // Get positions console.log(' 📊 Getting positions...'); const positions = await client.getPositions(); if (positions.day.length > 0) { console.log(` Found ${positions.day.length} day positions:`); positions.day.forEach(position => { const pnl = position.pnl > 0 ? `+${position.pnl}` : position.pnl; console.log(` ${position.tradingsymbol}: ${position.quantity} shares (PnL: ₹${pnl})`); }); } else { console.log(' No day positions found'); } console.log(''); // Get live quotes console.log(' 📊 Getting live quotes...'); const symbols = 'NSE:RELIANCE,NSE:TCS,NSE:INFY'; const quotes = await client.getQuote(symbols); console.log(' Live Quotes:'); Object.keys(quotes).forEach(symbol => { const quote = quotes[symbol]; console.log(` ${symbol}: ₹${quote.last_price} (${quote.change > 0 ? '+' : ''}${quote.change}%)`); }); } else { console.log('2. No access token found in .env file'); console.log(' To test API calls:'); console.log(' 1. Set ZERODHA_ACCESS_TOKEN in your .env file'); console.log(' 2. Or run the auth server: node src/auth-server.js'); } console.log('\n✅ Basic usage example completed!'); } catch (error) { console.error('❌ Error:', error.message); } } // Run the example basicUsage();

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ssonthal/ZerodhaTradingBot-MCPServer'

If you have feedback or need assistance with the MCP directory API, please join our Discord server