Select Page

TD Ameritrade is one of the leading US online brokers. This tutorial details how to create a Developer Account and your first API. While APIs of some online brokers does not support OAuth authentication. The official API documentation can be tricky especially with regards to getting a valid code and access token. This blog post will help you create them so you can start accessing APIs for quotes, trading, history, and so on. Finally this post will show how to use an API to get the real-time market quote for Apple (AAPL).

This tutorial explains:

How you can setup a TD Ameritrade Developer Account

Create your first API

Generate an authentication token

Query real-time stock market quote for Apple (AAPL)


Step 1 Register a Developer’s Account at https://developer.tdameritrade.com/

TD Ameritrade API

Step 2 After creating an account, login to your Developer’s Account. Select “My Apps” from the menu. Click “Add a new App” button.

Step 3 Enter the App Name. In the Callback URL field, fill in https://localhost . Write the purpose of the application and the Order Limit.

Step 4 Turn on your Windows localhost for the Callback URL to work properly. In Control Panel, choose Programs > Turn Windows features on or off. Check Internet Information Services Hostable Web Core. Click OK button. Note that you do not have to restart your computer for the change to take effect.

Step 5 From “My Apps”, select the recently added app. Copy the App’s Consumer Key under Keys tab.

Step 6 Under Details tab, copy Callback URL.

Step 7 Enter the URL in a web browser:

https://auth.tdameritrade.com/auth?response_type=code&redirect_uri=APP_CALLBACK_URL&client_id=APP_CONSUMER_KEY%40AMER.OAUTH

Where APP_CALLBACK_URL is the Callback URL from Step 6,

and APP_CONSUMER_KEY is the Consumer Key from Step 5.

Example URL:

https://auth.tdameritrade.com/auth?response_type=code&redirect_uri=https://localhost&client_id=JEJAAAAAAAAAAAALCA%40AMER.OAUTHAP

Step 8 The next steps will be for getting the authorization code that you will use to get an access token which will be needed to query TD Ameritrade APIs.

Enter the URL from Step 7 into a browser. This will display a login screen. Enter the User ID and Password of your TD Ameritrade brokerage account. Then click the Log In button.

Step 9 TD Ameritrade will ask you for authorize your App to access your brokerage account, i.e., to be able place trades, display balances and move money. The below screen will be displayed. Click “Allow” to authorize your App.

Step 10 The response will redirect to a 404 page similar to screenshot below. Copy everything after “code=” in a Notepad.

Step 11 To decode the authorization code, open this URL Decoder https://meyerweb.com/eric/tools/dencoder/ in a new browser. Paste the code copied from Step 10.

Step 12 Click the “Decode” button.

Step 13 To get the access token which can be used to access other APIs, select APIs from the top panel –> Authentication –> Post Access Token .

Alternatively you may open https://developer.tdameritrade.com/authentication/apis/post/token-0 in a new browser.

Step 14 Fill in the fields:

NameValuesDescription
grant_type (required)authorization_codeThe grant type of the oAuth scheme. Possible values are authorization_code, refresh_token
refresh_tokenRequired if using refresh token grant
access_typeofflineSet to offline to receive a refresh token on an authorization_code grant type request. Do not set to offline on a refresh_token grant type request.
code(Copy the decoded URL from Step 12)Required if trying to use authorization code grant
client_id(Copy the API Consumer Key from Step 5)
OAuth User ID of your application
redirect_url (Copy the Callback URL from Step 6)Required if trying to use authorization code grant

Click “Send” button.

Step 15a Scroll down to the Response tab, which is below the “Send” button. If you received a 400 response (Bad Request), check the Body Parameters. This is usually caused by an incorrect code entered. Re-do the previous steps until you receive a successful response.

Step 15b When the response received by your Post Access Token query is 200 OK, copy the access_token.

Step 16 To use the Quotes API, open https://developer.tdameritrade.com/quotes/apis/get/marketdata/quotes Fill in the following fields:
apikey = (Refer to Step 5)
symbol = AAPL
Authorization = (Refer to Step 15b)

Step 17 Before sending the Get Quotes query, click on Oauth 2.0 box to set authorization parameters to grant permission to access the market data quotes.

A Request quotes permissions screen will be displayed. Click the OK button.

Step 18 A new browser window will pop-up. Make sure to allow access from your browser. Login to your TD Ameritrade brokerage account.

After successful login, the login browser will automatically close and the Oauth 2.0 box will display “Authenticated”.

Step 19 Click the Send button. The Request tab will display the details of the request sent to TD Ameritrade.

The Response tab displays the 200 status response and the JSON data info for AAPL market data.

TD Ameritrade Get Market Data JSON Response

{
  "AAPL": {
    "assetType": "EQUITY",
    "assetMainType": "EQUITY",
    "cusip": "037833100",
    "symbol": "AAPL",
    "description": "Apple Inc. - Common Stock",
    "bidPrice": 168.35,
    "bidSize": 2700,
    "bidId": "P",
    "askPrice": 168.6,
    "askSize": 500,
    "askId": "P",
    "lastPrice": 168.48,
    "lastSize": 0,
    "lastId": "P",
    "openPrice": 168.02,
    "highPrice": 169.87,
    "lowPrice": 166.64,
    "bidTick": " ",
    "closePrice": 165.75,
    "netChange": 2.73,
    "totalVolume": 79265181,
    "quoteTimeInLong": 1649807996864,
    "tradeTimeInLong": 1649807996332,
    "mark": 168.35,
    "exchange": "q",
    "exchangeName": "NASD",
    "marginable": true,
    "shortable": true,
    "volatility": 0.0633,
    "digits": 4,
    "52WkHigh": 182.94,
    "52WkLow": 122.25,
    "nAV": 0,
    "peRatio": 28.2389,
    "divAmount": 0.88,
    "divYield": 0.53,
    "divDate": "2022-02-04 00:00:00.000",
    "securityStatus": "Normal",
    "regularMarketLastPrice": 167.66,
    "regularMarketLastSize": 36092,
    "regularMarketNetChange": 1.91,
    "regularMarketTradeTimeInLong": 1649793601299,
    "netPercentChangeInDouble": 1.6471,
    "markChangeInDouble": 2.6,
    "markPercentChangeInDouble": 1.5686,
    "regularMarketPercentChangeInDouble": 1.1523,
    "delayed": false,
    "realtimeEntitled": true
  }
}