NodeJS
Cài đặt & Thiết lập
Hướng dẫn cài đặt và thiết lập Node.js SDK
Yêu cầu môi trường
- Node.js
>=18 - Truy cập mạng tới REST API và WebSocket endpoints
clientId,apiKey,apiSecrettừ SSIprivateKeyđể ký lệnh giao dịch- OTP cho luồng xác thực Trading/Stream
Cài đặt
npm install @ssi.developer/ssi-sdkSDK hỗ trợ cả CommonJS và ES Module (dual CJS/ESM).
Cấu hình
Tạo đối tượng Config với thông tin xác thực:
import { Config } from '@ssi.developer/ssi-sdk';
const config = new Config({
clientId: 'YOUR_CLIENT_ID',
apiKey: 'YOUR_API_KEY',
apiSecret: 'YOUR_API_SECRET',
privateKey: 'YOUR_PRIVATE_KEY',
});Luồng khởi tạo
Config → Auth → authenticate(otp) → Data / Trading / Stream- Tạo
Configvới thông tin xác thực. - Khởi tạo
Authvớinew Auth(config). - Gọi
await auth.authenticate(otp)— bỏ trốngotpnếu chỉ dùng dữ liệu thị trường. - Khởi tạo client cần dùng (
Data,Trading,Stream) vớiauth. - Với
Stream, gọiawait stream.streaming.connect()trước khi subscribe. - Gọi
stream.disconnect()để dọn dẹp tài nguyên.
Ví dụ theo trường hợp sử dụng
Chỉ lấy dữ liệu thị trường (không cần OTP)
import { Auth, Data, Config } from '@ssi.developer/ssi-sdk';
const config = new Config({
clientId: 'YOUR_CLIENT_ID',
apiKey: 'YOUR_API_KEY',
apiSecret: 'YOUR_API_SECRET',
});
const auth = new Auth(config);
await auth.authenticate();
const data = new Data(auth);
const ohlc = await data.marketData.getOhlc1Minute('SSI');Giao dịch (cần OTP)
import { Auth, Trading, Config, OrderSide } from '@ssi.developer/ssi-sdk';
const config = new Config({
clientId: 'YOUR_CLIENT_ID',
apiKey: 'YOUR_API_KEY',
apiSecret: 'YOUR_API_SECRET',
privateKey: 'YOUR_PRIVATE_KEY',
});
const auth = new Auth(config);
await auth.authenticate('222222');
const trading = new Trading(auth);
const accounts = await trading.account.getAccountInfo();Streaming realtime (cần OTP)
import { Auth, Stream, Config } from '@ssi.developer/ssi-sdk';
const config = new Config({
clientId: 'YOUR_CLIENT_ID',
apiKey: 'YOUR_API_KEY',
apiSecret: 'YOUR_API_SECRET',
privateKey: 'YOUR_PRIVATE_KEY',
});
const auth = new Auth(config);
await auth.authenticate('222222');
const stream = new Stream(auth);
stream.streaming.onData = (msg) => {
console.log(msg);
};
await stream.streaming.connect();
stream.streaming.subscribeSymbolTrade(['SSI']);
await stream.streaming.wait();Mẹo
- Xác thực trước khi gọi API có bảo vệ bằng token.
- Với streaming, gọi
connect()sau khi xác thực thành công. - Dùng
logLevel: 'DEBUG'trongConfigđể gỡ lỗi. - Lưu credentials (
apiKey,apiSecret,privateKey) trong biến môi trường hoặc secret manager. - Gọi
stream.disconnect()để dọn dẹp đúng cách.