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](https://techiejackieblogs.com/wp-content/uploads/2021/05/tdameritrade-api-access-01-homepage.png)
![](https://techiejackieblogs.com/wp-content/uploads/2021/05/tdameritrade-api-access-02-register-developer-account-3.png)
Step 2 After creating an account, login to your Developer’s Account. Select “My Apps” from the menu. Click “Add a new App” button.
![](https://techiejackieblogs.com/wp-content/uploads/2021/12/tdameritrade-api-01-1024x118.png)
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.
![](https://techiejackieblogs.com/wp-content/uploads/2021/05/tdameritrade-api-access-04-add-a-new-app-2-1024x300.png)
![](https://techiejackieblogs.com/wp-content/uploads/2021/05/tdameritrade-api-access-05-create-a-new-app-2-1024x601.png)
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.
![](https://techiejackieblogs.com/wp-content/uploads/2021/05/tdameritrade-api-access-06-setup-windows-hostable-1.png)
Step 5 From “My Apps”, select the recently added app. Copy the App’s Consumer Key under Keys tab.
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/01-td-ameritrade-key-1024x455.png)
Step 6 Under Details tab, copy Callback URL.
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/03-td-ameritrade-details-1024x532.png)
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:
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.
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/05-td-ameritrade-build-auth-code-1024x733.png)
![](https://techiejackieblogs.com/wp-content/uploads/2021/05/tdameritrade-api-access-09-tda-auth-url-1-1024x25.png)
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.
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/06-td-ameritrade-authorization-1024x726.png)
Step 10 The response will redirect to a 404 page similar to screenshot below. Copy everything after “code=” in a Notepad.
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/07-td-ameritrade-authorization-1024x495.png)
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.
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/07a-td-ameritrade-decode-url.png)
Step 12 Click the “Decode” button.
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/07-td-ameritrade-decoded-url-1.png)
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:
Name | Values | Description |
grant_type (required) | authorization_code | The grant type of the oAuth scheme. Possible values are authorization_code, refresh_token |
refresh_token | Required if using refresh token grant | |
access_type | offline | Set 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.
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/08-td-ameritrade-get-post-access-token-2-1024x858.png)
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.
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/09-td-ameritrade-get-access-token-failed.png)
Step 15b When the response received by your Post Access Token query is 200 OK, copy the access_token.
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/09-td-ameritrade-get-access-token-success-200-response-4-538x1024.png)
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)
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/10-td-ameritrade-get-quote-2-1024x885.png)
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.
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/10-td-ameritrade-get-quote-set-oauth-2.png)
A Request quotes permissions screen will be displayed. Click the OK button.
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/11-Request-quotes-permission-td-ameritrade.png)
Step 18 A new browser window will pop-up. Make sure to allow access from your browser. Login to your TD Ameritrade brokerage account.
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/12-TD-ameritrade-oauth-2-for-get-quotes-api.png)
After successful login, the login browser will automatically close and the Oauth 2.0 box will display “Authenticated”.
![](https://techiejackieblogs.com/wp-content/uploads/2021/05/tdameritrade-api-access-19-quote-metadata-authenticated-1.png)
Step 19 Click the Send button. The Request tab will display the details of the request sent to TD Ameritrade.
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/12-Request-quotes-td-ameritrade.png)
The Response tab displays the 200 status response and the JSON data info for AAPL market data.
![](https://techiejackieblogs.com/wp-content/uploads/2022/04/13-TD-ameritrade-200-response-get-quotes-api.png)
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
}
}