Websocket connection error

sachinzerodha
sachinzerodha edited September 3 in Python client
# Step 1: Login and get access token using KiteConnect REST API (manual step for first time)
kite = KiteConnect(api_key=api_key)

print("Go to this URL and login: ", kite.login_url())

# After login, you receive request_token via redirect URL callback
# Assume you obtained the request_token manually or from your web app

request_token = input("Enter the request token obtained after login: ").strip()

# Generate session to get access_token and refresh_token
data = kite.generate_session(request_token, api_secret=api_secret)
access_token = data["access_token"]
refresh_token = data.get("refresh_token")

print("Access token:", access_token)
print("Refresh token:", refresh_token)

# Now use access token to create KiteTicker websocket connection
print("Starting KiteTicker...")
SPOT_INSTRUMENT_TOKENS = [738561] # Example: RELIANCE spot
FUTURES_INSTRUMENT_TOKENS = [5633] # Example: ACC futures

# Initialize ticker
print(api_key,access_token,"cdcidhcdn")
kws = KiteTicker(api_key, access_token, debug=True)

tokens = [260105,256265] #token numbers of instruments

def on_ticks(ws, ticks):
logging.debug("Ticks: {}".format(ticks))
print(ticks)

def on_connect(ws, response):
# Subscribe to a list of instrument_tokens
ws.subscribe([260105,256265])
ws.set_mode(ws.MODE_FULL,[260105])

def on_close(ws, code, reason):
# On connection close stop the main loop
# Reconnection will not happen after executing `ws.stop()`
ws.stop()

kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
kws.connect()

i am using this code to get data from websockets
still getting below error

C:\Users\Office\Downloads\ohlc_data_fetching_and_storing-main\ohlc_data_fetching_and_storing-main>py runss.py
Go to this URL and login: https://kite.zerodha.com/connect/login?api_key=xxx&v=3
Enter the request token obtained after login: ZHmCU5IgvnLuCEzcq6IttJyBn6YRjg81
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443
DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "POST /session/token HTTP/1.1" 200 None
Access token: xxxxxx
Refresh token:
Starting KiteTicker...
api_key access_token cdcidhcdn
DEBUG:kiteconnect.ticker:Start WebSocket connection.
2025-09-03 16:38:37+0530 [-] Log opened.
2025-09-03 16:38:37+0530 [-] failing WebSocket opening handshake ('WebSocket connection upgrade failed (403 - Forbidden)')
2025-09-03 16:38:37+0530 [-] dropping connection to peer tcp4:13.233.29.61:443 with abort=True: WebSocket connection upgrade failed (403 - Forbidden)
ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
  • Nivas
    Nivas edited September 3
    Please avoid sharing your API key, access token, and client and app specific details publicly. I've removed it for you this time. Additionally, market data cannot be accessed via WebSocket with the Kite Connect personal app (free version), which is why you encountered an error.
  • sachinzerodha
    we have added from different email paid version
    from email
    [email protected]
    we have 1000 rupee balance in it
  • Nivas
    It looks like the API key you mentioned earlier is associated with the free version. If you've set up a Kite Connect (paid version) app, you can use the API key and access token from that paid version app to resolve the issue.
  • sachinzerodha
    1. Do we currently have a paid plan active?
    we've paid already on the portal, but not sure as the API key appears to be still free
    2. Ask them the URL of billing page where you can check the active plan and upgrade ETC
    3.,why is this api key not providing the results and giving a 403 forbidden error still.
  • Nivas
    1. Do we currently have a paid plan active?
    Currently, there is no active paid app.
    we've paid already on the portal, but not sure as the API key appears to be still free
    2. Ask them the URL of billing page where you can check the active plan and upgrade ETC
    3.,why is this api key not providing the results and giving a 403 forbidden error still.
    You currently have credits in your developer account. To proceed, you need to create an app in the Kite Connect console and use the generated API key to fetch market data.

    Note: The Kite Personal app will not be converted to a paid version once you've added the credits. Instead, you have to create a new Kite Connect(paid version) app after adding the credits.
  • sachinzerodha
    Also i want to do i need to login daily to get access token can't i regenerate it with refresh token currently i am not able to do that
  • Nivas
    do i need to login daily to get access token
    This is done as per the exchange regulations. You need to login to the trading terminal every trading day. Go through the FAQs here.
    can't i regenerate it with refresh token
    A refresh_token is provided only for the platforms that are built for mass retail customers and not for individual users.
Sign In or Register to comment.