GuidesSDKs

TypeScript SDK

Install and use the official halfin TypeScript SDK

The @halfin/sdk-merchant package provides typed methods for the halfin API and webhook signature verification.

Installation

pnpm add @halfin/sdk-merchant

Quick Start

import { createHalfin } from '@halfin/sdk-merchant';

const client = createHalfin({
  apiKey: process.env.HALFIN_API_KEY!,
  baseUrl: 'https://dashboard.halfin.xyz/api',
});

Create an Invoice

import { createInvoice } from '@halfin/sdk-merchant';

const { data } = await createInvoice({
  client,
  body: {
    currency: 'BTC',
    amount: '0.001',
    description: 'Order #1234',
    metadata: { order_id: '1234' },
  },
});

console.log(data.id);              // "00000000-..."
console.log(data.checkout_url);    // "https://checkout.halfin.xyz/..."

Other Methods

import {
  cancelInvoice,
  createAddress,
  getInvoice,
  getRates,
  listBalances,
  listInvoices,
} from '@halfin/sdk-merchant';

// List invoices
const invoices = await listInvoices({
  client,
  query: { status: 'paid', limit: 10 },
});

// Get a single invoice
const invoice = await getInvoice({
  client,
  path: { invoiceID: '00000000-0000-0000-0000-000000000001' },
});

// Cancel a pending invoice
await cancelInvoice({
  client,
  path: { invoiceID: '00000000-0000-0000-0000-000000000001' },
});

// Create a static address
const address = await createAddress({
  client,
  body: { currency: 'ETH', label: 'Account top-up' },
});

// List balances
const balances = await listBalances({ client });

// Get exchange rates
const rates = await getRates({ client });

Webhook Verification

import { verifySignature } from '@halfin/sdk-merchant';

const isValid = verifySignature({
  signature: req.headers['x-halfin-signature'] as string,
  body: rawBody,
  secret: process.env.HALFIN_WEBHOOK_SECRET!,
  toleranceSeconds: 300, // reject events older than 5 minutes
});