Deribit Rust Client
Type-safe, async Rust client for the Deribit WebSocket JSON‑RPC v2 API. Request/response types are generated at build time from the official API spec, and a single connection supports both RPC calls and streaming subscriptions.
✨ Features
- 🏗️ Build-time codegen from Deribit’s spec (production by default, optional Testnet)
- ⚡ Async WebSocket JSON‑RPC 2.0 over a single multiplexed connection
- 🦀 Strongly-typed requests, responses, channels and subscription notifications
- 📡 Simple subscriptions API for public and private channels
- 🔁 Concurrency-friendly: methods take
&self(nomut), and the client is shareable viaArc - 💓 Automatic heartbeat handling: responds to Deribit
test_requestinternally (no manual pings needed)
🚀 Quick Start
Add the crate and tokio to your Cargo.toml:
[]
= "0.1.2"
= { = "1", = ["rt-multi-thread", "macros"] }
= "0.3" # for StreamExt in subscription examples
▶️ Basic usage
Public call example: fetch server time.
use ;
async
🔐 Authentication + private methods
Authenticate using client credentials and fetch an account summary.
use ;
async
📡 Streaming subscriptions
Untyped variant: subscribe by channel string and receive a Stream of serde_json::Value.
use ;
use StreamExt;
async
Typed variant: use generated channel types and get a Stream of typed messages.
use ;
use StreamExt;
async
🧪 Testnet
- Connect with
Env::Testnet:
let client = connect.await?;
- Enable the feature to also generate Testnet types:
[]
= { = "0.1.2", = ["testnet"] }
When the testnet feature is enabled, production types live at the crate root (deribit_api::*), and Testnet‑generated types are available under deribit_api::testnet::*.
Note: Enable the testnet feature only if you need endpoints or fields that exist only on Testnet. If you don't need any Testnet‑specific features, you can connect to Env::Testnet while using the default production spec and all overlapping APIs will work as expected.
🧩 API model
- Each endpoint like
public/get_timemaps to a request struct namedPublicGetTimeRequest. - Send requests via
client.call(request).await. - Responses deserialize into generated structs/enums where possible, or
serde_json::Valuefor generic schemas. - Subscriptions expose generated channel structs (e.g.,
TradesInstrumentNameChannel) implementing theSubscriptiontrait. Useclient.subscribe(channel).await?for typed streams, orclient.subscribe_raw("...")for untyped.
Error type: all calls return Result<T, deribit_api::Error> (covers RPC, WebSocket, and JSON decode errors).
🧵 Low-level: call_raw
If you want to call a method by name with ad‑hoc JSON parameters, use call_raw. It returns a serde_json::Value.
Requires adding serde_json to your Cargo.toml:
[]
= "1"
use ;
use json;
async
🤝 Concurrency and sharing
The client is safe to share across tasks using std::sync::Arc and does not require mut. All methods take &self and internally multiplex over a single WebSocket connection.
use Arc;
use ;
use StreamExt;
async
🔧 Configuration
-
Default spec source: production
https://www.deribit.com/static/deribit_api_v2.json. -
Override the API spec used for codegen at build time in one of these ways:
- Enable the
bundled-specfeature to force using bundledderibit_api_v2.jsonfile:- Enabling
bundled-specfeature inCargo.toml:[] = { = "0.1.2", = ["bundled-spec"] } - Running tests using bundled spec:
cargo test --features bundled-spec
- Enabling
- Environment variable
DERIBIT_API_SPECpointing to a local file path or a URL.- Examples:
DERIBIT_API_SPEC=./deribit_api_v2.json cargo buildDERIBIT_API_SPEC=https://example.com/deribit_api_v2.json cargo build
- Examples:
- Enable the
-
Testnet codegen: enable
testnetto also generate Testnet types alongside production:- Enabling
testnetfeature inCargo.toml:[] = { = "0.1.2", = ["testnet"] } - Production types are at the crate root (
deribit_api::*); Testnet types live underderibit_api::testnet::*. - Only enable this if you need new Testnet endpoints/fields that are not available on production; otherwise you can use
Env::Testnetwith the default production spec.
- Enabling
-
The build script also sets
GENERATED_DERIBIT_CLIENT_PATH(env var) to the formatted, generated production client file path intarget/, which can help with debugging.
📚 Examples
This repo ships several runnable examples:
# Public calls
# Subscriptions
# Authentication + private endpoints
# Setting heartbeats
# Testnet (enables the feature and uses the Testnet endpoint)
# Low-level + untyped stream
# Concurrent RPC + subscription on one connection
🛠️ Development
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
⚠️ Disclaimer
This software is for educational and development purposes. Use at your own risk when trading with real funds.