Using OAuth2 and Trading API
Alpaca implements OAuth 2.0 to allow third party applications to access Alpaca Trading API on behalf of the end-users. This document describes how you can integrate with Alpaca through OAuth.
By default once you have a valid client_id and client_secret, any paper account and the live account associated with the OAuth Client will be available to connect to your app. We welcome developers to build applications and products that are powered by Alpaca while also protecting the privacy and security of our users. To build using Alpaca’s APIs, please follow the guide below.
Getting the Access Token
At a high level the flow looks like this, we will go into detail about each step
- User requests a connection between your application and Alpaca
- User is redirected to Alpaca to login and authorize the application from inside the dashbaord
- Alpaca grants an authorization token to your application thorugh user-agent
- You application then makes an access token request
- Alpaca returns an access token grant.
1. Request for Connection on Behalf of User
When redirecting a user to Alpaca to authorize access to your application, you’ll need to construct the authorization URL with the correct parameters and scopes.
Here’s a list of parameters you should always specify:
|Required||Must be |
|Required||The redirect URL where the user will be sent after authorization. It must match one of the whitelisted redirect URIs for your application.|
|Optional||An unguessable random string, used to protect against request forgery attacks.|
|Optional||A space-delimited list of scopes your application requests access to. Read-only endpoint access is assumed by default.|
|Write access for account configurations and watchlists.|
|Place, cancel or modify orders.|
|Access to the Data API.|
We will be adding more scopes soon.
2. Users Authorizing App to Access Alpaca Account
From the user side, they will see the following authorization screen
3. Alpaca Redirect Back to App
If the user approves access, Alpaca will redirect them back to your
redirect_uri with a temporary
code parameter. If you specified a state parameter in step 1, it will be returned as well. The parameter will always match the value specified in step 1. If the values don’t match, the request should not be trusted.
4. App Receives Authorization Code
You can then use this code to exchange for an access token.
5. App Exchanges Auth Code with Access Token
After you have received the temporary
code, you can exchange it for an access token. This can be done by making a
POST call to
Parameters (All Required)
|Must be set to authorization_code for an access token request.|
|The authorization code received in step 4|
|The Client ID you received when you registered the application.|
|The Client Secret you received when you registered the application.|
|The redirect URI you used for the authorization code request.|
This request should take place behind-the-scenes from your backend server and shouldn’t be visible to the end users for security purposes.
Updated 24 days ago