Skip to content

Latest commit

ย 

History

331 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation


ts-kraken logo

๐Ÿฆ‘ Strongly-Typed TypeScript SDK for Kraken Exchange

REST & WebSocket v2 APIs โ€ข Node.js & Browser โ€ข Full Type Safety

Features โ€ข Quick Start โ€ข Playground โ€ข Browser Support โ€ข Documentation



โœจ Features

๐Ÿ”ง TypeScript Library

  • โœ… Full type safety with IntelliSense autocomplete
  • โœ… REST & WebSocket v2 API support
  • โœ… RxJS observables for real-time data streams
  • โœ… Helper methods for common operations (orders, balances, tickers)
  • โœ… Works in Node.js & browsers (no code changes needed)

๐Ÿ’ป Interactive REPL CLI

  • ๐Ÿš€ Instant access via npx ts-kraken
  • ๐ŸŽจ Beautiful terminal output with jq formatting
  • ๐Ÿ”Œ Subscribe to WebSocket streams directly from your shell
  • ๐Ÿ”‘ Load credentials from .env or set interactively

๐ŸŽฎ Web Playground

  • ๐ŸŒ Browser-based API testing interface
  • ๐Ÿ“Š Execute REST & WebSocket requests visually
  • โšก Quick actions for common operations
  • ๐Ÿ’พ Session-only credential storage (secure)
  • ๐ŸŽฏ Perfect when Kraken UI is down or slow!

Web Playground


๐Ÿš€ Quick Start

๐Ÿ“ฆ Install

npm install ts-kraken

Optional: Set up API credentials for private methods (trading, balances, orders):

Option 1: Use environment variables in your npm scripts (Node.js only)

{
  "scripts": {
    "start": "KRAKEN_API_KEY=xxx KRAKEN_API_SECRET=yyy node app.js"
  }
}

Option 2: Use a library like dotenv (Node.js only)

npm install dotenv
# .env
KRAKEN_API_KEY=your-api-key-here
KRAKEN_API_SECRET=your-api-secret-here
import 'dotenv/config';
// Now process.env.KRAKEN_API_KEY is available

Option 3: Pass credentials directly in code (works in browser & Node.js)

// See examples below - credentials passed as second parameter

๐ŸŽฏ Use in Your Code

๐Ÿ’ก Note: Examples use top-level await - requires ES2022+ or async context

import {
  publicRestRequest,
  privateRestRequest,
  publicWsSubscription,
  privateWsSubscription
} from 'ts-kraken';

const KRAKEN_API_KEY = 'your-api-key-here';
const KRAKEN_API_SECRET = 'your-api-secret-here';

// ๐Ÿ“ˆ Get BTC/USD ticker (public)
const ticker = await publicRestRequest({
  url: 'Ticker',
  params: { pair: 'XBTUSD' }
});

// ๐Ÿ’ฐ Get account balance (private)
const balance = await privateRestRequest(
  { url: 'Balance' },
  {
    apiKey: KRAKEN_API_KEY,
    apiSecret: KRAKEN_API_SECRET
  } /* Optional runtime override. In NodeJS, process.env values are used if not passed */
);

// ๐Ÿ“ก Subscribe to live ticker updates
const ticker$ = publicWsSubscription({
  channel: 'ticker',
  params: { symbol: ['BTC/USD'] }
});

ticker$.subscribe(({ data }) => {
  console.log('Latest price:', data[0].last);
});

// ๐Ÿ”’ Subscribe to private executions feed
const executions$ = await privateWsSubscription(
  {
    channel: 'executions',
    params: { snapshot: true }
  },
  {
    apiKey: KRAKEN_API_KEY,
    apiSecret: KRAKEN_API_SECRET
  } /* Optional runtime override. In NodeJS, process.env values are used if not passed */
);

executions$.subscribe(({ data }) => {
  console.log('Trade executed:', data);
});

IDE autocomplete

๐Ÿ’ก Full IDE support with autocomplete for all endpoints, parameters, and response types

IDE type hints


๐ŸŽฎ Interactive Playground

