ts-kraken


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



  • โœ… 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)
  • ๐Ÿš€ 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
  • ๐ŸŒ 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


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

๐Ÿ’ก 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

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

npx ts-kraken
# ๐Ÿ“Š 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

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

Create a .env file in your working directory:

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

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

The library automatically detects your runtime and uses:

  • Node.js: Native crypto module
  • Browser: Web Crypto API (crypto.subtle)
  • โœ… Modern browser (Chrome, Firefox, Safari, Edge)
  • โœ… ES2020+ support
  • โœ… Web Crypto API (built into all modern browsers)

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

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

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
}
}
}


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

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

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

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

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



MIT License - see LICENSE file for details



Made with ๐Ÿ’™ for the Kraken developer community