Invalid token


I was trying to get the historical data for a single instrument token using kiteconnect. The code for the same is as shown:

from kiteconnect import KiteConnect
import os
from nsepy import get_history
import datetime as dt
import pandas as pd

cwd = os.chdir("C:/Users/Administrator/Desktop/Zerodha")

#generate trading session
access_token = open("access_token.txt",'r').read()
key_secret = open("credentials.txt",'r').read().split()
kite = KiteConnect(api_key=key_secret[0])

vfrom = "2018-01-03 1"
vto = "2018-01-05"

ohlc = kite.historical_data('SNOWMAN','vfrom','vto','30minute')

When I run the code I get the following error: 'invalid token' as shown:

I must mention that the API token is freshly generated today and the other kiteconnect calls such as those for fetching quotes and ltp work perfectly.

May I know why is my call not working?

  • syker
    Have you also taken the Addons for Historical API???
  • Hsadikot
    Yes I have
  • syker
    syker edited December 2020
    Invalid token error is coming because of historical_data function parameter incorrect input ("SNOWMAN"). Instead, its token id should be passed to the function, which is in the current case is 1239809. You can find the list of instruments token id using this link ( There are other mistakes in the date that you have entered and the single quote (" ' ") used for variable vfrom and vto in the function. Hence I am sharing the code for your reference.

    from kiteconnect import KiteConnect
    from kiteconnect import KiteTicker
    import os
    import datetime as dt
    import pandas as pd

    api_k = "XXXXXXXXXXXXXX" #enter your api_key
    api_s = "XXXXXXXXXXXXXX" #enter your api_secret

    def get_login(api_k, api_s): # log in to zerodha API panel
    global kws, kite
    kite = KiteConnect(api_key=api_k)

    print("[*] Generate access Token : ", kite.login_url())
    request_tkn = input("[*] Enter Your Request Token Here : ")

    data = kite.generate_session(request_tkn, api_secret=api_s)

    kws = KiteTicker(api_k, data["access_token"])
    # print(data['access_token'])

    get_login(api_k, api_s)

    vfrom = "2020-08-05"
    vto = "2020-12-10"

    ohlc = kite.historical_data(12053506,vfrom,vto,'30minute')

Sign In or Register to comment.