longbridge provides an easy-to-use interface for invoking Longbridge OpenAPI.
- SDK docs: https://longbridge.github.io/openapi/java/index.html
- Longbridge OpenAPI: https://open.longbridge.com/en/
Runnable examples live in examples/java/:
examples/java/account_asset/src/main/java/main.javaexamples/java/history_candlesticks/src/main/java/Main.javaexamples/java/subscribe_quote/src/main/java/Main.javaexamples/java/submit_order/src/main/java/Main.javaexamples/java/today_orders/src/main/java/main.java
Install Longbridge OpenAPI SDK
Add io.github.longbridge:openapi-sdk to pom.xml
<dependencies>
<dependency>
<groupId>io.github.longbridge</groupId>
<artifactId>openapi-sdk</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>Longbridge 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.longbridgeapp.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.longbridgeapp.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 OAuth client and create a Config
OAuthBuilder.build loads a cached token from
~/.longbridge-openapi/tokens/<client_id> (%USERPROFILE%\.longbridge-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. The resulting OAuth handle is passed directly to
Config.fromOAuth.
import com.longbridge.*;
class Main {
public static void main(String[] args) throws Exception {
try (OAuth oauth = new OAuthBuilder("your-client-id")
.build(url -> System.out.println("Open this URL to authorize: " + url))
.get();
Config config = Config.fromOAuth(oauth)) {
// Use config to create contexts...
}
}
}Setting environment variables(MacOS/Linux)
export LONGBRIDGE_APP_KEY="App Key get from user center"
export LONGBRIDGE_APP_SECRET="App Secret get from user center"
export LONGBRIDGE_ACCESS_TOKEN="Access Token get from user center"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"import com.longbridge.*;
import com.longbridge.quote.*;
class Main {
public static void main(String[] args) throws Exception {
try (OAuth oauth = new OAuthBuilder("your-client-id")
.build(url -> System.out.println("Open this URL to authorize: " + url))
.get();
Config config = Config.fromOAuth(oauth);
QuoteContext ctx = QuoteContext.create(config).get()) {
SecurityQuote[] resp = ctx.getQuote(
new String[] { "700.HK", "AAPL.US", "TSLA.US", "NFLX.US" }).get();
for (SecurityQuote obj : resp) {
System.out.println(obj);
}
}
}
}import com.longbridge.*;
import com.longbridge.quote.*;
class Main {
public static void main(String[] args) throws Exception {
try (OAuth oauth = new OAuthBuilder("your-client-id")
.build(url -> System.out.println("Open this URL to authorize: " + url))
.get();
Config config = Config.fromOAuth(oauth);
QuoteContext ctx = QuoteContext.create(config).get()) {
ctx.setOnQuote((symbol, quote) -> System.out.printf("%s\t%s\n", symbol, quote));
ctx.subscribe(
new String[] { "700.HK", "AAPL.US", "TSLA.US", "NFLX.US" },
SubFlags.Quote).get();
Thread.sleep(30000);
}
}
}import com.longbridge.*;
import com.longbridge.trade.*;
import java.math.BigDecimal;
class Main {
public static void main(String[] args) throws Exception {
try (OAuth oauth = new OAuthBuilder("your-client-id")
.build(url -> System.out.println("Open this URL to authorize: " + url))
.get();
Config config = Config.fromOAuth(oauth);
TradeContext ctx = TradeContext.create(config).get()) {
SubmitOrderOptions opts = new SubmitOrderOptions("700.HK",
OrderType.LO,
OrderSide.Buy,
200,
TimeInForceType.Day).setSubmittedPrice(new BigDecimal(50));
SubmitOrderResponse resp = ctx.submitOrder(opts).get();
System.out.println(resp);
}
}
}- Windows
setxrequires a new terminal; usesetfor the currentcmd.exesession. - If you don't see push events, ensure the program keeps running (e.g.
Thread.sleep(...)). - For debugging, set
LONGBRIDGE_LOG_PATHto enable SDK logs.
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.