On this page
TypeScript SDK
Official TypeScript SDK for the Bitcoin API, providing full type safety and a lightweight client.
The official TypeScript SDK for the Bitcoin API is built on top of the lightweight openapi-fetch HTTP client and provides complete type safety for all requests and responses, automatically generated from our OpenAPI schema.
Installation
You can install the SDK via npm, yarn, or pnpm:
npm install @bitcoin-api-net/sdk
Initialization
To get started, import the createBitcoinClient function and create a client instance. You can pass a base URL and an API key for authenticated requests.
import { createBitcoinClient } from '@bitcoin-api-net/sdk';
const client = createBitcoinClient({
// Optional: Your API key to access protected endpoints (e.g., Rate-limit boosts)
apiKey: 'your_api_key_here',
// Optional: Override the base URL (defaults to https://bitcoin.net/)
// baseUrl: 'https://api.bitcoin.net/'
});
Examples
Fetching Current Price
The SDK uses HTTP methods (GET, POST, PUT, DELETE, etc.) as functions. Paths and parameters are strictly typed.
async function fetchCurrentPrice() {
const { data, error, response } = await client.GET('/api/v1/prices/current', {
params: {
query: {
symbol: 'btcusdt' // Strictly typed (only 'btcusdt' is allowed currently)
}
}
});
if (error) {
console.error('Error fetching price:', error);
return;
}
console.log(`Current price: ${data.price} (time: ${data.time})`);
}
fetchCurrentPrice();
Fetching Klines (Candles)
async function fetchCandles() {
const { data, error } = await client.GET('/api/v1/prices/candles', {
params: {
query: {
symbol: 'btcusdt',
interval: '1h', // '1m', '5m', '15m', '1h', '1d', etc.
limit: 10
}
}
});
if (data) {
console.log(`Received ${data.klines.length} candles.`);
}
}
Typings
The SDK automatically exports all types generated from the OpenAPI schema. You can import them directly if you need to describe variable types in your application:
import type { paths, components } from '@bitcoin-api-net/sdk';
// Example: Extracting the Kline type
type Kline = components['schemas']['Kline']; // (Depends on your exact OpenAPI schema structure)
Features
- Zero dependencies (except for
openapi-fetchitself). - Automatic Header Injection: If an
apiKeyis provided during initialization, the SDK automatically attaches theAuthorization: Bearer <apiKey>header to all requests. - Type Safety: Your IDE will suggest available paths, required query/body parameters, and the response structure.
- Promise-based: Modern API using async/await.