REST & WebSocket v2 APIs โข Node.js & Browser โข Full Type Safety
Features โข Quick Start โข Playground โข Browser Support โข Documentation
npx ts-krakenjq formatting.env or set interactively
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);
});
๐ก Full IDE support with autocomplete for all endpoints, parameters, and response types
Launch the browser-based API playground to test endpoints visually:
npm run watch:web-ui
Perfect for:
The playground features:
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

| 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:
crypto modulecrypto.subtle)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.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