Real-time Crypto Data
Deprecation of
v1beta1
andv1beta2
The
v1beta1
andv1beta2
endpoints were marked deprecated, and will be removed shortly. Please use the latestv1beta3
endpoints.Since Alpaca now executes your crypto orders in its own exchange, the
v1beta3
crypto market data endpoints no longer distribute data from other providers, but from Alpaca itself.The orderbooks and quotes are the actual orderbooks/top of orderbooks of the Alpaca exchange. The trades and the bars generated from them include all the trades executed on the Alpaca exchange. However, for better data quality and in order to provide historical data before the Alpaca exchange started, they are enriched by other providers as well.
Moreover, you may notice that the URL now includes a required path parameter: the crypto feed. So
/v1beta1/crypto
became/v1beta3/crypto/us
. We’re planning to extend our offering with other feeds in the future, but for now,us
is the only possible option there.
Crypto Data API provides websocket streaming for trades, quotes, orderbooks, minute bars and daily bars. This helps receive the most up to date market information that could help your trading strategy to act upon certain market movements.
Once a connection is established and you have successfully authenticated yourself you can subscribe to any event for a particular symbol or multiple symbols.
Advanced Websockets Tutorial
Check out our tutorial Advanced Live Websocket Crypto Data Streams in Python for some tips on handling live crypto data stream in Python.
Connection
URL
To access real-time data use this URL:
wss://stream.data.alpaca.markets/v1beta3/crypto/us
Message Format
Every message you receive from the server will be in the format:
[{"T": "{message_type}", {contents}},...]
Control messages (i.e. where "T"
is error
, success
or subscription
) always arrive in arrays of size one to make their processing easier.
Data points however may arrive in arrays that have a length that is greater than one. This is to facilitate clients whose connection is not fast enough to handle data points sent one by one. Our server buffers the outgoing messages but slow clients may get disconnected if their buffer becomes full.
Encoding and Compression
Messages over the websocket are encoded as clear text.
To reduce bandwidth requirements we have implemented compression as per RFC-7692. Our SDKs handle this for you so in most cases you won’t have to implement anything yourself.
Communication Flow
The communication can be thought of as two separate phases: establishment and receiving data.
Establishment
To establish the connection first you will need to connect to our server using the URL above.
Upon successfully connecting, you will receive the welcome message:
[{"T":"success","msg":"connected"}]
Now you will need to authenticate yourself using your credentials by sending the following message:
{"action": "auth", "key": "{APCA-API-KEY-ID}", "secret": "{APCA-API-SECRET-KEY}"}
Please note that each account can have up to one concurrent websocket connection. Subsequent attempts to connect are rejected.
If you provided correct credentials you will receive another success
message:
[{"T":"success","msg":"authenticated"}]
Receiving Data
Congratulations, you are ready to receive real-time crypto market data!
You can send one or more subscription messages (described below) and after confirmation you will receive the corresponding market data.
At any time you can subscribe to or unsubscribe from symbols. Please note that due to the internal buffering mentioned above for a short while you may receive data points for symbols you have recently unsubscribed from.
Client To Server
Authentication
After connecting you will have to authenticate as described below.
{"action":"auth","key":"PK********\*\*********","secret":"****************\*\*\*****************"}
Subscribe
You can subscribe to trades
, quotes
, bars
and orderbooks
of a particular crypto pair (or * for every pair with relevant limits still applicable). A subscribe
message should contain what subscription you want to add to your current subscriptions in your session so you don’t have to send what you’re already subscribed to.
{"action":"subscribe","trades":["BTC/USD"],"quotes":["LTC/USD","ETH/USD"],"bars":["BCH/USD"]}
You can also omit either one of trades
, quotes
, bars
or orderbooks
if you don’t want to subscribe to any pairs in that category but be sure to include at least one of the three.
Unsubscribe
Much like subscribe
you can also send an unsubscribe
message that subtracts the list of subscriptions specified from your current set of subscriptions.
{"action":"unsubscribe","trades":["BTC/USD"],"quotes":["ETH/USD"],"bars":[],"orderbooks":[]}
Server to Client
Control Messages
You may receive the following control messages during your session.
Message | Description |
---|---|
[{"T":"success","msg":"connected"}] | This means you have successfully connected to our server. |
[{"T":"success","msg":"authenticated"}] | This means you have successfully authenticated. |
Errors
You may receive an error during your session. Below are all the errors you may run into.
Error Message | Description |
---|---|
[{"T":"error","code":400,"msg":"invalid syntax"}] | The message you sent to the server did not follow the specification. |
[{"T":"error","code":401,"msg":"not authenticated" }] | You have attempted to subscribe or unsubscribe before authentication. |
[{"T":"error","code":402,"msg":"auth failed"}] | You have provided invalid authentication credentials. |
[{"T":"error","code":403,"msg":"already authenticated"}] | You have already successfully authenticated during your current session. |
[{"T":"error","code":404,"msg":"auth timeout"}] | You failed to successfully authenticate after connecting. You have a few seconds to authenticate after connecting. |
[{"T":"error","code":405,"msg":"symbol limit exceeded"}] | The symbol subscription request you sent would put you over the limit set by your subscription package. If this happens your symbol subscriptions are the same as they were before you sent the request that failed. |
[{"T":"error","code":406,"msg":"connection limit exceeded"}] | You already have an ongoing authenticated session. |
[{"T":"error","code":407,"msg":"slow client"}] | You may receive this if you are too slow to process the messages sent by the server. Please note that this is not guaranteed to arrive before you are disconnected to avoid keeping slow connections active forever. |
[{"T":"error","code":500,"msg":"internal error"}] | An unexpected error occurred on our end. Please let us know if this happens. |
Subscription Confirmation
After subscribing or unsubscribing you will receive a message that describes your current list of subscriptions.
[{"T":"subscription","trades":["BTC/USD"],"quotes":["ETH/BTC"],"bars":[], "orderbooks": ["UNI/USDT"]}]
You will always receive your entire list of subscriptions, as illustrated by the sample communication excerpt below:
> {"action": "subscribe", "trades": ["BTC/USD"], "quotes": ["BTC/USD", "LTC/USD"], "bars": ["*"], "orderbooks": ["ETH/BTC"]}
< [{"T":"subscription","trades":["BTC/USD"],"quotes":["BTC/USD","LTC/USD"],"orderbooks":["ETH/BTC"],"bars":["*"],"updatedBars":[],"dailyBars":[]}]
< [{"T":"t","S":"BTC/USD","p":26711.38,"s":0.00672,"t":"2023-03-17T12:32:12.116Z","i":6403852,"tks":"S"}]
< [{"T":"t","S":"BTC/USD","p":26711.38,"s":0.00427,"t":"2023-03-17T12:32:12.12Z","i":6403853,"tks":"S"}]
< [{"T":"t","S":"BTC/USD","p":26711.38,"s":0.01328,"t":"2023-03-17T12:32:12.121Z","i":6403854,"tks":"S"}]
< [{"T":"q","S":"BTC/USD","bp":26677.1564929,"bs":0.52365586,"ap":26730.8798,"as":0.25825915,"t":"2023-03-17T12:32:12.713306656Z"}]
> {"action": "unsubscribe", "bars": ["*"]}
> [{"T":"subscription","trades":["BTC/USD"],"quotes":["BTC/USD","LTC/USD"],"orderbooks":["ETH/BTC"],"bars":[],"updatedBars":[],"dailyBars":[]}]
Example
$ wscat -c wss://stream.data.alpaca.markets/v1beta3/crypto/us
connected (press CTRL+C to quit)
< [{"T":"success","msg":"connected"}]
> {"action": "auth", "key": "**\***", "secret": "**\***"}
< [{"T":"success","msg":"authenticated"}]
> {"action": "subscribe", "bars": ["BTC/USD"]}
< [{"T":"subscription","trades":[],"quotes":[],"orderbooks":[],"bars":["BTC/USD"],"updatedBars":[],"dailyBars":[]}]
< [{"T":"b","S":"BTC/USD","o":26675.04,"h":26695.36,"l":26668.79,"c":26688.7,"v":3.227759152,"t":"2023-03-17T12:28:00Z","n":93,"vw":26679.5912436798}]
< [{"T":"b","S":"BTC/USD","o":26687.9,"h":26692.91,"l":26628.55,"c":26651.39,"v":11.568622108,"t":"2023-03-17T12:29:00Z","n":197,"vw":26651.7679765663}]
Updated 14 days ago