Skip to main content

Crate longport

Crate longport 

Source
Expand description

§LongPort OpenAPI SDK for Rust

longport provides an easy-to-use interface for invoking LongPort OpenAPI.

§Context Types

ContextDescription
QuoteContextReal-time quotes, candlesticks, options, warrants, watchlists, push subscriptions
TradeContextOrders, positions, account balance, executions, cash flow
AssetContextAccount statement download
ContentContextNews, community topics
FundamentalContextFinancial reports, analyst ratings, dividends, valuation, company overview, shareholders
MarketContextMarket status, broker holdings, A/H premium, trade statistics, anomaly alerts, index constituents
CalendarContextFinancial calendar (earnings, dividends, splits, IPOs, macro data, market closures)
PortfolioContextExchange rates, portfolio P&L analysis
AlertContextPrice alert management (add/enable/disable/delete)
DCAContextDollar-cost averaging plan management
SharelistContextCommunity sharelist management

§Documentation

  • SDK docs: https://longport.github.io/openapi/rust/longport/index.html
  • crates.io: https://crates.io/crates/longport
  • LongPort OpenAPI: https://open.longportapp.com/en/

§Examples

Runnable examples live in examples/rust/:

  • examples/rust/account_asset/src/main.rs
  • examples/rust/http_client/src/main.rs
  • examples/rust/subscribe_quote/src/main.rs
  • examples/rust/subscribe_candlesticks/src/main.rs
  • examples/rust/submit_order/src/main.rs
  • examples/rust/today_orders/src/main.rs

§Quickstart

Add dependencies to Cargo.toml

[dependencies]
longport = "4.0.0"

§Authentication

LongPort OpenAPI supports two authentication methods:

OAuth 2.0 uses Bearer tokens without requiring HMAC signatures. The token is persisted automatically at ~/.longport/openapi/tokens/<client_id> (%USERPROFILE%\.longport\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

curl -X POST https://openapi.longportapp.com/oauth2/register \
  -H "Content-Type: application/json" \
  -d '{
    "client_name": "My Application",
    "redirect_uris": ["http://localhost:60355/callback"],
    "grant_types": ["authorization_code", "refresh_token"]
  }'

PowerShell (Windows)

