/v1 REST endpoint and for the /v1/stream WebSocket. It consists of two crates, published from the metastreams-sdk repository:
The API uses the same types, so they match its responses field for field.
Installation
The crates are not on crates.io yet. Add them as a git dependency pinned to a tag:Cargo.toml
rev instead of a tag. The minimum supported Rust version is 1.93. TLS uses rustls with the webpki root certificates, so you don’t install a crypto provider yourself.
Provider
MetastreamsProvider::new(key) creates a client for the production API, and MetastreamsProvider::new_with_base_url(key, url) creates one for another origin. The provider implements Clone, and its Debug output hides the key.
Each method sends one request, with a 30-second request timeout and a 10-second connect timeout. A method never retries.
METASTREAMS_API_KEY. The samples on this page read it from API_KEY, as the rest of these docs do.
Methods
When
identity_trades, wallet_balances or positions leaves chains at None, the SDK sends every chain it knows.
Pagination
A paginated response carrieshas_more and next_cursor. Copy next_cursor into the query to read the next page:
Session tokens
A backend creates a session token for a browser user withsession_token_create:
Streams
provider.connect() opens one WebSocket, which carries many subscriptions. The filter type selects the channel and the payload types:
TradeFilter, TransferFilter, CandleFilter and TokenFilter are in metastreams_sdk::types::filter. SpotTrendingFilter is in metastreams_sdk::types::trending, and the other filter types are in metastreams_sdk::types::stream. Each subscription is a futures::Stream of Events. An Event::Update carries one payload, and an Event::Exit means a token the subscription matched no longer matches. A channel without exits uses an empty exit type, so its Exit arm is match exit {}.
Subscription options
subscribe_throttled(&filter, Duration) sets throttleMs on a TokenFilter or CandleFilter, rounded up to whole milliseconds. Exits are never throttled. subscribe_candles(&CandleFilter, CandleOptions) also sets the candle metric, denomination and closed_only.
Subscription lifecycle
The stream sends no snapshot, except onspot.trending, whose first update is the full board. On other channels, read the REST endpoint after subscribe returns, as Initial snapshot describes. id() returns the subscription ID the server assigned, and dropping a subscription unsubscribes it.
A subscription ends with one Err(MetastreamsError::Closed(reason)), followed by None. A payload that fails to decode yields Err(MetastreamsError::FrameDecode { .. }), and the subscription continues.
connection.closed() resolves to the CloseReason when the socket closes. connection.close() closes the socket normally, and each subscription then ends with CloseReason::Client.
The SDK sends a ping every 15 seconds and treats the socket as lost after 45 seconds without a frame. Each subscription buffers up to 1,024 unread events. When the buffer is full, the SDK closes that subscription with CloseReason::Lagged and leaves the others open.
Close reasons
Reconnection
The SDK does not reconnect. Subscriptions end with their connection, and missed updates are not replayed. After a transient close, connect, subscribe again and re-read the snapshot from REST. Reconnect at once afterServerRestarting, and back off after other transient reasons. After KeyRevoked or CreditsExhausted, stop. See Reconnection.
Errors
Every method returnsmetastreams_sdk::Result<T>, whose error is MetastreamsError:
error.code is an ErrorCode from metastreams_sdk::types::error. A code that this SDK version does not know becomes ErrorCode::Unknown.
is_transient() reports whether the same call can succeed later: a transport failure, a 429, a 5xx, or a transient close. The example below retries transient errors and waits for Retry-After on a 429:
time feature of tokio.