I am sharing my code and Output, Looking for support
import os
import ssl
import json
import asyncio
import requests
import time
from dotenv import load_dotenv
import websockets
from upstox_client.feeder.proto import MarketDataFeedV3_pb2 as pb
from google.protobuf.json_format import MessageToDict
# === Load API token ===
load_dotenv()
ACCESS_TOKEN = os.getenv(“UPSTOX_ACCESS_TOKEN”) or open(“token.txt”).read().strip()
# === Update with live contracts for real tick data! ===
INSTRUMENT_KEYS = [
"NSE_FO|132447", # Example: Reliance PE (update for live test)
"NSE_FO|132446", # Example: Reliance CE (update for live test)
]
MODE = “full”
def get_ws_url():
url = "https://api.upstox.com/v3/feed/market-data-feed/authorize"
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {ACCESS_TOKEN}"
}
for i in range(5):
try:
resp = requests.get(url, headers=headers, timeout=10)
data = resp.json()
ws_url = data.get("data", {}).get("authorized_redirect_uri")
if ws_url:
print(f"\[INFO\] Got fresh WebSocket URL.")
return ws_url
else:
print(f"\[WARN\] /authorize response missing WS URL: {data}")
except Exception as e:
print(f"\[ERR\] Error fetching WS URL: {e}")
print(f"Retrying ({i+1}/5)...")
time.sleep(1)
raise Exception("Could not get valid WS URL after retries.")
async def run_ws():
ws_url = get_ws_url()
print(f"\\nConnecting to: {ws_url}")
print(f"Subscribing to: {INSTRUMENT_KEYS}")
print(f"Subscription mode: {MODE}\\n")
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
start = time.time()
duration = 10 \* 60 # 10 minutes
async with websockets.connect(ws_url, ssl=ssl_context) as ws:
req = {
"guid": "osa-greeks-loop",
"method": "sub",
"data": {
"mode": MODE,
"instrumentKeys": INSTRUMENT_KEYS
}
}
await ws.send(json.dumps(req))
print(f"\[WS\] Subscribed, waiting for messages... (10 min loop)\\n")
while True:
try:
\# Timeout every 3 seconds so you can see progress even if no message
msg = await asyncio.wait_for(ws.recv(), timeout=3.0)
now = time.strftime("%H:%M:%S")
if isinstance(msg, bytes):
feed = pb.FeedResponse()
feed.ParseFromString(msg)
data = MessageToDict(feed)
if "feeds" in data:
for key, val in data\["feeds"\].items():
print(f"\\n\[{now}\] === Tick for {key} ===")
print(json.dumps(val, indent=2))
else:
print(json.dumps(data, indent=2))
else:
print(f"\[{now}\] \[WS\] Non-binary message: {msg}")
except asyncio.TimeoutError:
print(f"\[{time.strftime('%H:%M:%S')}\] \[WS\] No new tick in last 3 sec (heartbeat).")
\# Exit after 10 min
if time.time() - start > duration:
print("10 minutes elapsed. Exiting...")
break
if _name_ == “_main_”:
asyncio.run(run_ws())
Output Connecting to: wss://wsfeeder-api.upstox.com/market-data-feeder/v3/upstox-developer-api/feeds?re
questId=d60df696-02c9-4ef3-b7cf-5a790b876f61&code=wpq6W-3c6cab18-92f2-4722-80f1-ef3dbc1be24a
Subscribing to: [‘NSE_FO|132447’, ‘NSE_FO|132446’]
Subscription mode: full
[WS] Subscribed, waiting for messages… (10 min loop)
{
“type”: “market_info”,
“currentTs”: “1754383711403”,
“marketInfo”: {
“segmentStatus”: {
“NSE_EQ”: “NORMAL_OPEN”,
“NSE_COM”: “NORMAL_OPEN”,
“BSE_FO”: “NORMAL_OPEN”,
“NSE_FO”: “NORMAL_OPEN”,
“BSE_EQ”: “NORMAL_OPEN”,
“MCX_INDEX”: “NORMAL_OPEN”,
“MCX_FO”: “NORMAL_OPEN”,
“BSE_INDEX”: “NORMAL_OPEN”,
“NCD_FO”: “NORMAL_OPEN”,
“NSE_INDEX”: “NORMAL_OPEN”,
“BCD_FO”: “NORMAL_OPEN”
}
}
}
[11:48:33] [WS] No new tick in last 3 sec (heartbeat).
[11:48:36] [WS] No new tick in last 3 sec (heartbeat).
[11:48:39] [WS] No new tick in last 3 sec (heartbeat).
[11:48:42] [WS] No new tick in last 3 sec (heartbeat).
[11:48:45] [WS] No new tick in last 3 sec (heartbeat).
[11:48:48] [WS] No new tick in last 3 sec (heartbeat).
[11:48:51] [WS] No new tick in last 3 sec (heartbeat).
[11:48:54] [WS] No new tick in last 3 sec (heartbeat).
[11:48:57] [WS] No new tick in last 3 sec (heartbeat).
[11:49:00] [WS] No new tick in last 3 sec (heartbeat).
[11:49:03] [WS] No new tick in last 3 sec (heartbeat).
[11:49:06] [WS] No new tick in last 3 sec (heartbeat).
[11:49:09] [WS] No new tick in last 3 sec (heartbeat).
[11:49:12] [WS] No new tick in last 3 sec (heartbeat).
[11:49:15] [WS] No new tick in last 3 sec (heartbeat).
[11:49:18] [WS] No new tick in last 3 sec (heartbeat).
[11:49:21] [WS] No new tick in last 3 sec (heartbeat).
[11:49:24] [WS] No new tick in last 3 sec (heartbeat).