Skip to main content

💰 NseIndiaApi v2.0 - Enhanced Edition

An unofficial Python API for the NSE India stock exchange with modern features.

Python version: >= 3.8

⭐ NEW in v2.0: Async support, caching, CLI tool, better error handling, data export, and more!

🎯 Key Features

  • ✅ Synchronous and Asynchronous API
  • ✅ Built-in caching with TTL
  • ✅ CLI tool for quick queries
  • ✅ Export to CSV, JSON, Parquet
  • ✅ Retry logic with exponential backoff
  • ✅ Custom exceptions for better error handling
  • ✅ Type hints and data models
  • ✅ Comprehensive logging
  • ✅ CI/CD pipeline
  • ✅ 100% backward compatible with v1.x

📚 Documentation

https://bennythadikaran.github.io/NseIndiaApi

🚀 Quick Start

Installation

# Basic installation (local use)
pip install nse[local]

# Server environment (AWS, Azure, etc.)
pip install nse[server]

# All features (recommended)
pip install nse[all]

# Development
pip install nse[dev]

Basic Usage

from nse import NSE

with NSE(download_folder="./data") as nse:
    # Get market status
    status = nse.status()
    
    # Get stock quote
    quote = nse.equityQuote("HDFCBANK")
    print(f"Price: {quote['close']}")
    
    # Get top gainers
    data = nse.listEquityStocksByIndex("NIFTY 50")
    gainers = nse.gainers(data, count=5)

Async Usage (NEW!)

from nse.async_nse import AsyncNSE
import asyncio

async def main():
    async with AsyncNSE("./data") as nse:
        # Fetch multiple quotes concurrently
        symbols = ["HDFCBANK", "INFY", "TCS", "RELIANCE"]
        quotes = await nse.fetch_multiple_quotes(symbols)
        
        for symbol, quote in quotes.items():
            print(f"{symbol}: {quote['priceInfo']['lastPrice']}")

asyncio.run(main())

CLI Usage (NEW!)

# Get stock quote
nse quote HDFCBANK

# Market status
nse status

# Top gainers
nse gainers --index "NIFTY 50" --count 10

# Top losers
nse losers --index "NIFTY 50" --count 10

# Download bhavcopy
nse bhavcopy --date 2024-01-15 --type equity

Caching (NEW!)

from nse import NSE
from nse.cache import SimpleCache
from pathlib import Path

cache = SimpleCache(Path("./data"), default_ttl=300)

with NSE("./data") as nse:
    symbol = "HDFCBANK"
    
    # Try cache first
    quote = cache.get(f"quote_{symbol}")
    
    if not quote:
        quote = nse.equityQuote(symbol)
        cache.set(f"quote_{symbol}", quote, ttl=60)
    
    print(quote)

Data Export (NEW!)

from nse import NSE
from nse.export import DataExporter
from pathlib import Path

with NSE("./data") as nse:
    data = nse.listEquityStocksByIndex("NIFTY 50")
    
    # Export to CSV
    DataExporter.to_csv(data["data"], Path("nifty50.csv"))
    
    # Export to JSON
    DataExporter.to_json(data["data"], Path("nifty50.json"))
    
    # Export to Parquet (requires pandas)
    DataExporter.to_parquet(data["data"], Path("nifty50.parquet"))

Error Handling (NEW!)

from nse import NSE
from nse.exceptions import NSETimeoutError, NSEInvalidSymbol

with NSE("./data") as nse:
    try:
        quote = nse.quote("INVALID_SYMBOL")
    except NSEInvalidSymbol as e:
        print(f"Invalid symbol: {e}")
    except NSETimeoutError as e:
        print(f"Request timeout: {e}")

🆕 What's New in v2.0

1. Async Support

Fetch multiple quotes concurrently for better performance.

2. Caching Layer

Reduce API calls with built-in file-based cache with TTL.

3. CLI Tool

No coding required for basic operations.

4. Data Export

Export to CSV, JSON, and Parquet formats.

5. Better Error Handling

Custom exception classes for different error types.

6. Retry Logic

Automatic retry with exponential backoff.

7. Logging

Structured logging with file and console output.

8. Type Safety

Data models using dataclasses for better IDE support.

9. CI/CD Pipeline

Automated testing and code quality checks.

10. Enhanced Testing

Comprehensive test suite with async tests.

📊 Performance

  • Async operations: 3-5x faster for bulk requests
  • Caching: Reduces API calls by 60-80%
  • Better connection pooling: Improved reliability

🔧 Development

# Clone repository
git clone https://github.com/BennyThadikaran/NseIndiaApi.git
cd NseIndiaApi

# Install in development mode
pip install -e .[dev]

# Run tests
pytest tests/ -v

# Code formatting
black src/

# Linting
ruff check src/

# Type checking
mypy src/

📝 API Limits

All requests through NSE are rate limited to 3 requests per second.

Best Practices:

  • Download large reports after market hours
  • Add 0.5-1 sec sleep between requests
  • Save and reuse files instead of re-downloading
  • Use caching for frequently accessed data

🔄 Migration from v1.x

Version 2.0 is 100% backward compatible. All v1.x code works without changes.

New features are additive and optional. See UPGRADE_NOTES.md for details.

📖 Examples

Check the examples/ directory for:

  • cache_example.py - Using cache to reduce API calls
  • async_example.py - Concurrent quote fetching
  • export_example.py - Exporting data to various formats

🤝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new features
  4. Ensure all tests pass
  5. Submit a pull request

📄 License

GNU General Public License v3 (GPLv3)

🙏 Credits

Original Author & Creator: Benny Thadikaran

This project was originally created and maintained by Benny Thadikaran. All core functionality, API design, and original implementation are his work.

v2.0 Enhancements: Community contributions building upon Benny's excellent foundation

📞 Support


If you ❤️ this project, please ⭐ the repo!

Metadata

Release files for nseindia-data 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nseindia-data 2.0.1
File Size Uploaded
nseindia_data-2.0.1.tar.gz 32.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nseindia-data 2.0.1
File Interpreter ABI Platform
nseindia_data-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 67.3 kB

Release files / nseindia_data-2.0.1.tar.gz

Download URL nseindia_data-2.0.1.tar.gz
Size 32.9 kB
Tags Source
SHA-256 checksum
How to use checksums
77f17dcecd903efb4d879074b526841a95c9d1041360d51c8bd8b1629816f237
BLAKE2b-256 checksum
How to use checksums
5e5680a1e49ebc05a7c641ed841e6479dd152443226fd510e81fa764fd478ed7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Release files / nseindia_data-2.0.1-py3-none-any.whl

Download URL nseindia_data-2.0.1-py3-none-any.whl
Size 34.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6071dbd9b6d704d70d237493120587073677d54eaaafbd0f2add2fe166ee2f4e
BLAKE2b-256 checksum
How to use checksums
c2bd69325faac0f4947afa626e5daac22797fc2d33537a86c06a4f96b4fbc5d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page