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.
- 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.
Install the package using npm:
npm install bit2b
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");
You can check if the API is reachable using the ping
method.
const pingResponse = await bit2b.ping();
console.log(pingResponse);
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);
Retrieve the current balance in your Bitrefill account.
const balance = await bit2b.getBalance();
console.log(balance);
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);
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 the payment of an invoice using your account balance.
const paidInvoice = await bit2b.triggerBalanceInvoicePayment("invoice_id");
console.log(paidInvoice);
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);
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);
});
The package includes TypeScript types for all major operations, such as ApiResponse
, ProductFilters
, InvoicePayload
, and more.
This package is licensed under the MIT License. See the LICENSE file for more details.
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.