Clone this repo (the built npm package doesn't bring the playground module).

git clone https://github.com/yeikiu/ts-kraken.git
cd ts-kraken
npm install

Launch the browser-based API playground to test endpoints visually:

npm run watch:web-ui

Perfect for:

  • ๐Ÿ” Testing API endpoints without writing code
  • ๐Ÿ“Š Exploring available methods and parameters
  • ๐Ÿšจ Trading when Kraken's official UI is down
  • ๐ŸŽ“ Learning the API structure

The playground features:

  • Quick Actions - Common operations with one click
  • Method Browser - All REST & WebSocket endpoints organized by category
  • Live Terminal - See real-time responses
  • Smart Forms - Auto-generated parameter inputs with validation

๐Ÿ’ป REPL CLI

Launch Instantly

npx ts-kraken

Quick Examples

# ๐Ÿ“Š Get current server time
.get Time

# ๐Ÿ’ฑ Get BTC/EUR trading pair info
.get AssetPairs pair=BTC/EUR

# ๐Ÿ’ฐ Check your balances (requires API keys)
.post Balance

# ๐Ÿ“ˆ Subscribe to live BTC/USD ticker
.pubsub ticker symbol[]=BTC/USD

# ๐Ÿ”’ Subscribe to your trade executions
.privsub executions snap_orders=true

# ๐Ÿ›‘ Unsubscribe from all streams
.unsuball

REPL Demo

REPL Commands Reference

Command Description Example
.get Fetch public REST data .get Ticker pair=BTC/USD
.post Fetch private REST data .post OpenOrders
.pubsub Subscribe to public WebSocket .pubsub ticker symbol[]=BTC/USD
.privsub Subscribe to private WebSocket .privsub balances
.unsub Unsubscribe from specific channel .unsub ticker
.unsuball Unsubscribe from all channels .unsuball
.setkeys Set API credentials (session only) .setkeys
.showkeys Display current credentials .showkeys
.help Show all commands .help
.exit Exit the REPL .exit

๐Ÿ’ก Pro tip: Use jq filters and -table flag for formatted output:

.get AssetPairs . as $base|keys|map($base[.])|map({wsname,tick_size}) -table

Set Up API Keys

Create a .env file in your working directory:

KRAKEN_API_KEY=your-api-key-here
KRAKEN_API_SECRET=your-api-secret-here

๐ŸŒ Browser & Node.js Support

ts-kraken v5.0+ works seamlessly in both environments with zero configuration.

How It Works

The library automatically detects your runtime and uses:

  • Node.js: Native crypto module
  • Browser: Web Crypto API (crypto.subtle)

Browser Requirements

  • โœ… Modern browser (Chrome, Firefox, Safari, Edge)
  • โœ… ES2020+ support
  • โœ… Web Crypto API (built into all modern browsers)

Browser Security Best Practices

When using ts-kraken in the browser:

โš ๏ธ NEVER hardcode API keys in client-side code
โœ… Store credentials in session storage only (not localStorage)
โœ… Use separate API keys with limited permissions
โœ… Consider using a backend proxy for sensitive operations
โœ… Be aware that browser code is visible to users

CORS & Proxy Setup

Some Kraken API endpoints may require a proxy due to CORS restrictions. The web playground handles this automatically in development mode.

Bundler Configuration

Most modern bundlers (Vite, Webpack, etc.) handle Node.js module externalization automatically. No configuration needed!

๐Ÿ“ฆ Vite Configuration (if needed)
// vite.config.ts
export default {
  resolve: {
    alias: {
      crypto: 'crypto-browserify' // fallback if needed
    }
  }
}

๐Ÿ“š Documentation

Official Resources

Additional Tools


๐ŸŽฏ Use Cases

๐Ÿค– Trading Bots

Build automated trading strategies with full type safety and real-time data streams.

๐Ÿ“Š Market Analysis

Subscribe to multiple WebSocket feeds and analyze market data in real-time with RxJS operators.

โšก Emergency Trading

Use the REPL or web playground to execute trades when Kraken's UI is experiencing issues.

๐Ÿงช API Testing

Quickly test and validate Kraken API endpoints before integrating into production.

๐Ÿ“ˆ Portfolio Management

Monitor balances, open orders, and trade history with strongly-typed responses.


๐Ÿ™ Acknowledgments


๐Ÿ“„ License

MIT License - see LICENSE file for details


๐Ÿ”— Links


Made with ๐Ÿ’™ for the Kraken developer community

About

๐Ÿ‘พ๐Ÿ“ˆ A strongly typed library to operate with the Kraken Crypto Exchange | Official docs ๐Ÿ‘‡

Topics

Resources

Stars

16 stars

Watchers

3 watching

Forks

Releases

Used by

Contributors

Languages