Skip to main content
This guide walks you through launching a complete self-serve billing flow: free trials with credits, hosted checkout for upgrades, usage-based billing, and a customer portal for account management.

What You’ll Build


Prerequisites

Before starting:
  1. API Key: Get your API key from Settings → API Keys
  2. Webhook Endpoint: Have a URL ready to receive webhooks
  3. Checkout Flow: Create a checkout flow in Settings → Hosted Pages → Checkout Flows

Step 1: Create the Customer

When a user signs up in your application, create a corresponding customer in Alguna. Endpoint: POST /customers
name and currency are required. Put your own identifier in aliases so you can address the customer by it later. Response:
Store the Alguna id, or address the customer by the alias you set. Either works wherever a customer is referenced.

Step 2: Grant Trial Credits

Give new users credits to try your product before subscribing. Credits are granted against a subscription, so create the subscription first — a free or trial plan is enough. Endpoint: POST /subscriptions/{id}/credits/grant
amount and reason are required. Set credit_type to units for a usage allowance, or monetary for a currency balance. Add expires_at if the grant should lapse.

Check the Balance

Endpoint: GET /subscriptions/{id}/credits/balance

Gate Work Before You Do It

To check whether a customer can afford an action, and hold the credit while the work runs, use the realtime gate: Endpoint: POST /credits/check
The response carries allowed, the priced charges per metric, and a reservation_id for the hold. When the work finishes, settle it with POST /credits/track, passing the reservation_id and close: true. See Credit Consumption.

Step 3: Handle Credit Depletion

When trial credits run out, Alguna sends a webhook so you can prompt the user to upgrade.

Configure Webhook

  1. Navigate to Settings → Webhooks
  2. Add your endpoint URL
  3. Subscribe to account.credits.balance_depleted

Webhook Payload

When you receive this webhook, redirect the user to your upgrade page or directly to hosted checkout.

Step 4: Send Users to Checkout

When a user is ready to subscribe, create a checkout session and redirect them to it. Endpoint: POST /checkout-sessions
Response:
Redirect the user to url. For a guest checkout, omit customer_id and pass a customer object instead; the customer is created when checkout completes. To render checkout inside your own page rather than redirecting, see Embedded Checkout.

Step 5: Handle Checkout Completion

When a user completes checkout, Alguna sends a webhook confirming the subscription.

Webhook Payload

Webhook payloads are camelCase, unlike API request and response bodies. Match on clientReferenceId to find the record on your side. When you receive this:
  1. Update the user’s status in your application
  2. Grant access to paid features
  3. Optionally send a welcome email

Step 6: Send Usage Events

Track usage by sending events to Alguna. Endpoint: POST /events
Send up to 100 events per request. The response lists which were ingested and which failed.
For trial users, add a property such as "is_trial": true so you can separate trial from paid usage in your metrics.

Usage Aggregation

Alguna can aggregate your events in different ways: Configure aggregation in Billable Metrics in the dashboard.

Step 7: Add the Customer Portal

Let users manage their subscription, view invoices and update payment methods. Endpoint: POST /customer-portal-sessions
Response:
Sessions are single-use and short-lived, so create one when the user clicks “Manage billing” rather than storing the link.

Portal Configuration

Each setting falls back to your organization’s default when omitted.
Set allow_subscription_cancel to false for enterprise customers who should contact sales to cancel.

Complete Integration Flow


Key Webhooks

Subscribe to these webhooks in Settings → Webhooks:

What You Don’t Need to Store

Alguna handles these for you:
  • Subscription IDs and status
  • Product and plan details
  • Usage calculations and overages
  • Invoice generation and payment processing
You only need to store the Alguna account ID (or use your internal ID as an alias).

Testing

  1. Use your Sandbox environment for testing
  2. Create test accounts and grant credits
  3. Deplete credits by sending test events
  4. Complete a test checkout
  5. Verify webhooks are received

Next Steps

Billable Metrics

Define how usage is measured and billed.

Customer Portal

Full portal configuration options.

Webhooks

Complete webhook reference.

API Reference

Full API documentation.