How to fetch Nifty 50 LTP from Dhan API?

Hi, I am new to Dhan Api and using gemini to build a code to fetch nifty 50 LTP and 20 market depth data for nifty 50 stocks. I am stuck in fetching nifty 50 data.

Tried the below code i found in this forum, as a proxy to LTP, but getting error

!pip install dhanhq
from dhanhq import dhanhq
from datetime import datetime, timedelta

client_id =
access_token = 

dhan = dhanhq(client_id,access_token)

BN=dhan.historical_daily_data(
    symbol='BANKNIFTY',
    exchange_segment='IDX_I',
    instrument_type='INDEX',
    expiry_code=0,
    from_date='2024-09-01',
    to_date='2024-09-18'
)

# This is optional , just to convert time stamps properly
data= pd.DataFrame(BN['data'])
data['start_Time'] = data['start_Time'].astype(int)
data['start_Time'] = data['start_Time'].apply(dhan.convert_to_date_time)
data

Hi @SOMASHEKAR_A_C

To fetch data using APIs, you will need to use security IDs instead of symbol names. You can find the required security IDs here: Instrument List - DhanHQ Ver 2.0 / API Document. For your reference, below is the code snippet to pull data:

Historical Daily Data
dhan.historical_daily_data(security_id, exchange_segment, instrument_type, from_date, to_date)

You may also check our Python library here: GitHub - dhan-oss/DhanHQ-py: The official Python client for communicating with the Dhan API.

Hi @SOMASHEKAR_A_C ,

In addition to the above, to access the 20-level DOM data, please refer to the documentation here: Full Market Depth - DhanHQ Ver 2.0 / API Document

Kindly ensure that the request is made as per the connection flow and the required request structure mentioned in the guide.