How to get the stock option train data with tda-api?

Viewed 157

I have registered a tdameritrade developer account and got a api key.

curl -X GET --header "Authorization: "    \
"https://api.tdameritrade.com/v1/marketdata/aapl/quotes?apikey=my_tda_api_key"

It can get aapl's data such as below:

{"AAPL":    {"assetType":"EQUITY","assetMainType":"EQUITY","cusip":"037833100","symbol":"AAPL",     
#omitted all other info        

Now i want to get aapl's stock option train with curl and my api key:

curl -X GET --header "Authorization: " "https://api.tdameritrade.com/v1/marketdata/chains?   \
apikey=my_tda_api_key&                                                                           \
symbol=aapl&contractType=all&includeQuotes=false&strategy=single&interval=1&range=all&       \
fromDate=2021-8-14&toDate=2023-8-14&expMonth=all&optionType=all"

I always get all HTTP/1.1 400 Bad Request ,tried many different arguments combination in the url ,still can't get normal response,how to fix then?

https://developer.tdameritrade.com/option-chains/apis/get/marketdata/chains#

1 Answers
import requests
import json
td_consumer_key = "xxxxxxxxxxxxxxxxxxxxxx"
base_url = 'https://api.tdameritrade.com/v1/marketdata/chains?\
            &symbol={stock_ticker}&contractType={contract_type}\
            &strike={strike}&fromDate={date}&toDate={date}'    
endpoint = base_url.format(stock_ticker = 'AAPL',
    contract_type = 'PUT',
    strike = 125,
    date='2021-08-27')    
page = requests.get(url=endpoint, 
            params={'apikey' : td_consumer_key})    
content = json.loads(page.content)
Related