A comprehensive package and API server for accessing equity/index details and historical data from the National Stock Exchange of India. This project provides both an NPM package for direct integration and a full-featured GraphQL/REST API server.
๐ Documentation | ๐ Examples
โ ๏ธ Prerequisites: Node.js 18+ required
npm install stock-nse-india
import { NseIndia } from "stock-nse-india";
const nseIndia = new NseIndia();
// Get all stock symbols
const symbols = await nseIndia.getAllStockSymbols();
console.log(symbols);
// Get equity details
const details = await nseIndia.getEquityDetails('IRCTC');
console.log(details);
// Get historical data
const range = {
start: new Date("2020-01-01"),
end: new Date("2023-12-31")
};
const historicalData = await nseIndia.getEquityHistoricalData('IRCTC', range);
console.log(historicalData);
# Clone and setup
git clone https://github.com/hi-imcodeman/stock-nse-india.git
cd stock-nse-india
npm install
# Start the server
npm start
๐ Server URLs:
npm install stock-nse-india
# or
yarn add stock-nse-india
npm install -g stock-nse-india
git clone https://github.com/hi-imcodeman/stock-nse-india.git
cd stock-nse-india
npm install
npm start
The project now includes a powerful GraphQL API for flexible data querying:
# Get equity information
query GetEquity {
equities(symbolFilter: { symbols: ["IRCTC", "RELIANCE"] }) {
symbol
details {
info {
companyName
industry
isFNOSec
}
metadata {
listingDate
status
}
}
}
}
# Get indices data
query GetIndices {
indices(filter: { filterBy: "NIFTY" }) {
key
index
last
variation
percentChange
}
}
The API includes schemas for:
The project includes a Model Context Protocol (MCP) server that allows AI assistants to access NSE India stock market data:
Model Context Protocol (MCP) is a standard for AI assistants to communicate with external data sources and tools. This MCP server exposes all NSE India functions as tools that AI models can use.
The MCP implementation is built with a modular architecture for maintainability and consistency:
src/mcp/mcp-tools.ts: Common tools configuration and handler functions shared across all implementationssrc/mcp/server/mcp-server.ts: Stdio-based MCP server for local AI assistant integrationsrc/mcp/client/mcp-client.ts: OpenAI Functions-based MCP client for natural language queriesAll components share the same tool definitions and business logic, ensuring consistency and making maintenance easier.
The MCP server provides 30 tools covering:
The project includes an advanced MCP client that uses OpenAI's native function calling feature for intelligent query processing:
processQuery(): Single-round query processing for straightforward requestsprocessQueryWithMultipleFunctions(): Multi-step query processing for complex analysisimport { mcpClient } from './mcp/client/mcp-client'
// Simple query
const response = await mcpClient.processQuery({
query: "What is the current price of TCS stock?",
model: "gpt-4o-mini"
})
// Complex multi-step query
const complexResponse = await mcpClient.processQueryWithMemory({
query: "Compare the performance of Reliance and TCS over the last month and analyze their trends"
})
# Start the stdio MCP server
npm run start:mcp
# Test the stdio MCP server
npm run test:mcp
Installation Steps:
Prerequisites: Ensure Node.js 18+ is installed on your system
node --version # Should be v18.0.0 or higher
Install the package (optional but recommended for faster startup):
npm install -g stock-nse-india
Note: If you don't install globally, npx will automatically download and cache the package on first use, which may take a few moments.
Configuration:
{
"mcpServers": {
"npx-stock-nse-india": {
"command": "npx",
"args": ["stock-nse-india", "mcp"],
"env": {
"NODE_ENV": "production"
}
}
}
}
{
"mcpServers": {
"nse-india-stdio": {
"command": "node",
"args": ["build/mcp/server/mcp-server-stdio.js"],
"env": {
"NODE_ENV": "production"
}
}
}
}
Cmd+, (Mac) or Ctrl+, (Windows/Linux) to open settingsAlternatively, you can directly edit the Cursor configuration file:
~/.cursor/mcp.json or in your workspace settings%APPDATA%\Cursor\mcp.jsonAfter configuration, the MCP server will be available in Cursor's AI assistant, allowing you to query NSE India stock market data directly from the chat interface.
For detailed MCP documentation, see MCP_README.md.
Comprehensive REST endpoints with automatic Swagger documentation:
GET / - Market statusGET /api/marketStatus - Market status informationGET /api/glossary - NSE glossaryGET /api/equity/:symbol - Equity detailsGET /api/equity/:symbol/historical - Historical dataGET /api/indices - Market indicesGET /api-docs - Interactive API documentationPOST /api/mcp/query - Natural language query using OpenAI FunctionsPOST /api/mcp/query-multiple - Multi-step natural language queriesVisit http://localhost:3000/api-docs for complete interactive API documentation powered by Swagger UI.
# Get help
nseindia --help
# Get market status
nseindia
# Get equity details
nseindia equity IRCTC
# Get historical data
nseindia historical IRCTC
# Get indices information
nseindia index
# Get specific index details
nseindia index "NIFTY AUTO"
# Pull and run from Docker Hub
docker run --rm -d -p 3001:3001 imcodeman/nseindia
# Or build locally
docker build -t nseindia . && docker run --rm -d -p 3001:3001 nseindia:latest
Image: imcodeman/nseindia
Registry: Docker Hub
# Server Configuration
PORT=3000
HOST_URL=http://localhost:3000
NODE_ENV=development
# CORS Configuration
CORS_ORIGINS=https://myapp.com,https://admin.myapp.com
CORS_METHODS=GET,POST,OPTIONS
CORS_HEADERS=Content-Type,Authorization,X-Requested-With
CORS_CREDENTIALS=true
getAllStockSymbols() - Get all NSE stock symbolsgetData() - Generic data retrievalgetDataByEndpoint() - Get data by specific NSE API endpointsgetEquityDetails(symbol) - Get equity informationgetEquityHistoricalData(symbol, range) - Historical price datagetEquityIntradayData(symbol) - Intraday trading datagetEquityOptionChain(symbol) - Options chain datagetEquityCorporateInfo(symbol) - Corporate informationgetEquityTradeInfo(symbol) - Trading statisticsgetEquityStockIndices() - Get all market indicesgetIndexIntradayData(index) - Index intraday datagetIndexOptionChain(index) - Index options datagetIndexOptionChainContractInfo(indexSymbol) - Get option chain contract information (expiry dates and strike prices)getCommodityOptionChain(symbol) - Commodity options datagetGainersAndLosersByIndex(index) - Top gainers and losersgetMostActiveEquities() - Most actively traded stocksโ ๏ธ Prerequisites: Node.js 18+ required
# Clone repository
git clone https://github.com/hi-imcodeman/stock-nse-india.git
cd stock-nse-india
# Install dependencies
npm install
# Development mode with auto-reload
npm run start:dev
# Build project
npm run build
# Run tests
npm test
# Generate documentation
npm run docs
npm start - Start production servernpm run start:dev - Development mode with auto-reloadnpm run build - Build TypeScript to JavaScriptnpm test - Run test suite with coveragenpm run docs - Generate TypeDoc documentationnpm run lint - Run ESLintnpm run start:mcp - Start stdio MCP servernpm run test:mcp - Test stdio MCP server# Run all tests
npm test
# Run tests with coverage
npm test -- --coverage
# Run specific test file
npm test -- utils.spec.ts
We welcome contributions! Please see our contributing guidelines and feel free to submit issues and pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.
โญ Star this repository if you find it helpful!
Generated using TypeDoc