Invoke-RestMethod -Method Post -Uri https://openapi.longportapp.com/oauth2/register `
  -ContentType "application/json" `
  -Body '{
    "client_name": "My Application",
    "redirect_uris": ["http://localhost:60355/callback"],
    "grant_types": ["authorization_code", "refresh_token"]
  }'

Response:

{
  "client_id": "your-client-id-here",
  "client_secret": null,
  "client_name": "My Application",
  "redirect_uris": ["http://localhost:60355/callback"]
}

Step 2: Build an OAuth handle and create Config

use std::sync::Arc;
use longport::{Config, oauth::OAuthBuilder};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Loads an existing token from ~/.longport/openapi/tokens/<client_id>.
    // If none exists or it is expired, opens the browser authorization flow.
    // Token refresh is handled automatically on every subsequent request.
    let oauth = OAuthBuilder::new("your-client-id")
        // .callback_port(8080)  // optional, default 60355
        .build(|url| println!("Open this URL to authorize: {url}"))
        .await?;

    let config = Arc::new(Config::from_oauth(oauth));

    // Use config to create contexts...
    Ok(())
}

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)

export LONGPORT_APP_KEY="App Key get from user center"
export LONGPORT_APP_SECRET="App Secret get from user center"
export LONGPORT_ACCESS_TOKEN="Access Token get from user center"

Setting environment variables (Windows)

setx LONGPORT_APP_KEY "App Key get from user center"
setx LONGPORT_APP_SECRET "App Secret get from user center"
setx LONGPORT_ACCESS_TOKEN "Access Token get from user center"

§Other environment variables

NameDescription
LONGPORT_LANGUAGELanguage identifier, zh-CN, zh-HK or en (Default: en)
LONGPORT_HTTP_URLHTTP endpoint url (Default: https://openapi.longportapp.com)
LONGPORT_QUOTE_WS_URLQuote websocket endpoint url (Default: wss://openapi-quote.longportapp.com/v2)
LONGPORT_TRADE_WS_URLTrade websocket endpoint url (Default: wss://openapi-trade.longportapp.com/v2)
LONGPORT_ENABLE_OVERNIGHTEnable overnight quote, true or false (Default: false)
LONGPORT_PUSH_CANDLESTICK_MODErealtime or confirmed (Default: realtime)
LONGPORT_QUOTE_TRANSPORTTransport for quote pull APIs, ws or http (Default: ws)
LONGPORT_PRINT_QUOTE_PACKAGESPrint quote packages when connected, true or false (Default: true)
LONGPORT_LOG_PATHSet the path of the log files (Default: no logs)
LONGPORT_PAPERTRADINGEnable paper trading mode, true or false (Default: false). See Paper Trading.

§Paper Trading

If your account is a paper trading (simulation) account and you want to place orders, you must explicitly enable paper trading mode. When enabled, all API calls target the paper trading environment and the server validates the token — if the token belongs to a real-money account the server returns an error.

By default this option is off: the server imposes no restrictions and accepts requests from both paper trading and real-money accounts.

Via environment variable (applies automatically to all constructor methods):

export LONGPORT_PAPERTRADING=true   # macOS / Linux
$env:LONGPORT_PAPERTRADING = "true"   # Windows PowerShell

Programmatically:

use std::sync::Arc;
use longport::Config;

let config = Arc::new(
    Config::from_apikey("app-key", "app-secret", "access-token")
        .enable_papertrading()
);

§Quote API (Get basic information of securities)

Using OAuth 2.0 (Recommended):

use std::sync::Arc;
use longport::{Config, QuoteContext, oauth::OAuthBuilder};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let oauth = OAuthBuilder::new("your-client-id")
        .build(|url| println!("Open this URL to authorize: {url}"))
        .await?;
    let config = Arc::new(Config::from_oauth(oauth));

    // Create a context for quote APIs
    let (ctx, _) = QuoteContext::new(config);

    // Get basic information of securities
    let resp = ctx
        .quote(["700.HK", "AAPL.US", "TSLA.US", "NFLX.US"])
        .await?;
    println!("{:?}", resp);

    Ok(())
}

Using legacy API key (environment variables):

use std::sync::Arc;
use longport::{Config, QuoteContext};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load configuration from environment variables
    let config = Arc::new(Config::from_apikey_env()?);

    // Create a context for quote APIs
    let (ctx, _) = QuoteContext::new(config.clone());

    // Get basic information of securities
    let resp = ctx
        .quote(["700.HK", "AAPL.US", "TSLA.US", "NFLX.US"])
        .await?;
    println!("{:?}", resp);

    Ok(())
}

§Quote API (Subscribe quotes)

use std::sync::Arc;
use longport::{quote::SubFlags, Config, QuoteContext};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load configuration from environment variables
    let config = Arc::new(Config::from_apikey_env()?);

    // Create a context for quote APIs
    let (ctx, mut receiver) = QuoteContext::new(config);

    // Subscribe
    ctx.subscribe(["700.HK"], SubFlags::QUOTE).await?;

    // Receive push events
    while let Some(event) = receiver.recv().await {
        println!("{:?}", event);
    }

    Ok(())
}

§Trade API (Submit order)

use std::sync::Arc;
use longport::{
    decimal,
    trade::{OrderSide, OrderType, SubmitOrderOptions, TimeInForceType},
    Config, TradeContext,
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load configuration from environment variables
    let config = Arc::new(Config::from_apikey_env()?);

    // Create a context for trade APIs
    let (ctx, _) = TradeContext::new(config);

    // Submit order
    let opts = SubmitOrderOptions::new(
        "700.HK",
        OrderType::LO,
        OrderSide::Buy,
        decimal!(500),
        TimeInForceType::Day,
    )
    .submitted_price(decimal!(50i32))
    .remark("Hello from Rust SDK".to_string());

    let resp = ctx.submit_order(opts).await?;
    println!("{:?}", resp);

    Ok(())
}

§Troubleshooting

  • Windows setx requires a new terminal; use set for the current cmd.exe session.
  • If you don’t see push events, keep the process alive (receiver loop / sleep).
  • For debugging, set LONGPORT_LOG_PATH to enable SDK logs.

§Crate features

To avoid compiling unused dependencies, longport gates certain features, all of which are disabled by default:

FeatureDescription
blockingProvides the blocking client API.

§License

Licensed under either of

Re-exports§

pub use agent::AgentContext;
pub use alert::AlertContext;
pub use asset::AssetContext;
pub use calendar::CalendarContext;
pub use content::ContentContext;
pub use dca::DCAContext;
pub use fund::FundContext;
pub use fundamental::FundamentalContext;
pub use fundamental::types::FinancialStatementKind;
pub use fundamental::types::IndustryRankIndicator;
pub use fundamental::types::IndustryRankSortType;
pub use fundamental::types::USCompanyDividends;
pub use fundamental::types::USCompanyOverview;
pub use fundamental::types::USDividendItem;
pub use fundamental::types::USETFDividendInfo;
pub use fundamental::types::USETFFile;
pub use fundamental::types::USETFFilesResponse;
pub use fundamental::types::USFinancialStatement;
pub use fundamental::types::USRankTag;
pub use fundamental::types::USValuationMetric;
pub use fundamental::types::USValuationOverview;
pub use grid::GridContext;
pub use market::MarketContext;
pub use portfolio::PortfolioContext;
pub use quote::QuoteContext;
pub use quote::USCryptoOverview;
pub use screener::ScreenerContext;
pub use sharelist::SharelistContext;
pub use signal::SignalContext;
pub use trade::GetUSAvailableHoldingOptions;
pub use trade::GetUSHistoryOrders;
pub use trade::GetUSRealizedPLOptions;
pub use trade::GetUSTransactionHistoryOptions;
pub use trade::QueryUSOrdersOptions;
pub use trade::QueryUSOrdersResponse;
pub use trade::TradeContext;
pub use trade::USAssetOverview;
pub use trade::USAvailableHolding;
pub use trade::USBuyLimitInfo;
pub use trade::USCashAndBuyPower;
pub use trade::USCashDetail;
pub use trade::USCashEntry;
pub use trade::USCryptoEntry;
pub use trade::USExchangeRate;
pub use trade::USExchangeRates;
pub use trade::USMarginStatus;
pub use trade::USOrderDetailResponse;
pub use trade::USOrderHistory;
pub use trade::USRealizedPL;
pub use trade::USRealizedPLEntry;
pub use trade::USRealizedPLMetric;
pub use trade::USTransaction;
pub use trade::USTransactionHistory;
pub use longport_oauth as oauth;
pub use longport_httpcli as httpclient;
pub use longport_wscli as wsclient;

Modules§

agent
AI Agent conversation types and context
alert
Price alert types and context
asset
Asset related types
calendar
Financial calendar types and context
content
Content related types
dca
DCA (dollar-cost averaging) types and context
fund
Fund (mutual fund) channel types and context.
fundamental
Fundamental data types and context
grid
Grid trading types and context
market
Market data types and context
portfolio
Portfolio analytics types and context
quote
Quote related types
runtime
Global tokio runtime shared across all contexts and language bindings.
screener
Stock screener — strategies, search, and indicators
sharelist
Community sharelist types and context
signal
Strategy signals and the catalyst facts behind them
trade
Trade related types

Macros§

decimal
A macro to construct decimal.

Structs§

Config
Configuration options for LongPort SDK
Decimal
Decimal represents a 128 bit representation of a fixed-precision decimal number. The finite set of values of type Decimal are of the form m / 10e, where m is an integer such that -296 < m < 296, and e is an integer between 0 and 28 inclusive.

Enums§

DcRegion
Data center region used for API gateway routing.
Error
LongPort OpenAPI SDK error type
Language
Language identifier
Market
Market
PushCandlestickMode
Push mode for candlestick
QuoteTransport
Transport used by the QuoteContext pull (request/response) APIs.
SimpleError
Simple error type
SimpleErrorKind
Simple error kind

Constants§

DC_REGION_HEADER
HTTP and WebSocket header that selects the data center serving a request.

Type Aliases§

Result
LongPort OpenAPI SDK result type