Node.js / JavaScript quickstart
Read a public BTC-USDT price with okx-api. No API key or TypeScript compiler is needed. Prices are BTC-USDT last traded prices.
Use Node.js 24 and npm. If you prefer to start from a finished project, view the complete OKX Node.js example on GitHub.
These examples use public market data. Keep authenticated account and trading calls on your server. See Runtime setup for the scope of each recipe; a successful build does not verify live exchange access.
Create the project
mkdir okx-node-quickstart
cd okx-node-quickstart
npm init -y
npm install okx-apinpm init creates package.json; npm install adds the current SDK release. You do not need to edit the package file for these examples.
Read a price
For a new project, create price.mjs with import. If your project uses require, choose price.cjs instead. Create one file; both request the price directly.
import { RestClient } from 'okx-api';
const client = new RestClient();
const tickers = await client.getTicker({ instId: 'BTC-USDT' });
const price = tickers[0]?.last;
if (!price) {
throw new Error('No BTC-USDT price was returned.');
}
console.log('BTC-USDT REST price:', price);const { RestClient } = require('okx-api');
async function main() {
const client = new RestClient();
const tickers = await client.getTicker({ instId: 'BTC-USDT' });
const price = tickers[0]?.last;
if (!price) {
throw new Error('No BTC-USDT price was returned.');
}
console.log('BTC-USDT REST price:', price);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});Run the file you created:
node price.mjsnode price.cjsThe result is a price string, for example:
BTC-USDT REST price: 65000.5Stream a price (optional)
For a new project, create stream.mjs with import. If your project uses require, choose stream.cjs instead. Create one stream file; it streams prices until you press Ctrl+C. Exceptions and reconnections are logged without stopping the client.
import { WebsocketClient } from 'okx-api';
import process from 'node:process';
const ws = new WebsocketClient();
ws.on('update', (message) => {
if (message.arg?.channel !== 'tickers') {
console.log('WebSocket update:', message);
return;
}
const price = message.data?.[0]?.last;
if (price) {
console.log('BTC-USDT WebSocket price:', String(price));
}
});
ws.on('response', (message) => {
console.log('WebSocket response:', message);
});
ws.on('exception', (error) => {
console.error('WebSocket exception:', error);
});
ws.on('reconnect', (event) => {
console.log('WebSocket reconnecting:', event);
});
ws.on('reconnected', (event) => {
console.log('WebSocket reconnected:', event);
});
function stop() {
ws.closeAll();
console.log('WebSocket closed.');
}
process.once('SIGINT', stop);
process.once('SIGTERM', stop);
Promise.all(ws.subscribe({ channel: 'tickers', instId: 'BTC-USDT' })).catch((error) => {
console.error('WebSocket subscription failed:', error);
});const { WebsocketClient } = require('okx-api');
const process = require('node:process');
const ws = new WebsocketClient();
ws.on('update', (message) => {
if (message.arg?.channel !== 'tickers') {
console.log('WebSocket update:', message);
return;
}
const price = message.data?.[0]?.last;
if (price) {
console.log('BTC-USDT WebSocket price:', String(price));
}
});
ws.on('response', (message) => {
console.log('WebSocket response:', message);
});
ws.on('exception', (error) => {
console.error('WebSocket exception:', error);
});
ws.on('reconnect', (event) => {
console.log('WebSocket reconnecting:', event);
});
ws.on('reconnected', (event) => {
console.log('WebSocket reconnected:', event);
});
function stop() {
ws.closeAll();
console.log('WebSocket closed.');
}
process.once('SIGINT', stop);
process.once('SIGTERM', stop);
Promise.all(ws.subscribe({ channel: 'tickers', instId: 'BTC-USDT' })).catch((error) => {
console.error('WebSocket subscription failed:', error);
});Run the matching file:
node stream.mjsnode stream.cjsThe script keeps streaming until Ctrl+C. Exceptions and reconnections are logged; signal handlers close the client. SDK connection logs may also appear:
BTC-USDT WebSocket price: 65001.2
WebSocket closed.Troubleshooting
- Use
.mjsforimportand.cjsforrequire. These Node.js module rules let you choose either format without configuring"type"inpackage.json. - If the SDK cannot be resolved, run the install command from the directory containing your program and
package.json. - If a request fails or no stream updates arrive, check HTTPS and WSS access to the exchange from your network and region.
- See Runtime setup for setup details and WebSocket subscriptions for continuous subscriptions.