Getting Invalid Token Error during the trading session randomly.


  • Getting Invalid Token Error during the trading session randomly.

    I am generating access token around 8.30 to 9.00 AM and that access token is used during the day. But everyday that token is invalidate after sometimes.

    [E 240607 10:25:10 smartConnect:243] Error occurred while making a POST request to https://apiconnect.angelbroking.com/rest/secure/angelbroking/order/v1/placeOrder. Error: Invalid Token. URL: https://apiconnect.angelbroking.com/rest/secure/angelbroking/order/v1/placeOrder, Headers: {'Content-type': 'application/json', 'X-ClientLocalIP': '127.0.0.1', 'X-ClientPublicIP': '106.193.147.98', 'X-MACAddress': '3b:6f:e8:a0:38:7f', 'Accept': 'application/json', 'X-PrivateKey': '3cob7ymd', 'X-UserType': 'USER', 'X-SourceID': 'WEB'}, Request: {'variety': 'NORMAL', 'tradingsymbol': 'BANKNIFTY12JUN2449400PE', 'symboltoken': '36794', 'transactiontype': 'BUY', 'exchange': 'NFO', 'ordertype': 'MARKET', 'producttype': 'CARRYFORWARD', 'duration': 'DAY', 'price': '0', 'triggerprice': '0', 'trailingStopLoss': '0', 'squareoff': '0', 'stoploss': '0', 'quantity': 15}, Response: {'success': False, 'message': 'Invalid Token', 'errorCode': 'AG8001', 'data': ''}

    If you see, error came at 10:25:10 AM, but order was placed successfully 20 seconds prior to that.


  • Home @easemyalgotrade

    In this case also Authorization key is missing from header.
    So please have a look on the below link then cross check and confirm

    Regards,
    SmartAPI Team


  • Today also hit the same issue for client ID S436951

    Order is place at 9:40AM

    2024-06-10 09:40:14,772 [140129280554752-Thread-125 (worker)] [AngelClient.place_order:639] [DEBUG] S436951 => 240610000250438 => {'ordertype': 'MARKET', 'transactiontype': 'BUY', 'instrument': Instrument(exchange='NFO', token='43981', symbol='NIFTY13JUN2423300PE', name='NIFTY', expiry=datetime.date(2024, 6, 13), lot_size=25), 'token': '43981', 'exchange': 'NFO', 'symbol': 'NIFTY13JUN2423300PE', 'quantity': 100, 'producttype': 'I'} => {'variety': 'NORMAL', 'tradingsymbol': 'NIFTY13JUN2423300PE', 'symboltoken': '43981', 'transactiontype': 'BUY', 'exchange': 'NFO', 'ordertype': 'MARKET', 'producttype': 'INTRADAY', 'duration': 'DAY', 'price': '0', 'triggerprice': '0', 'trailingStopLoss': '0', 'squareoff': '0', 'stoploss': '0', 'quantity': 100}

    And then Invalid API error came at 10:15AM with the same auth token.
    2024-06-10 10:15:04,106 [140129574299392-Thread-147 (worker)] [AngelClient.place_order:639] [DEBUG] S436951 => None => {'ordertype': 'MARKET', 'transactiontype': 'SELL', 'instrument': Instrument(exchange='NFO', token='36845', symbol='BANKNIFTY12JUN2450000PE', name='BANKNIFTY', expiry=datetime.date(2024, 6, 12), lot_size=15), 'token': '36845', 'exchange': 'NFO', 'symbol': 'BANKNIFTY12JUN2450000PE', 'quantity': 60, 'producttype': 'I'} => {'variety': 'NORMAL', 'tradingsymbol': 'BANKNIFTY12JUN2450000PE', 'symboltoken': '36845', 'transactiontype': 'SELL', 'exchange': 'NFO', 'ordertype': 'MARKET', 'producttype': 'INTRADAY', 'duration': 'DAY', 'price': '0', 'triggerprice': '0', 'trailingStopLoss': '0', 'squareoff': '0', 'stoploss': '0', 'quantity': 60}

    (angel python sdk logs)
    [E 240610 10:15:04 smartConnect:243] Error occurred while making a POST request to https://apiconnect.angelbroking.com/rest/secure/angelbroking/order/v1/placeOrder. Error: Invalid Token. URL: https://apiconnect.angelbroking.com/rest/secure/angelbroking/order/v1/placeOrder, Headers: {'Content-type': 'application/json', 'X-ClientLocalIP': '127.0.0.1', 'X-ClientPublicIP': '106.193.147.98', 'X-MACAddress': '3b:6f:e8:a0:38:7f', 'Accept': 'application/json', 'X-PrivateKey': 'zSlc4Y3b', 'X-UserType': 'USER', 'X-SourceID': 'WEB'}, Request: {'variety': 'NORMAL', 'tradingsymbol': 'BANKNIFTY12JUN2450000PE', 'symboltoken': '36845', 'transactiontype': 'SELL', 'exchange': 'NFO', 'ordertype': 'MARKET', 'producttype': 'INTRADAY', 'duration': 'DAY', 'price': '0', 'triggerprice': '0', 'trailingStopLoss': '0', 'squareoff': '0', 'stoploss': '0', 'quantity': 60}, Response: {'success': False, 'message': 'Invalid Token', 'errorCode': 'AG8001', 'data': ''}

    I am assuming your python sdk is working fine and sending auth key in headers everytime.


  • Hello @easemyalgotrade
    You are missing the Autherization key in header.
    Please go through the attached link to get the exact header body and then try
    https://smartapi.angelbroking.com/docs/Orders

    Regards,
    SmartAPI Team