Skip to content

Bun quickstart ​

Read a public BTCUSDT price with bitget-api. No API key is needed. Prices are BTCUSDT last traded prices.

Install Bun first. You can also view the complete Bitget Bun 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 ​

sh
mkdir bitget-bun-quickstart
cd bitget-bun-quickstart
bun add bitget-api

Read a price ​

Create price.js to make a public REST request.

js
import { RestClientV3 } from 'bitget-api';

const client = new RestClientV3();
const response = await client.getTickers({
  category: 'SPOT',
  symbol: 'BTCUSDT',
});
const price = response.data[0]?.lastPrice;

if (!price) {
  throw new Error('No BTCUSDT price was returned.');
}

console.log('BTCUSDT REST price:', price);

Run it:

sh
bun price.js
text
BTCUSDT REST price: 65000.5

Stream a price (optional) ​

Create stream.js to stream WebSocket prices until Ctrl+C.

js
import { WebsocketClientV3 } from 'bitget-api';
import process from 'node:process';

const ws = new WebsocketClientV3();

ws.on('update', (message) => {
  if (message.arg?.topic !== 'ticker') {
    console.log('WebSocket update:', message);
    return;
  }

  const price = message.data?.[0]?.lastPrice;

  if (price) {
    console.log('BTCUSDT 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({
  topic: 'ticker',
  payload: {
    instType: 'spot',
    symbol: 'BTCUSDT',
  },
}, 'v3Public').catch((error) => {
  console.error('WebSocket subscription failed:', error);
});

Run it:

sh
bun stream.js

The script keeps streaming until Ctrl+C. Exceptions and reconnections are logged; signal handlers close the client. SDK connection logs may also appear:

text
BTCUSDT WebSocket price: 65001.2
WebSocket closed.

Troubleshooting ​

  • Run bun install in the directory containing package.json if 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.

Built by Siebly. Open source SDKs for exchange APIs.