Skip to content

Deno quickstart ​

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

Install Deno first. You can also view the complete Gate.io Deno 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-deno-quickstart
cd gateio-deno-quickstart

1. Create package.json ​

Declare the SDK dependency and the commands used by this project.

json
{
  "name": "gateio-deno-quickstart",
  "private": true,
  "type": "module",
  "dependencies": {
    "gateio-api": "latest"
  }
}

2. Create deno.json ​

Tell Deno to install npm dependencies locally and define the price and stream tasks.

json
{
  "nodeModulesDir": "auto",
  "tasks": {
    "start": "deno run --allow-net --allow-env --allow-read price.js",
    "stream": "deno run --allow-net --allow-env --allow-read stream.js"
  }
}

3. Install dependencies ​

sh
deno install

Read a price ​

Create price.js to make a public REST request.

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);

Run it:

sh
deno task start
text
BTC_USDT REST price: 65000.5

Stream a price (optional) ​

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

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');

Run it:

sh
deno task stream

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 deno task start to retain the documented permissions. A permission prompt or denial can mean you ran deno run price.js directly. Network access is needed for the public exchange; environment and read access support npm dependencies.

  • Keep nodeModulesDir: "auto" in deno.json and run deno install if npm dependencies 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.