TastyTrade API Ruby gem
An open source SDK implementation of the TastyTrade API for Ruby.
Installation
In your Gemfile
gem 'tastytrade-api-ruby', git: 'https://github.com/wakproductions/schwab-api-ruby.git'
Authenticating
TastyTradeAPI authenticates via OAuth2. First, you need to send your user to:
https://my.tastytrade.com/auth.html?response_type=code&client_id=#{client_id}&scope=readonly&redirect_uri=#{redirect_uri}
In the URL, populate the client_id and redirect_uri for the settings you specified for your app in TastyTrade.
On the redirect_uri callback, you will receive the authorization grant code. You can then process it using code similar to this:
# https://127.0.0.1/tastytrade_api/callback?scope=readonly&code=<uuid>
def callback
authorization_code = params[:code]
if authorization_code.present?
authenticate_tastytrade(authorization_code)
# continue processing successful authorization
end
end
private
def authenticate_tastytrade(authorization_code)
client = Tastytrade::Client.new(
client_id: ENV.fetch('TASTYTRADE_CLIENT_ID'),
secret: ENV.fetch('TASTYTRADE_SECRET'),
redirect_uri: ENV.fetch('TASTYTRADE_REDIRECT_URI'),
)
# Returns:
# {
# access_token: <access token>,
# expires_in: <seconds to access token expiration>,
# refresh_token: <refresh token>,
# id_token: <id token>
# }
access_token_info = client.request_access_token(authorization_code)
unless access_token_info['access_token'].present? && access_token_info['refresh_token'].present?
raise "Error retrieving access token: #{access_token_info}"
end
# continue processing
end
Once you have the access token, you can initialize a new client with the values from TastyTrade:
Tastytrade::Client.new(
client_id: ENV.fetch('TASTYTRADE_CLIENT_ID'),
secret: ENV.fetch('TASTYTRADE_SECRET'),
redirect_uri: ENV.fetch('TASTYTRADE_REDIRECT_URI'),
access_token:,
refresh_token:,
access_token_expires_at:,
refresh_token_expires_at:
)
According to the TastyTrade docs, the refresh token is long lived and does not expire. You can use the client to retrieve a new access token using the refresh token:
client.refresh_access_token
Basic Usage
You first need to perform
client = SchwabAPI::Client.new(
client_id: <client_id>,
redirect_uri: 'https://127.0.0.1',
refresh_token: '<refresh_token>'
)
client.get_instrument_fundamentals('MSFT')
#=> {"MSFT"=>
{"fundamental"=>
{"symbol"=>"MSFT",
"high52"=>425.24,
"low52"=>340.33,
...
Current State of Functionality
The official API is documented here. Not all API endpoints are implemented yet. Submit a PR or open a Github issue if you'd like to request a feature.
- Authentication
- Account and Customer Info
- Instruments
- Margin Requirements
- Market Data
Contributions
If you would like to make a contribution, please submit a pull request to the original branch. Open a Github issue for any feature requests, bug reports, or to provide feedback.
Release Notes
See CHANGELOG.md