Node-Kraken-API: A Comprehensive Guide to the Typed REST/WS Node.JS Client for Kraken

·

Introduction

node-kraken-api is a powerful typed REST and WebSocket Node.JS client designed specifically for interacting with the Kraken cryptocurrency exchange. This unofficial API wrapper provides developers with a robust toolkit for seamless integration with Kraken's trading platform.

Key Features

Getting Started

Installation

Integrate node-kraken-api into your project with npm:

npm i --save node-kraken-api

Basic Implementation

import { Kraken } from "node-kraken-api";

// For public endpoints
const kraken = new Kraken();

// For private endpoints
const privateKraken = new Kraken({ 
  key: "YOUR_API_KEY", 
  secret: "YOUR_API_SECRET" 
});

REST API Usage

Public Endpoints Example

const kraken = new Kraken();

async function getMarketData() {
  const { unixtime } = await kraken.time();
  const { XXBT } = await kraken.assets();
  const ticker = await kraken.ticker({ pair: "XXBTZUSD" });
  return { unixtime, XXBT, ticker };
}

Private Endpoints Example

const kraken = new Kraken({ key: "...", secret: "..." });

async function placeOrder() {
  const { txid } = await kraken.addOrder({
    pair: "XXBTZUSD",
    type: "buy",
    ordertype: "limit",
    price: "65432.1",
    volume: "1",
  });
  return txid;
}

👉 Discover more trading strategies with OKX's advanced API

WebSocket Implementation

Public WebSocket Connections

const kraken = new Kraken();

async function monitorTrades() {
  const trade = await kraken.ws.trade()
    .on('update', (update, pair) => console.log(update, pair))
    .subscribe('XBT/USD');
}

Private WebSocket Connections

const kraken = new Kraken({ key: "...", secret: "..." });

async function trackOrders() {
  const { token } = await kraken.getWebSocketsToken();
  const orders = kraken.ws.openOrders({ token: token! })
    .on("update", (update, sequence) => console.log(update, sequence))
    .subscribe();
}

Advanced Configuration

API Settings

new Kraken({
  key: "API_KEY",
  secret: "API_SECRET",
  genotp: () => "OTP_CODE", // For two-factor authentication
  gennonce: () => Date.now() * 1000, // Custom nonce generator
  timeout: 5000 // Connection timeout in ms
});

Migration Guide

Version 0.4.1 to 2.2.2

The entire project has been rewritten in TypeScript with significant architectural changes. Key differences include:

Version 1.0.0 to 2.2.2

Minor changes to the Emitter class architecture with improved event handling consistency.

Development and Contribution

Contributions are welcome! The project particularly needs:

👉 Explore blockchain development opportunities with OKX

FAQ

Is this API officially supported by Kraken?

No, this is an unofficial wrapper. Always refer to Kraken's official documentation for the most up-to-date API specifications.

How do I handle WebSocket authentication?

You'll need to:

  1. Generate a WebSocket token using your API credentials
  2. Pass this token when establishing private WebSocket connections

What happens if there's a WebSocket disconnection?

The API automatically attempts to reconnect and resubscribe to previous channels, maintaining your subscriptions.

Can I use this for high-frequency trading?

While technically possible, consider implementing proper rate limiting and connection management for HFT scenarios.

How do I handle binary responses like export files?

Use the RetrieveExport method which returns a buffer:

const buf = await kraken.retrieveExport({ id: "EXPORT_ID" });
fs.writeFileSync("report.zip", buf);

License

MIT Licensed - See the project LICENSE for complete details.