node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »


Kraken API Universal Client

This is an API client for the Kraken cryptocurrency exchange API, with unit tests (let us know if there's some test you want to add) and no polyfill.

The API client supports all methods on Node.js v6.11.1 or later, and the separately exported public-only client supports calling public methods from the browser as well.


import KrakenClient from 'kraken-api-universal-client'
const kraken = new KrakenClient({
  key: 'YOUR_API_KEY',
  secret: 'YOUR_API_SECRET',
// Handles both public and private requests, as long as the provided API
// credentials have the necessary permissions
kraken.request('Assets') // public
  .then(response => { /**/ })
  .catch(error => { /**/ })
kraken.request('TradeBalance', { body: { asset: 'ZEUR' } }) // private
  .then(response => { /**/ })
  .catch(error => { /**/ })
import { createPublicKrakenClient } from 'kraken-api-universal-client'
const publicKraken = createPublicKrakenClient()
// Handles only public requests; will throw error if attempting private requests
  .then(response => { /* handle response */ })
  .catch(error => { /* handle error */ })


yarn add kraken-api-universal-client


npm install --save kraken-api-universal-client


1. Import client

The default export is the client class and the constants are available as a named export, which you can deconstruct to get e.g. the default endpoints. There is also a named export of createPublicKrakenClient, the factory for the client-side client that only handles public methods.

import KrakenClient, {
} from './index'
const {
} = constants

2. Initialize client

To initialize the client you need an API key and API secret, which you obtain through your Kraken account. For security reasons you should only assign the permissions you truly need to the given API key. If you are going to use the public-only client, you do not need any API credentials.

// default, server-side client
const kraken = new KrakenClient({
  key: 'YOUR_API_KEY',
  secret: 'YOUR_API_SECRET',
// public-only client
const publicKraken = createPublicKrakenClient()

3. Make requests

request(endpointKey: string, options?: {
  body?: object // key-value pairs according to Kraken docs
  fetch?: function // custom-implementation of fetch, used in tests
  nonce?: number // custom nonce, defaults to Number(`${}000`)

To make a request to the Kraken API, call the method request() on your initialized client with the endpointKey, and optionally with a body (containing key-value pairs according to the Kraken docs), a custom-implementation of fetch and/or a custom nonce (it defaults to multiplied by 1000, which was found to satisfy Kraken's requirement of "always increasing unsigned 64 bit integer"). The endpointKey is the last part of the Kraken API URL, e.g. like this:

Note that you do not need to specify whether the endpoint is public or private, the client will know that. If an endpointKey is missing, please submit an issue (and a PR, if you can).

// public method
  .then(response => { /**/ })
  .catch(error => { /**/ })
// private method without body
.then(response => { /**/ })
.catch(error => { /**/ })
// private method with body
kraken.request('TradeBalance', { body: { asset: 'ZEUR' } })
  .then(response => { /**/ })
  .catch(error => { /**/ })


The request method returns a Promise, so use .then() and .catch() to handle a response or error respectively (as seen in the example. Please refer to the Kraken documentation and the client code for the details on how responses and errors are structured.

No polyfill needed

As this library prefers to not polyfill fetch and Promise, it depends on fetch-ponyfill, which is recommended by isomorphic-fetch and uses the browser window.Promise and Node global.Promise. Therefore, make sure to use a Node version that implements Promise (if you follow the "engines" requirement in package.json, you should be fine).


Thanks to: