Longbridge OpenAPI SDK for Rust
longbridge provides an easy-to-use interface for invoking Longbridge OpenAPI.
Documentation
- SDK docs: https://longbridge.github.io/openapi/rust/longbridge/index.html
- crates.io: https://crates.io/crates/longbridge
- Longbridge OpenAPI: https://open.longbridge.com/en/
Examples
Runnable examples live in examples/rust/:
examples/rust/account_asset/src/main.rsexamples/rust/http_client/src/main.rsexamples/rust/subscribe_quote/src/main.rsexamples/rust/subscribe_candlesticks/src/main.rsexamples/rust/submit_order/src/main.rsexamples/rust/today_orders/src/main.rs
Quickstart
Add dependencies to Cargo.toml
[]
= "4.0.0"
Authentication
Longbridge OpenAPI supports two authentication methods:
1. OAuth 2.0 (Recommended)
OAuth 2.0 uses Bearer tokens without requiring HMAC signatures. The token is
persisted automatically at ~/.longbridge/openapi/tokens/<client_id>
(%USERPROFILE%\.longbridge\openapi\tokens\<client_id> on Windows) and
refreshed transparently on every request.
Step 1: Register an OAuth Client
Register an OAuth client to obtain your client_id:
bash / macOS / Linux
PowerShell (Windows)
Invoke-RestMethod -Method Post -Uri https://openapi.longbridge.com/oauth2/register `
-ContentType "application/json" `
-Body '{
"client_name": "My Application",
"redirect_uris": ["http://localhost:60355/callback"],
"grant_types": ["authorization_code", "refresh_token"]
}'
Response:
Step 2: Build an OAuth handle and create Config
use Arc;
use ;
async
Benefits:
- No shared secret required
- No per-request signature calculation
- Token lifecycle (load, refresh, persist) managed automatically
2. Legacy API Key (Environment Variables)
For backward compatibility you can use the traditional API key method.
Setting environment variables (macOS/Linux)
Setting environment variables (Windows)
setx LONGBRIDGE_APP_KEY "App Key get from user center"
setx LONGBRIDGE_APP_SECRET "App Secret get from user center"
setx LONGBRIDGE_ACCESS_TOKEN "Access Token get from user center"
Other environment variables
| Name | Description |
|---|---|
| LONGBRIDGE_LANGUAGE | Language identifier, zh-CN, zh-HK or en (Default: en) |
| LONGBRIDGE_HTTP_URL | HTTP endpoint url (Default: https://openapi.longbridge.com) |
| LONGBRIDGE_QUOTE_WS_URL | Quote websocket endpoint url (Default: wss://openapi-quote.longbridge.com/v2) |
| LONGBRIDGE_TRADE_WS_URL | Trade websocket endpoint url (Default: wss://openapi-trade.longbridge.com/v2) |
| LONGBRIDGE_ENABLE_OVERNIGHT | Enable overnight quote, true or false (Default: false) |
| LONGBRIDGE_PUSH_CANDLESTICK_MODE | realtime or confirmed (Default: realtime) |
| LONGBRIDGE_PRINT_QUOTE_PACKAGES | Print quote packages when connected, true or false (Default: true) |
| LONGBRIDGE_LOG_PATH | Set the path of the log files (Default: no logs) |
Quote API (Get basic information of securities)
Using OAuth 2.0 (Recommended):
use Arc;
use ;
async
Using legacy API key (environment variables):
use Arc;
use ;
async
Quote API (Subscribe quotes)
use Arc;
use ;
async
Trade API (Submit order)
use Arc;
use ;
async
Troubleshooting
- Windows
setxrequires a new terminal; usesetfor the currentcmd.exesession. - If you don't see push events, keep the process alive (receiver loop /
sleep). - For debugging, set
LONGBRIDGE_LOG_PATHto enable SDK logs.
Crate features
To avoid compiling unused dependencies, longbridge gates certain features, all of which are disabled by default:
| Feature | Description |
|---|---|
| blocking | Provides the blocking client API. |
License
Licensed under either of
- Apache License, Version 2.0,(LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.