Skip to content

Deno quickstart ​

Read a public BTC/USD price with @siebly/kraken-api. No API key is needed. Prices are BTC/USD last traded prices.

Install Deno first. You can also view the complete Kraken Deno example on GitHub.

See Runtime compatibility for the checks that have been run and their results. Successful imports and builds alone do not verify live exchange access.

Create the project ​

sh
mkdir kraken-deno-quickstart
cd kraken-deno-quickstart

1. Create package.json ​

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

json
{
  "name": "kraken-deno-quickstart",
  "private": true,
  "type": "module",
  "dependencies": {
    "@siebly/kraken-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 { SpotClient } from '@siebly/kraken-api';

const client = new SpotClient();
const response = await client.getTicker({ pair: 'XBTUSD' });
const ticker = Object.values(response.result)[0];
const price = ticker?.c[0];

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

console.log('BTC/USD REST price:', price);

Run it:

sh
deno task start
text
BTC/USD REST price: 65000.5

Stream a price (optional) ​

Create stream.js to receive one WebSocket price and close the connection.

js
import { WebsocketClient } from '@siebly/kraken-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 BTC/USD update within 30 seconds.'));
      }, 30_000);

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

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

        if (price) {
          resolve(String(price));
        }
      });
      ws.on('exception', reject);

      const subscription = Promise.resolve(ws.subscribe({ topic: 'ticker', payload: { symbol: ['BTC/USD'] } }, 'spotPublicV2'));
      subscription.catch(reject).finally(() => {
        if (stopped) {
          ws.closeAll();
        }
      });
    });

    console.log('BTC/USD 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:

sh
deno task stream

The script receives one update, closes the client in finally and exits. SDK connection logs may also appear:

text
BTC/USD 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 update arrives within 30 seconds, check HTTPS and WSS access to the exchange from your network and region.

  • See Runtime compatibility for tested versions and WebSocket subscriptions for continuous streams.

Built by Siebly. Open source SDKs for exchange APIs.