Deno quickstart
Read a public btcusdt price with @siebly/htx-api. No API key is needed. Prices are btcusdt last traded prices.
Install Deno first. You can also view the complete HTX Deno example on GitHub.
The streaming example requires DecompressionStream support in your runtime.
Create the project
mkdir htx-deno-quickstart
cd htx-deno-quickstart1. Create package.json
Declare the SDK dependency and the commands used by this project.
{
"name": "htx-deno-quickstart",
"private": true,
"type": "module",
"dependencies": {
"@siebly/htx-api": "latest"
}
}2. Create deno.json
Tell Deno to install npm dependencies locally and define the price and stream tasks.
{
"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
deno installRead a price
Create price.js to make a public REST request.
import { SpotClient } from '@siebly/htx-api';
const client = new SpotClient();
const response = await client.getTicker({ symbol: 'btcusdt' });
const price = response.tick.close;
if (!price) {
throw new Error('No btcusdt price was returned.');
}
console.log('btcusdt REST price:', price);Run it:
deno task startbtcusdt REST price: 65000.5Stream a price (optional)
Create stream.js to receive one WebSocket price and close the connection.
import { WebsocketClient } from '@siebly/htx-api';
import process from 'node:process';
async function main() {
const ws = new WebsocketClient();
let timeout;
let stopped = false;
try {
const firstPrice = new Promise((resolve, reject) => {
timeout = setTimeout(() => {
reject(new Error('No btcusdt update within 30 seconds.'));
}, 30_000);
ws.on('message', (message) => {
if (Array.isArray(message) || message.ch !== 'market.btcusdt.detail') {
return;
}
const tick = message.tick;
if (!tick || typeof tick !== 'object' || !('close' in tick)) {
return;
}
const price = tick.close;
if (price) {
resolve(String(price));
}
});
ws.on('exception', reject);
const subscription = Promise.resolve(ws.subscribe('market.btcusdt.detail', 'spotPublic'));
subscription.catch(reject).finally(() => {
if (stopped) {
ws.closeAll();
}
});
});
console.log('btcusdt WebSocket price:', await firstPrice);
} finally {
stopped = true;
clearTimeout(timeout);
ws.closeAll();
console.log('WebSocket closed.');
}
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});Run it:
deno task streamThe script receives one update, closes the client in finally and exits. SDK connection logs may also appear:
btcusdt WebSocket price: 65001.2
WebSocket closed.Troubleshooting
Use
deno task startto retain the documented permissions. A permission prompt or denial can mean you randeno run price.jsdirectly. Network access is needed for the public exchange; environment and read access support npm dependencies.Keep
nodeModulesDir: "auto"indeno.jsonand rundeno installif npm dependencies cannot be resolved.If a request fails or no update arrives within 30 seconds, check HTTPS and WSS access to the exchange from your network and region.
See Runtime setup for configuration details and WebSocket subscriptions for continuous streams.