Kalshi TypeScript Client

3.13.0 · active · verified Sun Apr 19

This package provides a TypeScript/JavaScript client for interacting with the Kalshi API, automatically generated from an OpenAPI specification. It leverages `axios` for HTTP requests and is currently at version 3.13.0. The client supports both Node.js and browser environments, as well as ES5 and ES6 language levels, and CommonJS and ES6 module systems. As a generated client, its release cadence typically follows updates to the underlying Kalshi OpenAPI specification, meaning the client API surface can change with new spec versions. A key differentiator is its comprehensive type definitions, enabling robust development in TypeScript projects and providing compile-time safety for API interactions. It primarily targets the `https://api.elections.kalshi.com/trade-api/v2` endpoint.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes the Kalshi client with an API key and fetches a list of existing API keys, demonstrating basic authentication and API interaction.

import { Configuration, ApiKeysApi } from 'kalshi-typescript';

// Ensure your Kalshi API key is set as an environment variable
// For production, use secure secret management best practices.
const KALSHI_API_KEY = process.env.KALSHI_API_KEY ?? '';

if (!KALSHI_API_KEY) {
  console.error('KALSHI_API_KEY environment variable is not set.');
  process.exit(1);
}

// Initialize the Configuration object with your API key and base path.
const config = new Configuration({
  apiKey: KALSHI_API_KEY,
  basePath: 'https://api.elections.kalshi.com/trade-api/v2' // As specified in the README
});

// Create an instance of the API client you want to use.
const apiKeysApi = new ApiKeysApi(config);

async function listApiKeys() {
  try {
    console.log('Attempting to fetch Kalshi API keys...');
    const response = await apiKeysApi.getApiKeys();
    console.log('Successfully retrieved API keys.');

    if (response.data && response.data.api_keys && response.data.api_keys.length > 0) {
      response.data.api_keys.forEach(key => {
        console.log(`  - Key ID: ${key.api_key_id}, Name: ${key.name}, Created At: ${new Date(key.created_at * 1000).toLocaleString()}`);
      });
    } else {
      console.log('No API keys found for this account.');
    }
  } catch (error: any) {
    console.error('Error fetching API keys:', error.message);
    if (error.response) {
      console.error('Response data:', error.response.data);
      console.error('Response status:', error.response.status);
    }
  }
}

listApiKeys();

view raw JSON →