SDK for interacting with the TD Ameritrade API.
Important: Before starting, you will need to make sure you have a developer application created (thus a client ID), and a valid refresh token. If you need help with either of these steps, you should refer to the following API Guide Pages:
After creating a Client
, you will need to give it an
access token. You can either use an existing one from your database or
filesystem, or fetch a new one from the API.
```rust use tda_sdk::Client;
let mut client = Client::new("CLIENTID", "REFRESHTOKEN", None);
let accesstoken = client.getaccess_token().unwrap();
// We must convert the token response into a token usable by the client. client.setaccesstoken(&Some(access_token.into())); ```
```rust use tda_sdk::{AccessToken, Client};
let accesstoken = AccessToken { expiresat: 0, token: "YOURTOKENSTRING".to_string(), scope: Vec::new(), };
let client = Client::new("CLIENTID", "REFRESHTOKEN", Some(access_token)); ```
After a token has been set, you may call any of the API methods. You can view all request parameters in the params module.
```rust use tda_sdk::{ Client, params::GetAccountsParams, responses::SecuritiesAccount, };
let mut client = Client::new("CLIENTID", "REFRESHTOKEN", None);
let accesstoken = client.getaccesstoken().unwrap(); client.setaccesstoken(&Some(accesstoken.into()));
let accounts = client.get_accounts(GetAccountsParams::default()).unwrap();
for account in accounts { match account.securitiesaccount { SecuritiesAccount::MarginAccount { r#type, accountid, .. } => { println!("Account ID: {}", account_id); println!("Account Type: {}", r#type); } } } ```
This library does not handle token expirations, that is up to the user.
However, the AccessToken
struct has a handy
method for detecting its expiration status.
Note: The get_access_token()
response has a different structure than
the token expected by the client. You will need to parse the response.
```rust use tda_sdk::{AccessToken, Client};
let client = Client::new("CLIENTID", "REFRESHTOKEN", None); let accesstoken: AccessToken = client.getaccess_token().unwrap().into();
if accesstoken.hasexpired() { panic!("Token has expired!"); } ```