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.Documentation Index
Fetch the complete documentation index at: https://alguna.com/docs/llms.txt
Use this file to discover all available pages before exploring further.
What You’ll Build
| Phase | What Happens |
|---|---|
| Trial | User signs up → Create account → Grant trial credits |
| Upgrade | Credits depleted → Redirect to hosted checkout → User subscribes |
| Ongoing | Send usage events → Alguna bills automatically |
| Portal | User manages subscription, invoices, and payment methods |
Prerequisites
Before starting:- API Key: Get your API key from Settings → API Keys
- Webhook Endpoint: Have a URL ready to receive webhooks
- Checkout Flow: Create a checkout flow in Settings → Hosted Pages → Checkout Flows
Step 1: Create Customer Account
When a user signs up in your application, create a corresponding account in Alguna. Endpoint:POST /accounts
Step 2: Grant Trial Credits
Give new users trial credits to try your product before subscribing. Endpoint:POST /accounts/{id}/credits/grants
Check Credit Balance
Endpoint:GET /accounts/{accountId}/credits/balance?type=units
Optional: Check Credit Availability
Before performing an action, verify the user has sufficient credits. Endpoint:GET /accounts/{accountId}/credits/availability?type=units&required=10
Step 3: Handle Credit Depletion
When trial credits run out, Alguna sends a webhook so you can prompt the user to upgrade.Configure Webhook
- Navigate to Settings → Webhooks
- Add your endpoint URL
- Subscribe to
account.credits.balance_depleted
Webhook Payload
Step 4: Redirect to Hosted Checkout
When users are ready to subscribe, redirect them to Alguna’s hosted checkout.Option A: Simple Redirect (No Code)
Redirect users directly to your checkout flow URL:| Parameter | Description |
|---|---|
flowId | Your checkout flow ID from the dashboard |
accountId | The Alguna account ID |
pt | Optional pass-through token for verification |
Option B: Server-Side Session (More Control)
Create a checkout session via API for more flexibility. Endpoint:POST /customer-session/checkout
url.
Step 5: Handle Checkout Completion
When a user completes checkout, Alguna sends a webhook confirming the subscription.Webhook Payload
- Update the user’s status in your application
- Grant access to paid features
- Optionally send a welcome email
Step 6: Send Usage Events
Track usage by sending events to Alguna. Events are processed within 15 minutes. Endpoint:POST /events
| Field | Required | Description |
|---|---|---|
account | Yes | Alguna account ID or your internal ID (alias) |
eventName | Yes | The type of event (must match your billable metric) |
timestamp | Yes | When the event occurred |
uniqueId | No | For idempotency (prevents duplicate processing) |
properties | No | Custom data for filtering and aggregation |
Usage Aggregation
Alguna can aggregate your events in different ways:| Aggregation | Use Case |
|---|---|
| Count | Count every event (e.g., API calls) |
| Count Unique | Count unique values (e.g., unique users) |
| Sum | Sum a property value (e.g., total minutes) |
| Max | Maximum value in period |
Step 7: Add Customer Portal
Let users manage their subscription, view invoices, and update payment methods. Endpoint:POST /portal/customer-session/customer-portal
sessionUrl when they click “Manage Billing” in your app.
Portal Configuration
| Setting | Default | Description |
|---|---|---|
planOverviewSettings.showSection | true | Show subscription details |
planOverviewSettings.allowSubscriptionCancel | true | Allow self-service cancellation |
invoiceListSettings.showSection | true | Show invoice history |
billingDetailsSettings.showSection | true | Show billing info and payment methods |
Complete Integration Flow
Key Webhooks
Subscribe to these webhooks in Settings → Webhooks:| Event | When It Fires |
|---|---|
checkout.session.completed | User completes payment |
account.credits.balance_depleted | Trial credits hit zero |
subscription.created | New subscription starts |
invoice.paid | Invoice payment succeeds |
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
Testing
- Use your Sandbox environment for testing
- Create test accounts and grant credits
- Deplete credits by sending test events
- Complete a test checkout
- 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.