Charge customers based on what they use
1. Before each operation -> check access
2. After each operation -> record usage
3. End of period -> Accend invoices automatically
- TypeScript
- Python
- Go
- cURL
const access = await accend.access.check({
customerId: "cus_123",
meterId: "meter_123",
quantity: 1
});
if (!access.data.allowed) {
return { error: "upgrade_required", paymentUrl: access.data.paymentUrl };
}
await accend.usage.createEvent({
customerId: "cus_123",
meterId: "meter_123",
quantity: 1,
idempotencyKey: `req_${requestId}`
});
access = accend.access.check({
"customerId": "cus_123",
"meterId": "meter_123",
"quantity": 1,
})
if not access["data"]["allowed"]:
raise RuntimeError("upgrade_required")
accend.usage.create_event({
"customerId": "cus_123",
"meterId": "meter_123",
"quantity": 1,
"idempotencyKey": f"req_{request_id}",
})
access, _ := client.CheckAccess(ctx, map[string]any{
"customerId": "cus_123",
"meterId": "meter_123",
"quantity": 1,
})
allowed := access["data"].(map[string]any)["allowed"].(bool)
if !allowed {
return
}
client.CreateUsageEvent(ctx, map[string]any{
"customerId": "cus_123",
"meterId": "meter_123",
"quantity": 1,
"idempotencyKey": "req_123",
})
curl -X POST "https://api.accend.now/v1/usage/events" \
-H "Authorization: Bearer $ACCEND_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{"customerId":"cus_123","meterId":"meter_123","quantity":1,"idempotencyKey":"req_123"}'
Always send an
idempotencyKey when recording usage. Retries should not double-count.