How to Use the Snapshot API in Alpaca Market Data API v2
In this tutorial, we show how to use the Snapshot API of the Alpaca Market Data API v2 for both single and multiple stock symbols
This tutorial is a step-by-step demonstration of how to use the Snapshot API in Alpaca Market Data API v2. This guide is part of the Alpaca 101 Tutorial Series. 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.
About the Alpaca Market Data API v2
Data is the backbone of API access to the market, and we have established our very own data product that can evolve alongside the Alpaca platform. We are starting minimally with real-time and historical price data and will be adding various types of data suited for the Alpaca users. Users can find the documentation for the Alpaca Market Data API v2 below:
This tutorial will cover how to use the Snapshot API in Alpaca Market Data API v2. While the Snapshot API is most frequently used in real-time, the endpoint is prefixed with the same URL one would use to access historical data with the Alpaca Market Data API v2: https://data.alpaca.markets/v2
Setting up Postman
For this tutorial, we will be implementing Postman to test any API calls which we send to the Alpaca Market Data API v2. As of right now, Alpaca Data API v2 provides three types of historical data which are Trades, Quotes, and Bars. That being said, either generate new API keys or fetch your previously generated paper API keys. Now that you have your keys ready, open up the Postman application.
- To initiate your first API request, click the + tab in the Postman UI.
- Add the following request URL:
https://paper-api.alpaca.markets/v2/account - Add your API keys
Click the Headers section and add two keys called APCA-API-KEY-ID and APCA-API-SECRET-KEY. Enter your key values in the VALUE column. - Hit the Send button
You should be able to see various account information from this API call. Now you’re ready to trade!
Fetching Snapshot for Multiple Stock Symbols
Let’s first create a raw request to fetch the snapshot for multiple stock symbols. The Snapshot API provides the latest trade, latest quote, minute bar, daily bar, and previous daily bar data for the given ticker symbols. For this example let’s use TSLA and AMZN. As we are interested in fetching the snapshot we append the following URL
/v2/stocks/snapshots
to the URL prefix: https://data.alpaca.markets/v2
As we are fetching the Snapshot for multiple symbols we set our required query parameter of symbols
which includes the desired stocks separated by commas. Our API Call is then the following
Testing our API call we see we receive the expected response of a snapshot response object containing the latest trade, latest quote, minute bar, daily bar, and previous daily bar data for each stock symbol.
Fetching Snapshot for One Stock Symbol
Let’s create a raw request to fetch the snapshot for one stock symbol. Like before, the Snapshot API provides the latest trade, latest quote, minute bar, daily bar, and previous daily bar data for the stock symbol requested. For this example let’s use AAPL. As we are interested in fetching the snapshot for single requested security we append the following URL
/v2/stocks/{symbol}/snapshot
to the URL prefix: https://data.alpaca.markets/v2
As we are fetching the Snapshot for a single stock symbol we set our required path parameter as the symbol for our desired stock. Our API Call is then the following
Testing our API call we see we receive the expected response of a snapshot response object containing the latest trade, latest quote, minute bar, daily bar, and previous daily bar data for the requested symbol.
Great! Now, moving forward here are some important points to remember:
- While the latest trade/quote endpoints are most frequently used in real-time, these endpoints are prefixed with the same URL one would use to access historical data with the Alpaca Market Data API v2:
https://data.alpaca.markets/v2
- When using the Snapshot API, users with the Free Plan will only be able to access the latest trade/quote/minute bar from IEX
- Users with either plan access the same historically aggregated daily and previous daily bar.
- Remember when fetching the snapshot for multiple stock symbols separate each symbol by a comma, a space will cause an Invalid Symbol Response
Thank you for using Alpaca. Here are a couple more links that you might be interested in.
You can also follow Alpaca and our weekly updates on our LinkedIn, Alpaca Community Slack, and @AlpacaHQ on Twitter!
Technology and services are offered by AlpacaDB, Inc. Brokerage services are provided by Alpaca Securities LLC, member FINRA/SIPC. Alpaca Securities LLC is a wholly-owned subsidiary of AlpacaDB, Inc.
Please do note that this is for illustration only and is not a recommendation or suggestion of this trade or this stock.