Trouble Accessing kite.quote - Incorrect API Key or Access Token Error


I'm encountering an issue where I'm able to retrieve the instrument list using the initialized Kite object, but when I attempt to use kite.quote to fetch quote data, it throws an error indicating an incorrect api_key or access_token. I have attached a screenshot of the error message along with the relevant code snippet below:

from kiteconnect import KiteConnect
import os

# Initialize Kite Connect with your API key and secret
cwd = os.chdir("/Users/hajareshyam/Work/option-chain/")

#generate trading session
key_secret = open("api_key.txt",'r').read().split()
access_token = open("access_token.txt",'r').read()

kite = KiteConnect(api_key=key_secret[0])
data = kite.set_access_token(access_token)
instrument_list = kite.instruments("NFO")

# Fetch underlying price for NIFTY BANK
quote_data = kite.quote("NSE:NIFTY BANK")
underlying_price = quote_data["NSE:NIFTY BANK"]["last_price"]
except Exception as e:
print("Error fetching underlying price:", str(e))

# print(underlying_price)
  • MAG
    MAG edited April 20
    The instrument list is public information and hence the kite.instruments() will succeed even if you are not authenticated. But to get any other account information or place /modify orders authentication is needed.

    Instead of using the kite.instruments call as a marker/check I would suggest people use the kite.profile() call to check whether the script/connection has authenticated successfully. If authentication is successful you would see your profile details. If the kite.profile call fails, it means your authentication has failed.

    In this case your script / connection was never authenticated. Just because instruments call returned data successfully you assumed that the connection was authenticated.

    @sujith @rakeshr Seeing this often. So maybe this needs to be changed such that instruments call also fails if not authenticated. Or else the documentation is updated to reflect the fact that kite.instruments() call will succeed irrespective of whether the kite connection object is authenticated or not.
  • sujith
    I will share the feedback with the team.
Sign In or Register to comment.