Create Metered Session
Create a new metered usage session to start tracking consumption for a customer. Sessions track usage in real-time and automatically deduct costs from the customer's balance.
Endpoint
POST
/v1/metered-billing/sessionsAuthorization: Bearer <ORVION_API_KEY> or X-API-Key: <ORVION_API_KEY>
Content-Type: application/json
Request Body
| Field | Type | Required | Description | Example |
|---|---|---|---|---|
| customer_ref | string | Required | Your internal customer identifier (e.g., user ID, email, API key) | user_123 |
| pricing | object | Required | Pricing configuration for this session | {"currency": "USD", "unit": "second", "unit_price": "0.0025"} |
| pricing.currency | string | Required | ISO currency code (3 characters) | USD |
| pricing.unit | string | Required | Unit of measurement (currently only 'second' is supported) | second |
| pricing.unit_price | string | Required | Price per unit as a decimal string | 0.0025 |
| cap | object | null | Optional | Optional maximum amount to charge for this session | {"amount": "50.00"} |
| cap.amount | string | Optional | Maximum charge amount as a decimal string | 50.00 |
| resource_ref | string | null | Optional | Reference to the resource being consumed (e.g., VPS ID, API endpoint) | vps:server_456 |
| metadata | object | null | Optional | Free-form JSON object stored with the session | {"vps_id": "server_456", "region": "us-east-1"} |
| idempotency_key | string | null | Optional | Unique key to prevent duplicate session creation | session_abc123_20250115 |
Response
| Field | Type | Required | Description | Example |
|---|---|---|---|---|
| id | string | Optional | Unique session ID | sess_abc123 |
| status | string | Optional | Session status: 'active', 'stopped', or 'settled' | active |
| customer_ref | string | Optional | Customer reference | user_123 |
| pricing | object | Optional | Pricing configuration | {"currency": "USD", "unit": "second", "unit_price": "0.0025"} |
| cap | object | null | Optional | Usage cap if set | {"amount": "50.00"} |
| usage | object | Optional | Current usage statistics | {"total_seconds": 0, "total_amount": "0.00"} |
| started_at | string | Optional | ISO 8601 timestamp when session started | 2025-01-15T10:00:00Z |
| created_at | string | Optional | ISO 8601 timestamp when session was created | 2025-01-15T10:00:00Z |
Example: cURL
cURL
curl -X POST "https://api.orvion.sh/v1/metered-billing/sessions" \-H "Authorization: Bearer $ORVION_API_KEY" \-H "Content-Type: application/json" \-d '{"customer_ref": "user_123","pricing": {"currency": "USD","unit": "second","unit_price": "0.0025"},"cap": {"amount": "50.00"},"resource_ref": "vps:server_456","metadata": {"vps_id": "server_456","region": "us-east-1"}}'
Example: Python SDK
Python
from orvion import OrvionClientfrom orvion.usage import UsagePricing, UsageCap# Initialize clientorvion = OrvionClient(api_key="your_api_key")# Create sessionsession = await orvion.usage.create_session(customer_id="user_123",pricing=UsagePricing(currency="USD",unit="second",unit_price="0.0025"),cap=UsageCap(amount="50.00"),resource_ref="vps:server_456",metadata={"vps_id": "server_456","region": "us-east-1"})print(f"Session created: {session.id}")print(f"Status: {session.status}")print(f"Pricing: ${session.pricing.unit_price}/second")
Example: Node.js SDK
JavaScript
import { OrvionClient } from '@orvion/sdk';// Initialize clientconst orvion = new OrvionClient({ apiKey: 'your_api_key' });// Create sessionconst session = await orvion.usage.createSession({customerId: 'user_123',pricing: {currency: 'USD',unit: 'second',unitPrice: '0.0025'},cap: {amount: '50.00'},resourceRef: 'vps:server_456',metadata: {vps_id: 'server_456',region: 'us-east-1'}});console.log(`Session created: ${session.id}`);console.log(`Status: ${session.status}`);console.log(`Pricing: $${session.pricing.unitPrice}/second`);
Response Example
{
"id": "sess_abc123",
"status": "active",
"customer_ref": "user_123",
"resource_ref": "vps:server_456",
"pricing": {
"currency": "USD",
"unit": "second",
"unit_price": "0.0025"
},
"cap": {
"amount": "50.00"
},
"usage": {
"total_seconds": 0,
"total_amount": "0.00"
},
"last_tick_at": null,
"settled_amount": null,
"invoice_id": null,
"metadata": {
"vps_id": "server_456",
"region": "us-east-1"
},
"started_at": "2025-01-15T10:00:00Z",
"stopped_at": null,
"settled_at": null,
"created_at": "2025-01-15T10:00:00Z"
}
Important Notes
Balance Requirement
Before creating a session, ensure the customer has sufficient balance. You can:
- Top up the balance via
POST /v1/metered-billing/balances/top-up - Check balance via
GET /v1/metered-billing/balances
If a session is created without sufficient balance, usage ticks will fail with insufficient_balance: true.
Idempotency
Use idempotency_key to prevent duplicate sessions if your request is retried. The same key will return the same session.
Pricing
- Currency: Must be a valid ISO currency code (e.g., USD, EUR)
- Unit: Currently only
"second"is supported - Unit Price: Must be a decimal string (e.g.,
"0.0025"for $0.0025/second)
Usage Caps
Set a cap.amount to limit the maximum charge for a session. Once the cap is reached:
- The session status changes to
stopped - No further ticks will be accepted
- You can still settle the session
Error Responses
400 Bad Request
{
"detail": "Invalid pricing configuration"
}
401 Unauthorized
{
"detail": "Invalid API key"
}
402 Payment Required
{
"detail": "Insufficient balance"
}
Next Steps
- Report Usage Ticks - Start tracking consumption
- Manage Balances - Top up customer balances
- Settle Sessions - Generate invoices from usage