What You’ll Build
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 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:
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
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
- Navigate to Settings → Webhooks
- Add your endpoint URL
- Subscribe to
account.credits.balance_depleted
Webhook Payload
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
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
clientReferenceId to find the record on your side.
When you receive this:
- 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. Endpoint:POST /events
Send up to 100 events per request. The response lists which were
ingested and which failed.
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
Portal Configuration
Each setting falls back to your organization’s default when omitted.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
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.