Bun quickstart
Read a public BTC-USDT price with kucoin-api. No API key is needed. Prices are BTC-USDT last traded prices.
Install Bun first. You can also view the complete KuCoin Bun example on GitHub.
These examples use public market data. Keep authenticated account and trading calls on your server. See Runtime setup for project recipes and required configuration.
Create the project
mkdir kucoin-bun-quickstart
cd kucoin-bun-quickstart
bun add kucoin-apiRead a price
Create price.js to make a public REST request.
import { SpotClient } from 'kucoin-api';
const client = new SpotClient();
const response = await client.getTicker({ symbol: 'BTC-USDT' });
const price = response.data.price;
if (!price) {
throw new Error('No BTC-USDT price was returned.');
}
console.log('BTC-USDT REST price:', price);Run it:
bun price.jsBTC-USDT REST price: 65000.5Stream a price (optional)
Create stream.js to stream WebSocket prices until Ctrl+C.
import { WebsocketClient } from 'kucoin-api';
import process from 'node:process';
const ws = new WebsocketClient();
ws.on('update', (message) => {
if (message.topic !== '/market/ticker:BTC-USDT') {
console.log('WebSocket update:', message);
return;
}
const price = message.data?.price;
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);
ws.subscribe('/market/ticker:BTC-USDT', 'spotPublicV1');Run it:
bun stream.jsThe 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
Run
bun installin the directory containingpackage.jsonif the SDK import cannot be resolved.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 streams.