node-kraken-api
About
node-kraken-api is a typed REST/WS Node.JS client for the Kraken cryptocurrency exchange.
This is an unofficial API. Please refer to the official documentation for up-to-date information.
REST API Docs: kraken.com/features/api
WebSocket API Docs: docs.kraken.com/websockets
Features
- Fully typed REST and WS responses and options.
- REST methods/comments are generated from the official OpenAPI specifications file.
- WS methods/comments are sourced from the official WebSockets 1.8.3 documentation.
- Note:
- All named response properties are optional and nullable unless explicitly marked required in the documentation.
RetrieveExport
(binary endpoint); see the example.- Full WS orderbook mirroring and checksum validation.
MIGRATION NOTICE 0.4.1 -> 1.0.0 [BREAKING]:
The entire project has been completely rewritten using TypeScript and many features have changed.
Added
- Complete WS 1.8.3 functionality
- Typings
- New REST methods
Removed
- Custom response parsing
- Ratelimiting
- REST syncing
- Method name settings
- Direct construction using
module.exports()
Changed
.call()
: renamed to .request()
..setOTP()
: removed; OTP is now provided using a user-supplied generator.
Synopsis
Methods
Properties
Classes
Usage
Integration
npm i --save node-kraken-api
import { Kraken } from "node-kraken-api";
Settings
{
key?: string;
secret?: string;
genotp?: () => string;
gennonce?: () => number;
timeout?: number;
}
REST API
Public
const kraken = new Kraken();
const { unixtime } = await kraken.time();
const { XXBT } = await kraken.assets();
const ticker = await kraken.ticker({ pair: "XXBTZUSD" })
Private
const kraken = new Kraken({ key: "...", secret: "..." });
const { txid } = await kraken.addOrder({
pair: "XXBTZUSD",
type: "buy",
ordertype: "limit",
price: "65432.1",
volume: "1",
});
If your key requires an OTP, provide a generator:
const kraken = new Kraken({ key: "...", secret: "...", genotp: () => "..." });
RetrieveExport is the only method that promises a buffer:
const kraken = new Kraken({ key: "...", secret: "..." });
const buf = await kraken.retrieveExport({ id: "FOOB" })
fs.writeFileSync("report.zip", buf)
WebSockets
- All WebSocket subscriptions and requests are located within
.ws
.
.ws.pub
and .ws.priv
provides ping, heartbeat, systemStatus, and general error monitoring.
- Automatically connects to the socket when server data is requested.
- See
Kraken.WS.Connection.open()
and Kraken.WS.Connection.close()
for manual connection management.
- Subscription methods return a
Kraken.Subscriber
object that manages subscriptions for a given name and options.
Public
const kraken = new Kraken();
const trade = await kraken.ws.trade()
.on('update', (update, pair) => console.log(update, pair))
.on('status', (status) => console.log(status))
.on('error', (error, pair) => console.log(error, pair))
.subscribe('XBT/USD')
const book100 = await kraken.ws.book({depth: 100})
.on("mirror", (mirror, pair) => console.log(mirror, pair))
.on("error", (error, pair) => console.log(error, pair))
.on("status", (status) => console.log(status)
.subscribe("XBT/USD", "ETH/USD");
await book100.unsubscribe('XBT/ETH');
Private
const kraken = new Kraken({ key: "...", secret: "..." });
const { token } = await kraken.getWebSocketsToken();
const orders = kraken.ws.openOrders({ token: token! })
.on("update", (update, sequence) => console.log(update, sequence))
.subscribe();
await orders.unsubscribe();
Testing
Testing is performed by @jpcx/testts.
To run tests:
- Save an
auth.json
file with your key and secret: { key: string, secret: string }
- Please ensure that this key has readonly permissions.
- Run
npm test
in the main directory.
Development
Contribution is welcome!
Given the amount of typings in this project, there may be discrepancies so please raise an issue or create a pull request.
Also, I am US-based and can't access the futures API; if you have access and want to contribute let me know!
Author
Justin Collier - jpcx
bitcoin:bc1qnkturlnv4yufkc40k4ysxz4maak5mug7l820my
litecoin:ltc1q9jnvesyffgysel7h4cttg7fscenaje2ka08qvc
ethereum:0xD03c9c3027C6bBDDad31d183Ba07DA9db34ee641
monero:49UydqpjBjfPcYEV26jrruQwkCkW9VVFxXdmBwmarAVUPz6FSK2nLsRCdtQTMrFZ3NBy9aGrgGGhKZKCpApy5xBWA3GiBsn
Inspired by npm-kraken-api (nothingisdead).
License
This project is licensed under the MIT License - see the LICENSE file for details