Skip to main content
React hooks for billing.io. Built on top of @billing-io/sdk with built-in state management, auto-polling, and TypeScript support.

npm

@billing-io/react

GitHub

billing-io/sdk-react

Installation

npm install @billing-io/react @billing-io/sdk
@billing-io/sdk and react >= 18 are peer dependencies.

Setup

Wrap your app with BillingProvider:
import { BillingProvider } from "@billing-io/react";

function App() {
  return (
    <BillingProvider apiKey="sk_live_...">
      <YourApp />
    </BillingProvider>
  );
}

Create a Checkout

import { useCreateCheckout } from "@billing-io/react";

function CheckoutButton() {
  const { createCheckout, data, loading, error } = useCreateCheckout();

  const handleClick = async () => {
    await createCheckout({
      amount_usd: 49.99,
      chain: "tron",
      token: "USDT",
    });
  };

  return (
    <div>
      <button onClick={handleClick} disabled={loading}>
        {loading ? "Creating..." : "Pay $49.99"}
      </button>
      {data && <p>Send payment to: {data.deposit_address}</p>}
      {error && <p>Error: {error.message}</p>}
    </div>
  );
}

Poll Checkout Status

useCheckoutStatus automatically polls at the interval returned by the API:
import { useCheckoutStatus } from "@billing-io/react";

function CheckoutTracker({ checkoutId }: { checkoutId: string }) {
  const { data, loading } = useCheckoutStatus(checkoutId);

  if (loading) return <p>Loading...</p>;

  return (
    <div>
      <p>Status: {data.status}</p>
      <p>
        Confirmations: {data.confirmations}/{data.required_confirmations}
      </p>
    </div>
  );
}

List Checkouts

import { useCheckouts } from "@billing-io/react";

function CheckoutList() {
  const { data, loading, error } = useCheckouts({ limit: 10 });

  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error: {error.message}</p>;

  return (
    <ul>
      {data.map((checkout) => (
        <li key={checkout.checkout_id}>
          {checkout.checkout_id} - {checkout.status}
        </li>
      ))}
    </ul>
  );
}

Access the SDK Client Directly

Use useBillingClient for operations not covered by hooks:
import { useBillingClient } from "@billing-io/react";

function CustomComponent() {
  const billing = useBillingClient();

  const handleAction = async () => {
    const invoice = await billing.invoices.create({
      /* ... */
    });
  };

  return <button onClick={handleAction}>Create Invoice</button>;
}

Customers

import { useCustomers, useCreateCustomer } from "@billing-io/react";

function CustomerList() {
  const { data, loading } = useCustomers({ limit: 20 });
  const { createCustomer } = useCreateCustomer();

  return (
    <div>
      <button onClick={() => createCustomer({ email: "alice@example.com", name: "Alice" })}>
        Add Customer
      </button>
      {data?.map((c) => <p key={c.id}>{c.name} - {c.email}</p>)}
    </div>
  );
}

Subscriptions

import { useSubscriptions, useCreateSubscription } from "@billing-io/react";

function SubscriptionManager() {
  const { data: subs, loading } = useSubscriptions({ status: "active" });
  const { createSubscription } = useCreateSubscription();

  const handleSubscribe = () => {
    createSubscription({
      customer_id: "cus_abc123",
      plan_id: "plan_abc123",
      payment_method_id: "pm_abc123",
    });
  };

  if (loading) return <p>Loading...</p>;

  return (
    <div>
      <button onClick={handleSubscribe}>Subscribe</button>
      {subs?.map((sub) => (
        <p key={sub.id}>{sub.customer_id} - {sub.status}</p>
      ))}
    </div>
  );
}

Payouts

import { usePayouts, useCreatePayout, useExecutePayout } from "@billing-io/react";

function PayoutDashboard() {
  const { data: payouts } = usePayouts({ limit: 10 });
  const { createPayout } = useCreatePayout();
  const { executePayout } = useExecutePayout();

  return (
    <div>
      {payouts?.map((p) => (
        <div key={p.id}>
          {p.amount} {p.token} - {p.status}
          {p.status === "approved" && (
            <button onClick={() => executePayout(p.id)}>
              Submit TX
            </button>
          )}
        </div>
      ))}
    </div>
  );
}

Available Hooks

HookDescription
Checkouts
useCreateCheckoutCreate a checkout with loading/error state
useCheckoutStatusPoll checkout status with auto-refresh
useCheckoutsList checkouts with pagination
Customers
useCustomersList customers
useCustomerGet a single customer by ID
useCreateCustomerCreate a new customer
useUpdateCustomerUpdate a customer
Payment Methods
usePaymentMethodsList payment methods
useCreatePaymentMethodCreate a payment method
useUpdatePaymentMethodUpdate a payment method
useDeletePaymentMethodDelete a payment method
useSetDefaultPaymentMethodSet default payment method
Payment Links
usePaymentLinksList payment links
useCreatePaymentLinkCreate a payment link
Subscriptions
useSubscriptionsList subscriptions
useCreateSubscriptionCreate a subscription
useUpdateSubscriptionUpdate/cancel a subscription
useSubscriptionPlansList plans
useCreateSubscriptionPlanCreate a plan
useUpdateSubscriptionPlanUpdate/archive a plan
useRenewalsList subscription renewals
useRetryRenewalRetry a failed renewal
Entitlements
useEntitlementsList entitlements
useCreateEntitlementCreate an entitlement
useUpdateEntitlementUpdate an entitlement
useDeleteEntitlementDelete an entitlement
useCheckEntitlementCheck feature access for a customer
Payouts
usePayoutsList payout intents
useCreatePayoutCreate a payout intent
useUpdatePayoutUpdate a payout intent
useExecutePayoutSubmit tx hash for execution
useSettlementsList settlements
Revenue
useRevenueEventsList revenue events
useAccountingGet accounting summary
useAdjustmentsList adjustments
useCreateAdjustmentCreate an adjustment
Other
useWebhookEndpointsManage webhook endpoints
useEventsList billing events
useBillingClientAccess the raw SDK client