Reference / Server API
UsageRail server API
The server API lets your backend manage customers and keys directly: create a customer when someone signs up on your site, give them a key, read their usage, move them to a free plan, or sign them in to the portal. It's for calls from your server, never from a browser.
curl https://api.usagerail.com/v1/consumers \
-H "Authorization: Bearer $USAGERAIL_MANAGEMENT_KEY" \
-H "Idempotency-Key: signup-8f14e45f" \
-H "Content-Type: application/json" \
-d '{"external_id": "user_1042", "email": "ada@example.com"}'Authentication
Every call needs the app's server API key, which starts with urm_. Create it on the app's page in the console, under Server API key. It's separate from the app key the SDK uses, and like the app key it's shown once. After a rotation, the old one keeps working for 24 hours.
Send it as a bearer token. A wrong key gets a 401 invalid_management_key, and an address that keeps sending wrong keys gets a 429 for a while.
Idempotency
Every POST and DELETE needs an Idempotency-Key header of up to 255 characters, such as an ID from your own database. UsageRail keeps each key for 24 hours.
- Sending the same request again with the same key returns the first response, with an
Idempotent-Replayed: trueheader, and does nothing new. - The same key with a different method, path or body gets a 422
idempotency_key_reused. - A request that failed with a 500 leaves nothing behind, so you can retry it with the same key.
- A new API key and a portal link appear only in the first response. A replay returns the same IDs and expiry time without them; use a new idempotency key to get a new one.
Customers
Every endpoint that returns a customer returns this object:
{
"consumer": {
"id": "cons_7Qm2xR9s",
"email": "ada@example.com",
"external_id": "4f9c1d0e7b6a...",
"status": "active",
"email_verified": false,
"created_at": "2026-10-21T09:14:03.120Z",
"plan": { "id": "plan_free", "name": "Free", "version": 1 },
"quota": { "behavior": "stop", "limit": "1000" },
"rate_limits": { "per_second": 2, "per_minute": null },
"current_period": {
"period_start": "2026-10-01T00:00:00.000Z",
"period_end": "2026-11-01T00:00:00.000Z",
"billable_requests": "37",
"billable_units": "37"
},
"balance": null,
"active_keys": 1,
"subscription": null
}
}Counts and limits are strings, so large numbers survive any JSON parser. external_id is stored hashed when your app hashes IDs, which is the default, so you get the hash back rather than the ID you sent. plan is null for a customer without a plan, and balance is only set on prepaid plans.
subscription is set while the customer pays for a plan through your Stripe account:
{
"status": "active",
"current_period_end": "2026-11-21T09:14:03.000Z",
"pending_change": { "kind": "change", "plan_id": "plan_basic", "interval": "month", "effective_at": "2026-11-21T09:14:03.000Z" }
}status is active, trialing or past_due (a payment failed and Stripe is retrying). pending_change is a change waiting for the end of the period, or null. Its kind is change or cancel; for a cancellation, plan_id is the free plan the customer chose, or null for your default plan. A customer who was on a prepaid plan before subscribing and still has unexpired credits goes back to that prepaid plan instead.
Create a customer
POST /v1/consumers with an optional external_id (your own ID for the customer, up to 256 characters) and an optional email.
The new customer starts on the app's default plan and the response is a 201. With an external_id that already belongs to a customer, you get that customer back with a 200 and nothing changes; use claim to add an email to an existing customer.
| Status | error | Why |
|---|---|---|
| 400 | invalid_external_id, invalid_email, invalid_json | The body isn't valid. |
| 403 | new_consumer_blocked | The app isn't taking new customers right now. |
| 409 | consumer_exists | The email belongs to another customer. |
| 409 | consumer_deleted | The external_id belongs to a deleted customer. |
Get a customer
GET /v1/consumers/{id} returns the customer, or a 404 not_found if there's no such customer in this app.
Claim an email or external ID
POST /v1/consumers/{id}/claim with exactly one of email or external_id attaches it to an existing customer. Use it to connect a customer who signed up in the portal to your own user ID, or to give a customer an email so they can sign in to the portal.
| Status | error | Why |
|---|---|---|
| 200 | Attached, or the customer already had that exact value. | |
| 400 | claim_one_identifier | Send exactly one of the two fields. |
| 409 | already_claimed | The customer already has a different value for that field. |
| 409 | consumer_exists | Another customer has that value. |
| 409 | consumer_pending_deletion | The customer is being deleted. |
Keys
Create a key
POST /v1/consumers/{id}/keys with an optional name of up to 100 characters. The response is a 201:
{
"key_id": "PRjuGinc",
"consumer_id": "cons_7Qm2xR9s",
"name": "Production",
"key": "acme_live_PRjuGincAHq68hICyifjdBmNASZ1Fw9mwPOgy8pt0EJiLE"
}key appears in this response only. Errors: 404 not_found, 409 consumer_not_active for a suspended customer, 409 key_limit_reached when the customer or the app has no room for another key, and 409 app_read_only while the app isn't taking new keys.
Revoke a key
DELETE /v1/keys/{key_id} revokes the key at once; running SDKs stop accepting it within about 20 seconds. The response is {"key_id": "PRjuGinc", "status": "revoked"}, or a 404 if there's no active key with that ID.
Usage
GET /v1/consumers/{id}/usage?days=7 returns the customer's usage for the last 1 to 30 days, 7 by default, counted in calendar days in the app's time zone:
{
"days": 7,
"retention_days": 30,
"timezone": "America/New_York",
"unit_mode": "requests",
"daily": [
{ "day": "2026-10-21", "requests": "41", "billable_requests": "37", "billable_units": "37" }
],
"endpoints": [
{ "method": "GET", "route": "/v1/items/:id", "requests": "41", "billable_requests": "37", "billable_units": "37" }
]
}daily has one entry per day, including days without usage. endpoints lists the 20 busiest routes. requests counts every request, including refused ones; the billable fields count only the ones that are billed. If your plan keeps fewer days than you asked for, days says how many you got.
Plans
POST /v1/consumers/{id}/plan with a plan_id changes the customer's plan. Running SDKs pick up the new quota and rate limits within about 20 seconds of the change taking effect.
For a customer without a subscription, only free plans work, and the change is immediate. A plan with a price returns a 409 paid_plan_needs_checkout: customers move onto paid plans by paying, through checkout below or in your portal. Other errors: 404 plan_not_found or not_found, 409 consumer_not_active, and 409 subscription_managed_in_stripe for a customer whose plan comes from an existing Stripe subscription: change that one in Stripe.
For a subscribed customer, the change follows the same rules as in the portal (plans and checkout explains them), in order:
| Change | Takes effect |
|---|---|
| To a plan with a higher monthly or yearly fee on the same billing interval, or the same fee with at least as many included units, where neither plan bills overage | Now. Stripe charges the difference for the rest of the period, and the plan changes once that payment goes through. |
| Any other paid plan, including plans that bill overage and switches between monthly and yearly | At the end of the period |
| A free plan | At the end of the period. The subscription ends and the customer moves to that plan. |
"plan_id": null | At the end of the period. The subscription ends and the customer moves to your default plan. |
When a subscription ends, a customer who was on a prepaid plan before subscribing and still has unexpired credits goes back to that prepaid plan, whatever free plan was chosen. If they can't go back to a prepaid plan and have no free plan to move to, their API requests are refused.
Add "interval": "month" or "year" to pick the billing interval; without it, the customer keeps their current one when the plan offers it. To drop the change that's waiting, send { "keep": true }, or the plan and interval the customer is already on. Only one change waits at a time: a new one replaces it. An upgrade waiting for its payment counts too. Dropping or replacing it voids the Stripe invoice for the difference, so its payment link stops working. The answer has the customer and a change:
{ "consumer": { "id": "cons_7Qm2xR9s", "subscription": { "status": "active", "pending_change": null } }, "change": { "status": "changed" } }change.status is changed (an upgrade that went through), payment_required (Stripe couldn't take the difference; payment_url is a Stripe page where the customer can pay it, and the plan changes once they do), scheduled (with effective_at), or kept (the waiting change was dropped). The same idempotency rules apply as for checkout below. Errors: 409 subscription_past_due while a payment is failing (cancelling still works), 409 plan_change_not_supported for a prepaid plan, 409 already_on_plan when there's nothing to change, and 400 interval_not_offered.
Checkout
POST /v1/consumers/{id}/checkout opens a Stripe Checkout page for a paid plan, in your own Stripe account. Send it to the customer; once they pay, they move onto the plan, with its quota and rate limits.
{
"plan_id": "plan_8ZtQ2wLm",
"interval": "month",
"success_url": "https://example.com/billing/done",
"cancel_url": "https://example.com/billing"
}interval is month (the default) or year, if the plan has a yearly price. Plans that bill overage are monthly only. Both URLs must use https. The answer is { "url": "https://checkout.stripe.com/..." }. Asking again with a new Idempotency-Key returns the same page if it's for the same plan terms and interval and has more than 10 minutes left; otherwise the old page is closed and a new one opened.
Send an Idempotency-Key as with other writes. While a request with the same key is still running, another one gets a 409 idempotency_request_in_progress. A 502 stripe_unreachable isn't stored, so you can retry with the same key.
Hidden plans work here too. Errors: 409 stripe_not_connected until you connect Stripe, 409 already_subscribed if the customer already pays for a plan, 409 subscription_managed_in_stripe if their plan comes from an existing Stripe subscription, 409 prepaid_not_available or plan_is_free, 400 interval_not_offered, and 404 plan_not_found or not_found.
To sell a credit pack instead, send credit_pack_id with the two URLs. A customer who isn't on a prepaid plan yet also needs plan_id, the prepaid plan they move to once they've paid. The credits are added when Stripe confirms the payment; for a bank payment that clears later, that's when it clears. Errors: 404 credit_pack_not_found for a pack that isn't for sale, 400 prepaid_plan_required, 409 not_a_prepaid_plan, and 409 plan_change_not_supported while the customer has a subscription.
Adjust a balance
POST /v1/consumers/{id}/credits/adjust adds or removes credits by hand, with a reason that goes in your audit log:
{ "units": 500, "reason": "Credit for the outage on October 20" }Positive units add credits that never expire and pay off any debt first. Negative units come out of the credits that expire soonest, and whatever is left over becomes debt. The Idempotency-Key is what stops a retry from adjusting twice. The answer is the customer.
Portal sign-in links
POST /v1/consumers/{id}/portal-link returns a link that signs the customer in to your portal, with an optional next path to open after sign-in, such as /keys:
{
"url": "https://acme.usagerail.app/auth/callback?token=...",
"expires_at": "2026-10-21T09:29:03.120Z"
}The link works once and for 15 minutes. It opens a page with a Sign in button, so a link previewed by an email scanner isn't used up.
| Status | error | Why |
|---|---|---|
| 409 | consumer_has_no_email | The portal signs people in by email. Claim an email first. |
| 409 | consumer_not_active | The customer is suspended. |
| 409 | portal_unavailable | The portal isn't available right now. |
Errors
Errors are JSON with an error field. Besides the ones listed with each endpoint, any call can get a 401 invalid_management_key, a 404 not_found for a customer or key that isn't in this app, and a 410 app_deleted once the app is being deleted.