Skip to content

Node.js / JavaScript quickstart ​

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

Use Node.js 24 and npm. If you prefer to start from a finished project, view the complete Bitget 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 ​

sh
mkdir bitget-node-quickstart
cd bitget-node-quickstart
npm init -y
npm install bitget-api

npm 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.

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);
js
const { RestClientV3 } = require('bitget-api');

async function main() {
  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);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Run the file you created:

sh
node price.mjs
sh
node price.cjs

The result is a price string, for example:

text
BTCUSDT REST price: 65000.5

Stream 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.

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);
});
js
const { WebsocketClientV3 } = require('bitget-api');
const process = require('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 the matching file:

sh
node stream.mjs
sh
node stream.cjs

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 ​

  • Use .mjs for import and .cjs for require. These Node.js module rules let you choose either format without configuring "type" in package.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.

Built by Siebly. Open source SDKs for exchange APIs.