Hi @Krishnakis_47134930,
Great observation that Sensex works but Nifty 50 doesn’t. The issue is indeed the instrument key string format, but there’s a critical detail RAJA_NAGA mentioned that needs clarification for your specific case.
The Root Issue:
Your API calls show:
- ✓ WORKING:
instrument=BSE_INDEX|SENSEX
- ✗ FAILING:
instrument=NSE_INDEX|Nifty 50
The problem is NOT a recent API change - rather, it’s about exact character matching. Upstox performs NO fuzzy matching on instrument keys. Every space, case, and character must be exact.
Fact: The code worked 2 weeks ago with the same key
This tells us one of these scenarios happened:
Scenario 1: Hardcoded Key Mismatch (MOST LIKELY)
You’re using a hardcoded string that might have invisible characters or case sensitivity issues:
"NSE_INDEX|Nifty 50" ← Looks right, but check:
- Are there trailing spaces?
- Is it actually two spaces between words?
- Is the case exactly "Nifty" (capital N, lowercase ifty)?
Scenario 2: Instrument Master Changed (LESS LIKELY)
Upstox may have updated Nifty 50’s symbol in their master file during a market session (rare, but possible).
DEFINITIVE SOLUTION:
Step 1: Get the Current, Correct Instrument Key
DON’T rely on hardcoding. Use the official Upstox Instrument Master:
import requests
import pandas as pd
# Download Upstox instrument master
url = "https://assets.upstox.com/market-data-feed/instruments.json.gz"
response = requests.get(url)
with open('instruments.json.gz', 'wb') as f:
f.write(response.content)
# Load and find Nifty 50
import gzip
import json
with gzip.open('instruments.json.gz', 'rt') as f:
data = json.load(f)
# Filter for Nifty 50
nifty_50 = [item for item in data if 'Nifty 50' in item.get('name', '')]
for item in nifty_50:
print(f"Key: {item['instrumentKey']}")
print(f"Name: {item['name']}")
print(f"Type: {item['instrumentType']}")
This will return the EXACT instrument key used by Upstox today.
Step 2: Store in Database (NOT Hardcoded)
Once you get the correct key:
# Store in your database
instrument_key = "NSE_INDEX|Nifty 50" # As returned from master file
# Make API call
historical_data_response = upstox_client.market_data.historical_candles(
instrument_key=instrument_key,
interval="day",
data=["2026-01-06", "2026-01-06"]
)
Step 3: Verify the Response
if not historical_data_response.candles:
# Debug: Check what instrument key is actually being used
print(f"Used key: {instrument_key}")
print(f"Key length: {len(instrument_key)}")
print(f"Key bytes: {repr(instrument_key)}")
# This will show hidden characters, extra spaces, etc.
else:
print(f"✓ Got {len(historical_data_response.candles)} candles")
Why It “Was Working” Before:
Two possibilities:
- Your hardcoded key was accidentally correct before
- You had the correct key from the master file at that time
- API-side change (very unlikely for such a core endpoint)
Why Sensex Works But Nifty Doesn’t:
Simple answer: BSE_INDEX|SENSEX is easier to get right (no spaces, obvious name). The Nifty key with space is more prone to typos like:
- Extra space:
NSE_INDEX|Nifty 50 (two spaces)
- Different spacing:
NSE_INDEX|Nifty50 (no space)
- Case mismatch:
NSE_INDEX|NIFTY 50 or NSE_INDEX|nifty 50
PRODUCTION-READY CODE:
import hashlib
# Download instrument master ONCE at startup or on schedule
def sync_instruments():
response = requests.get("https://assets.upstox.com/market-data-feed/instruments.json.gz")
with gzip.open(io.BytesIO(response.content), 'rt') as f:
instruments = json.load(f)
# Store in DB with hash for quick lookup
instrument_map = {}
for item in instruments:
key = item['instrumentKey']
name = item['name']
instrument_map[name] = key
# Save to database
save_to_database('instruments', instrument_map)
return instrument_map
# Use it
instrument_map = load_from_database('instruments')
nifty_key = instrument_map.get('Nifty 50')
if not nifty_key:
# If not found, sync fresh master
instrument_map = sync_instruments()
nifty_key = instrument_map['Nifty 50']
# Now API call will definitely work
response = upstox_client.market_data.historical_candles(
instrument_key=nifty_key,
...
)
Action Items:
- Download the instrument master file using the script above
- Find the exact Nifty 50 key from the master
- Replace your hardcoded value
- Test with fresh call
- If still 0 candles, share the exact instrument key you’re using
This will definitively solve it. The 0 candles response is Upstox’s way of saying “I don’t recognize this instrument key.”
-VENKATA