I am facing a critical challenge in reliably acquiring this data. My attempts so far have been unsuccessful:
-
Public URL Fails: The public URL (https://assets.upstox.com/market-quote/instruments/...) is consistently returning a 403 Forbidden or Access Denied error, indicating that automated access to this resource is blocked.
-
API Endpoints in Sandbox: The documented API endpoints that I have tried, such as HistoryApi and MarketQuoteApi, appear to be unsupported in the Sandbox environment, returning 404 Not Found errors when trying to fetch the full instrument list.
-
Current Workaround (Not Viable for Live): My current solution involves manually downloading the daily F&O Bhavcopy from the NSE website. While this works for Sandbox testing, it is not a viable long-term solution for a live, autonomous bot because the NSE file lacks the unique Upstox instrument_key required to place orders via the API.
To build a robust and reliable application, I need a stable, official, and authenticated method to get this data.
My Specific Request:
Could you please provide guidance on the recommended, official API endpoint to download the complete instrument master list for a given segment (e.g., NSE_FO)? The key requirement is that the data returned must include the Upstox instrument_key for each contract.
For your reference, below is the core Python function from my script that handles the logic for data loading and option selection. This demonstrates how I intend to use the data.
codePython
def start_day_trade():
"""The main entry point function, with separate logic for LIVE and SANDBOX modes."""
global STRATEGY_ATTEMPTED_TODAY, STRATEGY_STATE
# ... (Time checks and holiday checks are performed here) ...
logging.info(f"--- Attempting to Start Trading Job for {stock_symbol} at {now} ---")
client = login_and_get_client()
if not client:
# ... (Handle login failure) ...
return
# ... (Wait until trade execution time) ...
# 1. THIS IS THE PROBLEMATIC STEP
# Currently relies on a manual NSE Bhavcopy file.
# The goal is to replace this with a direct API call.
fo_instruments_df = download_instrument_master_if_needed("NSE_FO")
if fo_instruments_df is None or fo_instruments_df.empty:
emergency_shutdown("Failed to load F&O instrument file. Aborting.")
return
# 2. DETERMINE THE STOCK'S LTP
# This logic uses the downloaded file to find a futures contract to estimate the LTP.
stock_ltp = None
try:
fo_instruments_df.columns = [col.strip().upper() for col in fo_instruments_df.columns]
stock_future = fo_instruments_df[(fo_instruments_df['SYMBOL'] == stock_symbol) & (fo_instruments_df['INSTRUMENT'] == 'FUTSTK')].iloc[0]
stock_ltp = stock_future['CLOSE']
except Exception as e:
emergency_shutdown(f"Could not find a futures contract for {stock_symbol} to determine LTP. Error: {e}")
return
# 3. FIND THE ATM OPTIONS
# This uses the LTP and the instrument list to find the correct options to trade.
call_instrument, put_instrument = get_itm_options(fo_instruments_df, stock_ltp, stock_symbol)
if not (call_instrument and put_instrument):
emergency_shutdown(f"Could not find ATM options for {stock_symbol} at LTP {stock_ltp}.")
return
# 4. PROCEED WITH TRADING
# The script then proceeds to place orders using the found instruments.
# ... (rest of the trading logic) ...
Could you please review this approach and let me know:
-
Is this the correct general logic for achieving my goal?
-
If so, which specific API endpoint and function from the Python SDK should I use to replace the manual download_instrument_master_if_needed step?
-
If this is not the correct approach, could you please provide a working code sample demonstrating the official method for fetching the daily F&O instrument list with their keys?