Node.js / JavaScript quickstart
Read a public BTC/USD price with @siebly/kraken-api. No API key or TypeScript compiler is needed. Prices are BTC/USD last traded prices.
Use Node.js 24 and npm. If you prefer to start from a finished project, view the complete Kraken Node.js 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
mkdir kraken-node-quickstart
cd kraken-node-quickstart
npm init -y
npm install @siebly/kraken-apinpm 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.
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);const { SpotClient } = require('@siebly/kraken-api');
async function main() {
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);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});Run the file you created:
node price.mjsnode price.cjsThe result is a price string, for example:
BTC/USD REST price: 65000.5Stream 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 closes the client after the first price or a failure.
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;
});const { WebsocketClient } = require('@siebly/kraken-api');
const process = require('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 the matching file:
node stream.mjsnode stream.cjsThe script prints the first update, closes its WebSocket and exits. SDK connection logs may also appear:
BTC/USD WebSocket price: 65001.2
WebSocket closed.Troubleshooting
- Use
.mjsforimportand.cjsforrequire. These Node.js module rules let you choose either format without configuring"type"inpackage.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 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 subscriptions.