Node.js / TypeScript quickstart
Write a TypeScript script that reads a public BTC/USD price with @siebly/kraken-api, then compile and run it in Node.js. No API key is needed. Prices are BTC/USD last traded prices.
Use Node.js 24 and npm. You can also view the complete Kraken TypeScript example on GitHub.
The SDK also works with plain JavaScript. TypeScript is optional, but we strongly recommend it for SDK integrations: checking the included types catches many invalid method names, parameters and response assumptions at build time. Keep a type check in CI even if your runtime can execute TypeScript directly; execution alone does not check types.
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-typescript-quickstart
cd kraken-typescript-quickstart
npm init -y
npm install @siebly/kraken-api
npm install --save-dev typescript@6.0.3 @types/node@24.13.2The install command adds the latest SDK release, which includes its own type definitions. The development dependencies supply the TypeScript compiler and Node.js types.
1. Create tsconfig.json
Enable strict type checking and compile the TypeScript programs into dist/.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"skipLibCheck": true,
"outDir": "dist",
"types": [
"node"
]
},
"include": [
"*.mts"
]
}Read a price
Create price.mts. The SDK’s type definitions let TypeScript check the constructor options, request parameters and response types.
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);Check types, compile and run:
npx tsc --noEmit
npx tsc
node dist/price.mjsBTC/USD REST price: 65000.5The .mts source compiles to .mjs. NodeNext compiler settings follow Node.js module resolution without changing the package’s default module format.
Stream a price (optional)
Create stream.mts when you want a live update. Its promise resolves to a price string and its finally block closes the client.
import { WebsocketClient } from '@siebly/kraken-api';
import process from 'node:process';
async function main() {
const ws = new WebsocketClient();
let timeout: ReturnType<typeof setTimeout> | undefined;
let stopped = false;
try {
const firstPrice = new Promise<string>((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: unknown) => {
console.error(error);
process.exitCode = 1;
});Compile and run it:
npx tsc
node dist/stream.mjsBTC/USD WebSocket price: 65001.2
WebSocket closed.Troubleshooting
npx tsc --noEmitchecks types without making exchange requests. Fix compiler errors before running the emitted JavaScript.- If Node cannot find
dist/price.mjsordist/stream.mjs, compile withnpx tscfirst. - If types cannot be resolved, check that both development dependencies installed successfully.
- For network or API errors, see Troubleshooting and Runtime compatibility.