npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kraken-api-universal-client

v1.0.0

Published

Universal (browser & Node.js) client for the Kraken API

Downloads

8

Readme

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.

Example

// SERVER-SIDE
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 => { /**/ })

// CLIENT-SIDE
import { createPublicKrakenClient } from 'kraken-api-universal-client'

const publicKraken = createPublicKrakenClient()

// Handles only public requests; will throw error if attempting private requests
publicKraken.request('Assets')
  .then(response => { /* handle response */ })
  .catch(error => { /* handle error */ })

Installation

yarn add kraken-api-universal-client

or

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

API

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, {
  createPublicKrakenClient,
  constants,
} from './index'

const {
  DEFAULT_ENDPOINTS,
  HEADER_API_KEY_KEY,
  HEADER_API_SIGNATURE_KEY,
  HEADER_USER_AGENT_KEY,
  HEADER_USER_AGENT_VALUE,
  PRIVATE,
  PUBLIC,
} = 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(`${Date.now()}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 Date.now() 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:

  • https://api.kraken.com/0/public/Assets -> endpointKey: 'Assets'
  • https://api.kraken.com/0/private/Balance -> endpointKey: 'Balance'

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
kraken.request('Assets')
  .then(response => { /**/ })
  .catch(error => { /**/ })

// private method without body
kraken.request('Balance')
.then(response => { /**/ })
.catch(error => { /**/ })

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

Response/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).

Credits

Thanks to:

License

MIT