Per-user quotas and billing
~10 min Python TypeScriptGoal: Cap each of your end-users so they cannot exhaust your MIOSA allocation, then pull a monthly usage rollup per user to generate accurate invoices.
What you’ll use: Quotas API, Usage API
Overview
Every sandbox created with an external_user_id is automatically attributed. Two APIs build on top of that:
- Quotas - hard caps per
external_user_id: max sandboxes, max concurrent, max credits. - Usage - period-based metering rollup queryable by
computer_id, filterable and paginated.
Before you start - attribution
Quotas and usage rollups key off external_user_id, the opaque identifier you stamp on each sandbox at create time. MIOSA propagates it to every usage record and audit event automatically, so there is no separate tagging step after create.
Attribution is covered end to end in Multi-tenant SaaS: tagging at create, filtering sandboxes by tenant, and per-tenant usage. This recipe picks up from there and focuses on caps and invoicing.
Step 1 - Set per-user quotas
Call client.quotas.set once during onboarding (or whenever a user upgrades their plan). Limits take effect immediately for all subsequent sandbox creates.
Step 2 - Check current quota state
Before creating a sandbox on behalf of a user, check whether they have headroom. This lets you surface a friendly upgrade prompt rather than a raw API error.
Step 3 - Monthly invoice rollup
At the end of each billing period, pull a usage report. The response gives you the data you need to generate per-user line items.
Connecting to Stripe (optional)
Once you have the per-user credit totals, map them to money and charge via Stripe:
What you learned
- Every sandbox created with
external_user_idis attributable - no extra tagging step required after create. client.quotas.set(user_id, {...})enforces hard caps per user. Call it on plan changes, not on every request.client.quotas.get(user_id)returns both the limits and current usage in a single call - use it for proactive upgrade prompts.client.usage.sessions()gives you the raw per-session metering rows you need to build an invoice rollup.- Connecting to Stripe is a thin adapter layer - MIOSA gives you the credits; you decide the USD rate.
Related
Full walkthrough of tagging, filtering, preview share tokens, and per-tenant usage in a SaaS context.
Listen for sandbox lifecycle events to trigger billing actions (e.g., credit deduction on destroy).
Full parameter reference and enforcement semantics for the Quotas API.