Skip to content

tuliof/kest-trade-sdk

Repository files navigation

Questrade Typescript SDK

A TypeScript SDK for the Questrade API, built with Bun. Provides type-safe access to:

  • Accounts - Get account information, balances, positions, activities, executions, and orders
  • Market Data - Search symbols, get quotes, candles (OHLC), option chains, and market info

Refer to the Questrade API documentation for detailed endpoint information.

Features

  • Full TypeScript support with comprehensive type definitions
  • 🔄 Automatic token rotation - Questrade refresh tokens are automatically updated
  • 🔐 Token persistence - Save rotated tokens automatically via callbacks
  • 🧪 Comprehensive test coverage with unit and integration tests
  • 📦 Zero dependencies (except Zod for validation)
  • Built with Bun for maximum performance

Prerequisites

Before you can use this SDK, you need:

  1. Questrade Account - Create one at questrade.com
  2. API Key - Register your application at apphub.questrade.com to get your refresh token

Installation

bun add kest-trade-sdk

Quick Start

import { QuestradeClient } from 'kest-trade-sdk'

// Simple initialization - tokens are saved to .env automatically!
const client = new QuestradeClient({
  refreshToken: process.env.QUESTRADE_REFRESH_TOKEN,
})

await client.initialize()

// Get accounts
const accounts = await client.accounts.getAccounts()
console.log(accounts)

// Get market quotes
const quotes = await client.market.getQuotes([8049]) // AAPL symbol ID
console.log(quotes)

⚠️ Important: Token Rotation

Questrade rotates refresh tokens on every use. When you exchange a refresh token for an access token, you receive a new refresh token. The old one becomes invalid immediately.

The SDK handles this automatically:

  • Updates the internal token state
  • Saves the new token to .env file (default)
  • Calls your custom onTokenRefresh callback (if provided)

Important: You must persist the new refresh token, otherwise you'll need to manually generate a new one from the Questrade dashboard.

Client Configuration

Simple Usage (Recommended)

const client = new QuestradeClient({
  refreshToken: process.env.QUESTRADE_REFRESH_TOKEN,
})
await client.initialize()

Advanced Configuration

const client = new QuestradeClient({
  refreshToken: 'your_refresh_token',

  // Optional: Custom token persistence
  onTokenRefresh: async (token) => {
    await db.saveRefreshToken(token.refresh_token)
  },

  // Optional: Auto-refresh before expiry (default: true)
  autoRefresh: true,
  refreshBuffer: 60,
})
await client.initialize()

Examples

For practical usage patterns, see the examples/ folder:

Development

# Install dependencies
bun install

# Run unit tests
bun run test:unit

# Run integration tests (requires valid QUESTRADE_REFRESH_TOKEN in .env)
bun run test:integration

# Run all tests
bun test

# Lint and format
bun run lint:fix

# Build
bun run build

Testing

The SDK includes comprehensive test coverage:

  • Unit tests: Mock-based tests for all functionality
  • Integration tests: Real API tests (require valid token)

To run integration tests, create a .env file:

QUESTRADE_REFRESH_TOKEN=your_token_here

Questrade API Reference documentation

TO-DO

  • Add authorization methods
  • Add account calls methods
  • Add market calls methods
  • Improve security practices for token storage
  • Add Streaming API support
  • Add buy/sell order methods

License

BSD licensed. See the LICENSE file for details.

About

A TypeScript SDK for the Questrade API. Powered by Bun

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published