longport provides an easy-to-use interface for invoking LongPort OpenAPI.
| Context | Description |
|---|---|
QuoteContext |
Real-time quotes, candlesticks, options, warrants, watchlists, push subscriptions |
TradeContext |
Orders, positions, account balance, executions, cash flow |
AssetContext |
Account statement download |
ContentContext |
News, community topics |
FundamentalContext |
Financial reports, analyst ratings, dividends, valuation, company overview, shareholders |
MarketContext |
Market status, broker holdings, A/H premium, trade statistics, anomaly alerts, index constituents |
CalendarContext |
Financial calendar (earnings, dividends, splits, IPOs, macro data, market closures) |
PortfolioContext |
Exchange rates, portfolio P&L analysis |
AlertContext |
Price alert management (add/enable/disable/delete) |
DCAContext |
Dollar-cost averaging plan management |
SharelistContext |
Community sharelist management |
Runnable examples live in examples/nodejs/:
examples/nodejs/account_asset.jsexamples/nodejs/http_client.jsexamples/nodejs/subscribe_candlesticks.jsexamples/nodejs/subscribe_quote.jsexamples/nodejs/submit_order.jsexamples/nodejs/today_orders.jsInstall LongPort OpenAPI SDK
npm install longport
LongPort OpenAPI supports two authentication methods:
OAuth 2.0 is the modern authentication method that uses Bearer tokens without requiring HMAC signatures.
Step 1: Register OAuth Client
First, register an OAuth client to get 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"]
}
Save the client_id for use in your application.
Step 2: Build an OAuth client and create Config
OAuth.build() loads a cached token from
~/.longport/openapi/tokens/<client_id>
(%USERPROFILE%\.longport\openapi\tokens\<client_id> on Windows) if one
exists and is still valid, or starts the browser authorization flow
automatically. The token is persisted to the same path after a successful
authorization or refresh.
const { OAuth, Config } = require('longport');
async function main() {
const oauth = await OAuth.build(
"your-client-id",
(_, url) => console.log("Open this URL to authorize: " + url)
);
const config = Config.fromOAuth(oauth);
// Use config to create contexts...
}
main();
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"
| Name | Description |
|---|---|
| LONGPORT_LANGUAGE | Language identifier, zh-CN, zh-HK or en (Default: en) |
| LONGPORT_HTTP_URL | HTTP endpoint url (Default: https://openapi.longportapp.com) |
| LONGPORT_QUOTE_WS_URL | Quote websocket endpoint url (Default: wss://openapi-quote.longportapp.com/v2) |
| LONGPORT_TRADE_WS_URL | Trade websocket endpoint url (Default: wss://openapi-trade.longportapp.com/v2) |
| LONGPORT_ENABLE_OVERNIGHT | Enable overnight quote, true or false (Default: false) |
| LONGPORT_PUSH_CANDLESTICK_MODE | realtime or confirmed (Default: realtime) |
| LONGPORT_QUOTE_TRANSPORT | Transport for quote pull APIs, ws or http (Default: ws) |
| LONGPORT_PRINT_QUOTE_PACKAGES | Print quote packages when connected, true or false (Default: true) |
| LONGPORT_LOG_PATH | Set the path of the log files (Default: no logs) |
| LONGPORT_PAPERTRADING | Enable paper trading mode, true or false (Default: false). See Paper Trading. |
Then create a config from the environment:
const { Config } = require('longport');
const config = Config.fromApikeyEnv();
const { OAuth, Config, QuoteContext } = require('longport');
async function main() {
const oauth = await OAuth.build(
"your-client-id",
(_, url) => console.log("Open this URL to authorize: " + url)
);
const config = Config.fromOAuth(oauth);
const ctx = QuoteContext.new(config);
const resp = await ctx.quote(["700.HK", "AAPL.US", "TSLA.US", "NFLX.US"]);
for (let obj of resp) {
console.log(obj.toString());
}
}
main();
const { OAuth, Config, QuoteContext, SubType } = require('longport');
async function main() {
const oauth = await OAuth.build(
"your-client-id",
(_, url) => console.log("Open this URL to authorize: " + url)
);
const config = Config.fromOAuth(oauth);
const ctx = QuoteContext.new(config);
ctx.setOnQuote((_, event) => console.log(event.toString()));
await ctx.subscribe(
["700.HK", "AAPL.US", "TSLA.US", "NFLX.US"],
[SubType.Quote],
);
}
main();
const {
OAuth,
Config,
TradeContext,
Decimal,
OrderSide,
TimeInForceType,
OrderType,
} = require('longport');
async function main() {
const oauth = await OAuth.build(
"your-client-id",
(_, url) => console.log("Open this URL to authorize: " + url)
);
const config = Config.fromOAuth(oauth);
const ctx = TradeContext.new(config);
const resp = await ctx.submitOrder({
symbol: "700.HK",
orderType: OrderType.LO,
side: OrderSide.Buy,
timeInForce: TimeInForceType.Day,
submittedPrice: new Decimal("50"),
submittedQuantity: new Decimal(200),
});
console.log(resp.toString());
}
main();
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:
const { Config } = require('longport');
const config = Config.fromApikey(
process.env.LONGPORT_APP_KEY,
process.env.LONGPORT_APP_SECRET,
process.env.LONGPORT_ACCESS_TOKEN,
{ enablePapertrading: true },
);
setx requires a new terminal; use set for the current cmd.exe session.LONGPORT_LOG_PATH to enable SDK logs.Licensed under either of