This blog was last updated on November 7, 2024.

Connecting to Alpaca's Trading API allows you to access and trade stocks programmatically using Python. This guide will walk you through the process of creating your Alpaca Trading API account, generating your API Key, and using Google Colab as your integrated development environment (IDE).

Alpaca - Commission-Free API First Stock Brokerage
Alpaca is a modern platform for trading. Alpaca’s API is the interface for your trading algorithms, bots, or applications to communicate with Alpaca’s brokerage and other services.

Prerequisites:

  1. Sign Up for a Free Account: If you haven't already, create a free Alpaca Trading API account. This will give you access to your own API Keys and a paper trading account to test trade. Note: If your country is not listed under the Country of Tax Residence, you will only have access to paper trading. Curious if you can live trade in your country? Check out the list of countries with access to live trading.
  1. Choose your IDE and Language: Trading API can be used with various languages and IDEs. In this tutorial, we’ll be using Python and Google Colab. If you’re coding in Python, other common IDEs you could use include Visual Studio Code, Jupyter Notebook, and Pycharm. You can also use non-Python IDEs like Postman.

Step 1: How to Get Your Alpaca API Keys

  1. Log in to your Alpaca Trading API account.
  2. Obtain API Keys: After logging in, navigate to the “Home” section to generate your API Key and Secret API Key. Note: It’s important to write down or save your Secret Key, as you will only have access to it once. You can regenerate your API and Secret API Keys at any time. However, you will need to re-establish your connection with your IDE using the same process described below.

Step 2: Setting Up Google Colab

  1. Open Google Colab: Go to Google Colab in your browser.
  2. Create a new Notebook: Click the “New Notebook” button. This will initialize a Python environment for us to run the code in.
  3. Install Required Packages: Install Alpaca-specific Python packages using the command:
!pip install alpaca-py requests

Step 3: Authentication and Connecting to Alpaca’s Trading API

  1. Connect to Alpaca’s Trading API: Use the code provided below to initialize your connection to your Alpaca Trading API account. Insert your API Key in place of 'YOUR_API_KEY_ID' and the Secret API Key into the 'YOUR_API_SECRET_KEY' to connect to your session.
import alpaca_trade_api as alpaca

api = alpaca.REST('YOUR_API_KEY_ID', 'YOUR_API_SECRET_KEY', 'https://paper-api.alpaca.markets')
  1. Fetch Account Information: Verify your setup by fetching account details.
account = api.get_account()
print(account)

You should see your account details show up and confirm that you’ve successfully connected to your Alpaca Trading API account. 

And that’s it, you're ready to start trading with Alpaca!

In our next tutorial, learn how to start paper trading with Alpaca’s Trading API.


The Paper Trading API is offered by AlpacaDB, Inc. and does not require real money or permit a user to transact in real securities in the market. Providing use of the Paper Trading API is not an offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, given or in any manner endorsed by AlpacaDB, Inc. or any AlpacaDB, Inc. affiliate and the information made available through the Paper Trading API is not an offer or solicitation of any kind in any jurisdiction where AlpacaDB, Inc. or any AlpacaDB, Inc. affiliate (collectively, “Alpaca”) is not authorized to do business.

Please note that this article is for general informational purposes only and is believed to be accurate as of the posting date but may be subject to change. The examples above are for illustrative purposes only.

Securities brokerage services are provided by Alpaca Securities LLC ("Alpaca Securities"), member FINRA/SIPC, a wholly-owned subsidiary of AlpacaDB, Inc. Technology and services are offered by AlpacaDB, Inc.

This is not an offer, solicitation of an offer, or advice to buy or sell securities or open a brokerage account in any jurisdiction where Alpaca Securities are not registered or licensed, as applicable.