Prerequisites
Install the required dependencies.Download the Protobuf Definitions
Load the Proto Definitions
Authentication
Streaming requests authenticate with either a Bearer access token or thex-api-key metadata header. This guide uses an API key.
Subscribe to Events
TheSubscribeEvents endpoint provides a server-streaming interface suitable for most applications.
Bidirectional Streaming
Backend and long-running services can useStreamEvents to dynamically subscribe and unsubscribe from accounts over an existing connection.
keepCase: false, request and response fields use camel case: tradingAccountId, environment, orderDelivery, and event.primed.eventType. environment is required on every subscribe and unsubscribe request: "BROKER_ENVIRONMENT_PAPER" or "BROKER_ENVIRONMENT_LIVE", matching the account’s environment. Set orderDelivery: "ORDER_DELIVERY_PRIMED" on the subscribe request to receive retained order snapshots first. Wait for the order primed event before treating that initial delivery as complete.
Terminal orders and zero-quantity positions expire from retained stream state after one hour. Rebuild a fresh snapshot set on reconnect so an old cache does not retain entries that no longer replay. See Rebuilding a cached view.
Unsubscribe
To stop receiving events for an account, send an unsubscribe request on the same stream.Handling Disconnects
Applications should automatically reconnect when a stream closes unexpectedly. After reconnecting:- Create a new stream.
- Authenticate using a Bearer access token or API key.
- Re-subscribe to required trading accounts.
- Resume event processing.
TypeScript
The same approach works in TypeScript. Many teams choose to generate strongly typed TypeScript definitions from the Anthid protobuf files during their build process rather than loading protobuf definitions dynamically at runtime.Related Resources
- Streaming Overview
- Python Guide
- Rust Guide
- Trading Accounts
- Orders
- Positions
- Ledger
- Intents