How to Use OAuth to Access Alpaca API?
This tutorial will be a step by step demonstration of connecting to OAuth using the Alpaca API. We will move forward assuming that you’ve registered on the Alpaca website. If you haven’t please visit Alpaca and sign up. Feel free to check out the other tutorials in this series at Alpaca Resources.
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.
Step 1:
Let’s Create a New OAuth app at the OAuth page in your account.
Step 2:
Enter the information for the app. Note all OAuth apps need to be approved by Alpaca before they can execute trades on behalf of users.
Step 3:
As soon as you create the app, you will be issued your Client ID and Client Secret ID. These can be found by clicking “View App Credentials”.
Step 4:
To integrate your application with Alpaca, use the following flow:
GET https://app.alpaca.markets/oauth/authorize?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URL&state=SOMETHING_RANDOM&scope=account:write%20trading
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:
Step 5:
You will see an authorization screen. Once you approve access Alpaca will redirect you 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.
Step 6:
Your application will receive the authorization code, which can be used to exchange for an access token by making a POST request. If your app is meant for funded accounts you would use the api.alpaca.markets
endpoint vs. the paper-api.alpaca.markets
endpoint for paper trading
Once you have integrated and have a valid access token you can start making calls to Alpaca Trading API v2 on behalf of the end-user.
Thank you for using Alpaca. Here are a couple more links that you might be interested in.
Sign up for the weekly newsletter to keep up with the API updates and upcoming competitions, job opportunities by clicking here.
You can also follow Alpaca and our weekly updates on our LinkedIn, Alpaca Community Slack and @AlpacaHQ on Twitter!
Commission-Free trading means that there are no commission charges for Alpaca self-directed individual cash brokerage accounts that trade U.S. listed securities through an API. Relevant SEC and FINRA fees may apply.
Brokerage services are provided by Alpaca Securities LLC ("Alpaca"), memberFINRA/SIPC, a wholly-owned subsidiary of AlpacaDB, Inc. Technology and services are offered by AlpacaDB, Inc.