OAuth 2.0 is the recommended authentication method. It uses Bearer tokens and avoids the need for HMAC signatures.
Step 1: Register an OAuth Client
Register your application to obtain a client_id by sending a POST request to the registration endpoint.
macOS / Linux:
curl -X POST https://openapi.longbridge.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"]
}'
Windows (PowerShell):
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"]
}'
Step 2: Build the OAuth Client in Node.js
Use OAuth.build() to handle the authorization flow. This method automatically checks for a cached token in ~/.longbridge/openapi/tokens/<client_id> (or %USERPROFILE% on Windows) before starting a browser-based flow.
const { OAuth, Config } = require('longbridge');
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();