/Bit2B

Primary LanguageTypeScript

Bit2B

Bit2B is an npm package that provides a convenient interface to integrate with Bitrefill's B2B API. It includes functionalities for product listing, invoice creation, balance retrieval, and more, making it easy for businesses to interact with Bitrefill's services.

Features

  • Ping API: Check if the API is reachable.
  • Product Listing: Fetch products with optional filters and pagination.
  • Invoice Management: Create invoices with various payment methods, including balance and cryptocurrency.
  • Balance Retrieval: Get the current balance of your Bitrefill account.
  • Invoice Payment: Automatically pay invoices using the account balance.

Installation

Install the package using npm:

npm install bit2b

Usage

Initialization

First, you need to initialize the Bit2B class with your API base URL and API key.

import Bit2B from "bit2b";

const bit2b = new Bit2B("https://api.bitrefill.com", "your_api_key_here");

Ping the API

You can check if the API is reachable using the ping method.

const pingResponse = await bit2b.ping();
console.log(pingResponse);

Fetch Products

Retrieve a list of products, with optional filters for pagination and other criteria.

const products = await bit2b.getProducts({
  limit: 50,
  start: 0,
  include_test_products: false,
});
console.log(products);

You can also fetch all products without pagination:

const allProducts = await bit2b.getAllProductsWithoutPagination({
  include_test_products: false,
});
console.log(allProducts);

Get Account Balance

Retrieve the current balance in your Bitrefill account.

const balance = await bit2b.getBalance();
console.log(balance);

Create an Invoice

Create an invoice for a product purchase. You can specify payment methods such as balance or cryptocurrency.

const invoice = await bit2b.createInvoice({
  products: [
    {
      product_id: "some_product_id",
      value: 10,
      quantity: 1,
    },
  ],
  payment_method: "balance", // or 'bitcoin'
});
console.log(invoice);

Automatic Invoice Payment

Automatically pay an invoice using the balance in your account.

const autoPaidInvoice = await bit2b.createInvoiceWithAutomaticPayment({
  products: [
    {
      product_id: "some_product_id",
      value: 10,
      quantity: 1,
    },
  ],
});
console.log(autoPaidInvoice);

Trigger Balance Payment for an Invoice

Trigger the payment of an invoice using your account balance.

const paidInvoice = await bit2b.triggerBalanceInvoicePayment("invoice_id");
console.log(paidInvoice);

Poll Invoice Status

Poll the status of an invoice until it reaches a final state or times out.

const invoiceStatus = await bit2b.awaitForInvoiceStatusCompletion("invoice_id");
console.log(invoiceStatus);

Error Handling

The package includes a custom error handler to manage errors during API interactions. You can use this in your Express app:

import { handleError } from "bit2b/errorHandler";

app.use((err, req, res, next) => {
  handleError(err, res);
});

Types

The package includes TypeScript types for all major operations, such as ApiResponse, ProductFilters, InvoicePayload, and more.

License

This package is licensed under the MIT License. See the LICENSE file for more details.

Demo Application

A demo application is available in the apps/api directory, showcasing how to integrate and use the Bit2B library within an Express.js app. This demo serves as a practical example to help you get started quickly.