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

How to Use the Snapshot API in Alpaca Market Data API v2
Snapshot API

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:

Welcome
This page will help you get started with Alpaca Docs. You’ll be up and running in a jiffy!

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!

Account Authorization 

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

https://data.alpaca.markets/v2/stocks/snapshots?symbols=TSLA,AMZN
API Call


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.

{
    "TSLA": {
        "latestTrade": {
            "t": "2021-05-13T13:50:02.487676337Z",
            "x": "V",
            "p": 589.11,
            "s": 100,
            "c": [
                "@"
            ],
            "i": 2895,
            "z": "C"
        },
        "latestQuote": {
            "t": "2021-05-13T13:50:02.487676337Z",
            "ax": "V",
            "ap": 609.5,
            "as": 1,
            "bx": "V",
            "bp": 583.03,
            "bs": 1,
            "c": [
                "R"
            ]
        },
        "minuteBar": {
            "t": "2021-05-13T13:49:00Z",
            "o": 589.31,
            "h": 589.66,
            "l": 587.85,
            "c": 588.44,
            "v": 4841
        },
        "dailyBar": {
            "t": "2021-05-13T04:00:00Z",
            "o": 601.88,
            "h": 605.98,
            "l": 587.85,
            "c": 588.44,
            "v": 114216
        },
        "prevDailyBar": {
            "t": "2021-05-12T04:00:00Z",
            "o": 602.45,
            "h": 619.98,
            "l": 586.84,
            "c": 590.055,
            "v": 774655
        }
    },
    "AMZN": {
        "latestTrade": {
            "t": "2021-05-13T13:48:48.356588707Z",
            "x": "V",
            "p": 3163.17,
            "s": 100,
            "c": [
                "@"
            ],
            "i": 849,
            "z": "C"
        },
        "latestQuote": {
            "t": "2021-05-13T13:50:01.037327384Z",
            "ax": "V",
            "ap": 0,
            "as": 0,
            "bx": "V",
            "bp": 0,
            "bs": 0,
            "c": [
                "R"
            ]
        },
        "minuteBar": {
            "t": "2021-05-13T13:48:00Z",
            "o": 3163.17,
            "h": 3163.17,
            "l": 3163.17,
            "c": 3163.17,
            "v": 425
        },
        "dailyBar": {
            "t": "2021-05-13T04:00:00Z",
            "o": 3188.88,
            "h": 3202.195,
            "l": 3163.17,
            "c": 3163.17,
            "v": 17580
        },
        "prevDailyBar": {
            "t": "2021-05-12T04:00:00Z",
            "o": 3183.505,
            "h": 3205.19,
            "l": 3133.6,
            "c": 3155,
            "v": 228729
        }
    }
}
Successful Response Object 


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

https://data.alpaca.markets/v2/stocks/AAPL/snapshot
API Call


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.

{
    "symbol": "AAPL",
    "latestTrade": {
        "t": "2021-05-13T13:58:58.397809861Z",
        "x": "V",
        "p": 125.28,
        "s": 1125,
        "c": [
            "@"
        ],
        "i": 2739,
        "z": "C"
    },
    "latestQuote": {
        "t": "2021-05-13T13:59:01.533680985Z",
        "ax": "V",
        "ap": 125.3,
        "as": 2,
        "bx": "V",
        "bp": 125.27,
        "bs": 5,
        "c": [
            "R"
        ]
    },
    "minuteBar": {
        "t": "2021-05-13T13:58:00Z",
        "o": 125.33,
        "h": 125.33,
        "l": 125.28,
        "c": 125.28,
        "v": 2405
    },
    "dailyBar": {
        "t": "2021-05-13T04:00:00Z",
        "o": 124.615,
        "h": 125.81,
        "l": 124.43,
        "c": 125.28,
        "v": 334018
    },
    "prevDailyBar": {
        "t": "2021-05-12T04:00:00Z",
        "o": 123.36,
        "h": 124.64,
        "l": 122.25,
        "c": 122.71,
        "v": 1591428
    }
}
Successful Response Object


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



Alpaca API

Thank you for using Alpaca. Here are a couple more links that you might be interested in.

Alpaca GitHub Repository

Alpaca
Alpaca builds an API for free stock trading. Alpaca has 41 repositories available. Follow their code on GitHub.

Alpaca Blog

Alpaca Blog | API for Stock Trading
Alpaca Blog for product updates, new integration, and more. Alpaca’s commission-free trading API is built for algorithmic trading and building apps

Alpaca Forum

Alpaca Community Forum
Alpaca user forum for FAQ, feature requests, many discussions around Alpaca stock trading API

Alpaca API documentation

Welcome
This page will help you get started with Alpaca Docs. You’ll be up and running in a jiffy!

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.