Overview
A robust Node.js connector for OKX APIs and WebSockets featuring:
✅ Full integration with all OKX API endpoints
✅ TypeScript support with comprehensive type declarations
✅ 100+ end-to-end tests validating real API/WebSocket interactions
✅ Advanced WebSocket capabilities:
- Configurable connection heartbeats
- Automatic reconnection & resubscription
- Seamless authentication handling
✅ Browser compatibility via Webpack bundling
👉 Get started with OKX API integration
Key Features Breakdown
1. REST API Integration
- Complete coverage of OKX's REST API endpoints
- Type-safe request/response handling
- Built-in rate limit management
2. WebSocket Implementation
const wsClient = new WebsocketClient({
accounts: [{
apiKey: API_KEY,
apiSecret: API_SECRET,
apiPass: API_PASS
}]
});
wsClient.on('update', (data) => {
console.log('Real-time update:', data);
});3. Testing Framework
100+ integration tests covering:
- API authentication
- Order management
- Market data endpoints
- WebSocket connectivity
Installation & Setup
Prerequisites
- Node.js ≥ 14.x
- TypeScript ≥ 4.0 (recommended)
Installation
npm install okx-api-new
# or
yarn add okx-api-newConfiguration
Generate API keys from your OKX account dashboard and configure:
import { RestClient } from 'okx-api-new';
const client = new RestClient({
apiKey: 'YOUR_API_KEY',
apiSecret: 'YOUR_API_SECRET',
apiPass: 'YOUR_API_PASSWORD'
});Core Functionality
REST API Methods
| Category | Example Methods |
|---|---|
| Market Data | getTickers(), getFundingRateHistory() |
| Account | getBalance(), getPositions() |
| Trading | placeOrder(), cancelOrder() |
WebSocket Channels
Public:
- Tickers
- Funding rates
- Order book
Private:
- Account updates
- Position changes
- Order events
👉 Explore all available methods
Best Practices
Error Handling
try {
const response = await client.getFundingRateHistory({
instId: "BTC-USDT-SWAP"
});
console.log(response.data);
} catch (error) {
console.error('API Error:', error.response?.data);
}Performance Optimization
- Batch WebSocket subscriptions
- Cache frequent API responses
- Implement exponential backoff for retries
FAQ
Q: How do I handle WebSocket disconnections?
A: The client automatically manages reconnections and resubscriptions. Listen for reconnected events to confirm restoration.
Q: Is browser support available?
A: Yes! Use the Webpack bundle from /dist or configure your bundler with the necessary polyfills.
Q: How frequently should I poll REST APIs?
A: For most use cases, combine WebSocket subscriptions with periodic REST API checks (every 30-60 seconds).
Q: Where can I find API rate limits?
A: Refer to OKX's official API documentation for current limits.
Project Structure
okx-api-new/
├── src/ # TypeScript source
├── lib/ # Compiled JavaScript
├── dist/ # Browser bundle
├── examples/ # Implementation samples
└── test/ # Integration testsContribution Guidelines
We welcome community contributions! Before submitting:
- Ensure all tests pass (
npm test) - Maintain TypeScript type coverage
- Include relevant documentation updates
For bug reports or feature requests, please open a GitHub issue.