Skip to content

Node.js / JavaScript quickstart ​

Read a public BTC_USDT price with gateio-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 Gate.io Node.js 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 ​

sh
mkdir gateio-node-quickstart
cd gateio-node-quickstart
npm init -y
npm install gateio-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 { RestClient } from 'gateio-api';

const client = new RestClient();
const tickers = await client.getSpotTicker({ currency_pair: '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);
js
const { RestClient } = require('gateio-api');

async function main() {
  const client = new RestClient();
  const tickers = await client.getSpotTicker({ currency_pair: '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:

sh
node price.mjs
sh
node price.cjs

The result is a price string, for example:

text
BTC_USDT 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 { WebsocketClient } from 'gateio-api';
import process from 'node:process';

const ws = new WebsocketClient();

ws.on('update', (message) => {
  if (message.channel !== 'spot.tickers') {
    console.log('WebSocket update:', message);
    return;
  }

  const price = message.result?.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);

ws.subscribe({ topic: 'spot.tickers', payload: ['BTC_USDT'] }, 'spotV4');
js
const { WebsocketClient } = require('gateio-api');
const process = require('node:process');

const ws = new WebsocketClient();

ws.on('update', (message) => {
  if (message.channel !== 'spot.tickers') {
    console.log('WebSocket update:', message);
    return;
  }

  const price = message.result?.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);

ws.subscribe({ topic: 'spot.tickers', payload: ['BTC_USDT'] }, 'spotV4');

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