Skip to content
HisarPay

Crypto payments in Node.js

A library written in TypeScript without runtime dependencies. Express, Fastify, Next.js or plain node:http: it runs wherever you do.

  • Node.js 18+
  • TypeScript
  • ESM · CommonJS

What it does

  • Requests and responses are fully typed; inside a switch on the webhook event, data narrows to its own type.
  • Uses Node’s own fetch, so nothing else comes along.
  • Ships as ESM and CommonJS.
  • Cancellation with AbortSignal, timeout and retries per request.
Creating an invoice from a terminal

Requirements

Node.js
18 or later
Module format
ESM and CommonJS
HisarPay
A test key (hp_test_…)

Setup

  1. Step 1

    Install

    Keep the key in an environment variable and out of code that reaches a browser.

    Terminal
    npm install https://hisarpay.com/downloads/hisarpay-node-1.0.0.tgz
  2. Step 2

    Open the invoice

    Send your order number as order_id. If the same number comes again, the existing invoice is returned instead of a new one.

    TypeScript
    import { HisarPay } from '@hisarpay/node';
    
    const hisarpay = new HisarPay(process.env.HISARPAY_API_KEY!);
    
    export async function startPayment(order: { id: string; total: string; title: string }) {
      const invoice = await hisarpay.invoices.create({
        price_usd: order.total,               // "49.90", a decimal string
        order_id: order.id,                   // the same order never gets a second invoice
        description: order.title,
        success_url: `https://shop.example/orders/${order.id}`,
      });
      return invoice.payment_url;             // redirect the customer here
    }
  3. Step 3

    Verify the webhook

    The signature is made over the raw body. In Express, define this route before express.json; in Next.js, request.text() is enough.

    import express from 'express';
    import { HisarPay, SignatureVerificationError } from '@hisarpay/node';
    
    const app = express();
    
    // the raw body: register this route before any express.json()
    app.post('/webhooks/hisarpay', express.raw({ type: 'application/json' }), async (req, res) => {
      let event;
      try {
        event = HisarPay.webhooks.constructEvent(
          req.body,
          req.headers['x-hisarpay-signature'],
          process.env.HISARPAY_WEBHOOK_SECRET!,
        );
      } catch (err) {
        if (err instanceof SignatureVerificationError) return res.sendStatus(400);
        throw err;
      }
      if (event.livemode && (event.type === 'invoice.paid' || event.type === 'invoice.overpaid')) {
        await markPaid(event.id, event.data.invoice.order_id);   // once per event.id
      }
      res.sendStatus(200);
    });
  4. Step 4

    Run the sample shop

    Download the example project from the top of this page: a small shop with three products that verifies the webhook and shows the state of an order. The library is inside; put your test key in .env and start it.

Client options

All optional; the defaults suit most shops.

baseUrl
Defaults to https://hisarpay.com. Point it at a local API during development.
timeoutMs
Timeout of one attempt in milliseconds. Defaults to 30000.
maxRetries
Retries after the first attempt (network errors, timeouts, 429, 5xx). Defaults to 2.
fetch
Your own fetch, for a proxy for instance.
idempotencyKey
Your own key for one POST request. Left out, the library makes one and keeps it across retries.

Resources

What you find on the client.

ResourceWhat it does
invoicesCreate, retrieve, look up by order, list, cancel, simulate in test mode
paymentLinksCreate, update and pause links, read their figures
withdrawalsWithdraw to a trusted address, follow its state
balancesBalances and the ledger
webhookEndpoints · eventsManage endpoints, read and resend events
reportsCSV, Excel and PDF reports

Frequently asked questions

Does it run on an edge runtime?
The library is written for Node.js and verifies signatures with node:crypto. Choose the Node runtime instead of edge.
Can I read the webhook with express.json and verify that?
No. JSON that was parsed and written again is not the same bytes, and the signature will not match. Use express.raw on this route.
How do I make a real signature in tests?
With HisarPay.webhooks.generateTestHeader, which gives a valid header for your own payload and secret.
Which licence does the library have?
MIT. Read the source and ship it with your project.

PHP

A Composer package that works with plain PHP, Laravel and Symfony. No framework needed.

  • PHP 8.1+
  • Composer
  • Laravel · Symfony

Python

Sync and async clients, samples for Flask, FastAPI and Django, type definitions included.

  • Python 3.9+
  • sync · async
  • Flask · FastAPI · Django

REST API

A predictable REST API for everything from invoices to withdrawals, with signed webhooks and a test mode.

  • REST
  • OpenAPI 3.1
  • Webhooks

Your first collection is an e-mail away

Sign-up is free; your test keys are ready the moment you confirm your e-mail.