Issue with Fetching Historical Stock Options Data via API - HTTP 400 Error

I am experiencing an issue with the /v2/charts/rollingoption API endpoint. While retrieving historical data for index options (OPTIDX) works fine, I consistently receive an HTTP 400 error when attempting to fetch data for stock options (OPTSTK).

Here are the key details:

  • API Endpoint: api. dhan .co /v2 /charts /rollingoption
  • Error: HTTP 400 (No detailed error message)
  • Stock Symbols Tested: RELIANCE, TCS, INFY, SBIN, HDFCBANK, and others
  • Index options data works fine with the same API, while stock options return the 400 error.

I’ve tested various combinations of parameters (expiryFlag, strike price, date ranges). Despite this, the issue persists.

Could you kindly confirm:

  1. Does the endpoint support stock options (OPTSTK)?
  2. Are there any known limitations or restrictions for stock options data?
  3. Is there an alternative method to fetch stock options data?

Request Structure for Stock Options Historical Data (HTTP 400 Error)

POST https:// api .dhan .co /v2 /charts /rollingoption

{
“access-token”: “YOUR_ACCESS_TOKEN”,
“Content-Type”: “application/json”
}

Request Body (Stock Options - FAILING with HTTP 400):

{
“securityId”: “2885”,
“exchangeSegment”: “NSE_FNO”,
“instrument”: “OPTSTK”,
“expiryFlag”: “M”,
“expiryCode”: 0,
“strike”: “ATM”,
“drvOptionType”: “CALL”,
“requiredData”: [
“open”,
“high”,
“low”,
“close”,
“volume”,
“oi”,
“iv”,
“strike”,
“spot”
],
“fromDate”: “2025-09-01”,
“toDate”: “2025-09-30”
}

Result: HTTP 400 :cross_mark:

Hey @sathwik_natha, Good to see you after a while!

Thanks for sharing your query! Our new API Support Chatbot has already analyzed your query and suggested the following solution👇

:light_bulb: ChatBot Suggestion:
I can help troubleshoot your issue with the /v2/charts/rollingoption endpoint for stock options data. Based on the documentation and your request structure, here are some key issues and corrections:

  • exchangeSegment should be "NSE_FNO" for stock options
  • expiryFlag must be "MONTH" (not "M")
  • fromDate should not be a future date
  • Ensure you’re using the correct security ID from Dhan’s Security Master
  • The HTTP 400 error likely indicates invalid parameters

:white_check_mark: Try this corrected request structure:

import requests
import json

url = "https://api.dhan.co/v2/charts/rollingoption"
headers = {
    "Accept": "application/json",
    "Content-Type": "application/json",
    "access-token": "YOUR_ACCESS_TOKEN"
}

payload = {
    "securityId": 2885,  # Integer, not string
    "exchangeSegment": "NSE_FNO",
    "instrument": "OPTSTK",
    "expiryFlag": "MONTH",  # Use full word "MONTH" not "M"
    "expiryCode": 1,  # 1 for current month, not 0
    "strike": "ATM",
    "drvOptionType": "CALL",
    "requiredData": [
        "open", "high", "low", "close", 
        "volume", "oi", "iv", "strike", "spot"
    ],
    "fromDate": "2023-09-01",  # Use past date, not future
    "toDate": "2023-09-30"
}

try:
    response = requests.post(url, headers=headers, data=json.dumps(payload))
    response.raise_for_status()
    print(response.json())
except requests.exceptions.HTTPError as e:
    error_code = response.status_code
    error_message = response.text
    print(f"HTTP Error: {error_code}")
    print(f"Error Message: {error_message}")
    
    # Map error codes to Dhan error types
    if error_code == 400:
        print("This could be DH-905 (Input Exception) - Check parameters")
    elif error_code == 401:
        print("This could be DH-901 (Invalid Authentication)")

Please give this a try and let us know how it goes. You can also check out our API ChatBot available on Introduction - DhanHQ Ver 2.0 / API Document — it’s great for instant help with API-related issues. :rocket:

If it doesn’t fix the problem, @Hardik @Shrutika_Poojari will jump in to help further.

1 Like

Hi @Sameet,

Thank you for your response.

I tested the exact code from the chatbot - still getting DH-905
I implemented the exact parameters suggested:

  • :white_check_mark: exchangeSegment: "NSE_FNO"
  • :white_check_mark: expiryFlag: "MONTH" (not “M”)
  • :white_check_mark: expiryCode: 1 (not 0)
  • :white_check_mark: securityId: 2885 (integer)
  • :white_check_mark: Past dates: “2023-09-01” to “2023-09-30”
  • :white_check_mark: Used [data=json.dumps(payload)] as shown in example
  • :white_check_mark: Also tried NSE_I as suggested by API ChatBot again

Result: Still HTTP 400 DH-905 for all variations.

Can someone from Dhan support actually please test this endpoint and confirm it works? Or provide the complete working code? I’ve tried every combination suggested and nothing works - not even for index options which worked for us previously.

Hi @sathwik_natha, I’m just chiming in to confirm that the suggested code by the bot does not work.

Output:

HTTP Error: 400
Error Message: {"errorType":"Input_Exception","errorCode":"DH-905","errorMessage":"Missing required fields, bad values for parameters etc."}
This could be DH-905 (Input Exception) - Check parameters

Can you please look into this?

1 Like

Hey @sathwik_natha @sriv

Let me try this out and get back to you. Can you confirm which stock are you trying to fetch the data for? Also, if this is coming up for all instruments or only a few?

I am also getting the same error for crude oil expired options

RuntimeError: HTTP 400: {“errorType”:“Input_Exception”,“errorCode”:“DH-905”,“errorMessage”:“Missing required fields, bad values for parameters etc.”}