Managing budgets with the API
The /budget routes are how scripts, signup services and infrastructure code manage shared budgets without the Admin UI. A shared budget is a named set of spend and rate limits that end users, tags, organizations and projects point at by budget_id. This page walks through the whole lifecycle in order: create a budget, look it up, list and inspect every budget, change it, and delete it. Every call needs a database, and with audit logging on, a Spanway Enterprise feature, every change writes a row to the audit log.
A real-world scenario
A company's signup service creates a customer on the gateway for every new account. Rather than send each customer's limits, the service refers to a budget named after the plan, so the platform team keeps the plans in one place. When a plan changes, an automation job calls /budget/update once. Each night a reconciliation job calls /budget/list and /budget/info to check that the plans on the gateway still match the pricing page, and when a plan is retired the job deletes its budget after moving the last customers to another one.
Who can call the routes
Every route takes a virtual key in the Authorization header. Creating, changing and deleting a budget needs a proxy admin key. Listing and inspecting budgets is allowed for proxy admins and admin viewers. An internal user or other plain key gets 401 Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. ..., and an admin viewer that tries to create, change or delete a budget gets 403 user not allowed to access this route, role= proxy_admin_viewer. ....
1. Create a budget
curl -X POST http://localhost:4000/budget/new \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"budget_id": "free-tier", "max_budget": 5, "soft_budget": 4, "budget_duration": "30d", "rpm_limit": 20}'
Expected result: the budget as stored. budget_reset_at is the end of the first 30 days from now, and created_by is the user behind the key.
{
"budget_id": "free-tier",
"max_budget": 5.0,
"soft_budget": 4.0,
"max_parallel_requests": null,
"tpm_limit": null,
"rpm_limit": 20,
"tpd_limit": null,
"model_max_budget": null,
"budget_duration": "30d",
"budget_reset_at": "2026-12-21T00:00:00Z",
"created_at": "2026-11-21T09:30:00Z",
"created_by": "admin",
"updated_at": "2026-11-21T09:30:00Z",
"updated_by": "admin"
}
Leave budget_id out to have one generated, and read it from the answer. The fields are described on shared budgets. In the Admin UI the same action is Budgets, then Create Budget.
2. Look budgets up by id
curl -X POST http://localhost:4000/budget/info \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"budgets": ["free-tier", "pro-tier"]}'
Expected result: an array with one entry per budget that exists. Ids that do not exist are left out rather than reported, so compare the answer with what you asked for.
3. List every budget
curl http://localhost:4000/budget/list -H "Authorization: Bearer $SPANWAY_API_KEY"
Expected result: every budget as one array, in the shape shown above. This is what the Budgets page shows in its table. The list also holds the budget rows the gateway creates by itself, for an organization's limits and for a team member's limits, which have generated ids. Changing or deleting one of those changes that organization's or member's limits, so edit them from the organization or team instead. For a paged, sorted and filtered list use GET /management/v1/budgets, which takes page_size, a sort such as -max_budget and filters such as filter[budget_duration][in]=7d,30d.
4. Inspect the editable fields of one budget
curl "http://localhost:4000/budget/settings?budget_id=free-tier" -H "Authorization: Bearer $SPANWAY_API_KEY"
Expected result: one entry per editable field with its type, description, current value and default, which is what the edit form in the Admin UI is built from.
[
{
"field_name": "max_budget",
"field_type": "Float",
"field_description": "Requests will fail if this budget (in USD) is exceeded.",
"field_value": 5.0,
"stored_in_db": true,
"field_default_value": null
}
]
5. Change a budget
curl -X POST http://localhost:4000/budget/update \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"budget_id": "free-tier", "max_budget": 8}'
Expected result: the budget with max_budget now 8.0 and every other field unchanged. Every end user, tag, organization or project attached to free-tier is held to 8 dollars from its next request on.
Only the fields you send change. Sending a new budget_duration moves budget_reset_at to the end of the first new period, unless you send budget_reset_at yourself. Sending "budget_duration": null clears the reset time, so the budget never resets. In the Admin UI the same action is the row's edit action on the Budgets page, then Save.
6. Delete a budget
Move the objects that use the budget to another one first, with POST /customer/update, POST /tag/update, PATCH /organization/update or POST /project/update and a new budget_id, because a customer, tag or project whose budget is deleted keeps its spend but loses the shared limits. An organization must always have a budget, so deleting one that an organization uses is refused with 400 Budget <id> is the budget of organization <org>. Give the organization another budget first, then delete this one.
curl -X POST http://localhost:4000/budget/delete \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"id": "free-tier"}'
Expected result: the row that was removed, in the same shape as the create answer. An id that does not exist answers 404 Budget with id '<id>' not found, and nothing is written to the audit log. In the Admin UI the same action is the row's delete action, which opens Delete Budget? with the budget's id and limits for you to confirm.
Routes
| Route | Who | What |
|---|---|---|
POST /budget/new | proxy admin | Creates a budget from the fields on shared budgets. |
POST /budget/update | proxy admin | Changes the fields sent for budget_id. |
POST /budget/info | proxy admin, admin viewer | {"budgets": ["<budget_id>", ...]} returns those budgets. |
GET /budget/list | proxy admin, admin viewer | Every budget as one array. |
GET /management/v1/budgets | proxy admin, admin viewer | Budgets paged, sorted and filtered. |
GET /budget/settings | proxy admin, admin viewer | The editable fields of one budget with their current values and descriptions. |
POST /budget/delete | proxy admin | {"id": "<budget_id>"} deletes a budget. |
Errors
| Status | Route | Meaning | Fix |
|---|---|---|---|
400 Budget with id 'free-tier' already exists. | /budget/new | The id is taken. | Use another id, or /budget/update. |
400 max_budget must be a non-negative finite number (or soft_budget). | /budget/new, /budget/update | The amount is negative, infinite or not a number. | Send 0 or more. |
400 Invalid budget_duration '<value>'. ... | /budget/new, /budget/update | The duration is not a valid period, for example 1hr, or never advances. | Use a number followed by s, m, h, d, w or mo, or hourly, daily, weekly or monthly. |
400 naming model_max_budget. | /budget/new, /budget/update | A per model entry has an invalid limit. | Give each model max_budget, budget_duration, tpm_limit or rpm_limit with valid values. |
400 budget_id is required. | /budget/update | The body did not name the budget. | Send budget_id. |
400 Specify list of budget id's to query. | /budget/info | budgets is empty. | Send at least one id. |
400 Admin-only endpoint. Not allowed to access this., your role=.... | /budget/list, /budget/settings, /budget/delete | The key is not a proxy admin (or admin viewer for the read routes). | Use an admin key. |
401 Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. ... | all | The key belongs to an internal user or another role that may not manage budgets. | Use a proxy admin key, or an admin viewer key for the read routes. |
403 user not allowed to access this route, role= proxy_admin_viewer. Trying to access: <route> | /budget/new, /budget/update, /budget/delete | Admin viewers can only read. | Use a proxy admin key. |
404 Budget with id '<id>' not found | /budget/update, /budget/delete | No budget has that id, and nothing changed. | Check the id with /budget/list. |
400 Budget <id> is the budget of organization <org>. ... | /budget/delete | An organization uses the budget. | Give the organization another budget first. |
400 DB not connected. This endpoint needs a database; ... | /budget/settings, /budget/list | The gateway has no database. | Set DATABASE_URL. |
500 DB not connected. This endpoint needs a database; ... | /budget/new, /budget/update, /budget/info, /budget/delete | The gateway has no database. | Set DATABASE_URL. |
| 422 | all | The body has a field of the wrong type, for example "max_budget": "five". | Send numbers as numbers. |
The error reference lists the same messages.
Logs
| Level | Line | Meaning | Action |
|---|---|---|---|
| WARNING | Failed to evict the team member default budget cache | /budget/update saved the change, but a cached copy used for team members was not cleared, so they may see the old limit for a short while. | Check Redis. |
| WARNING | Unrecognized budget_duration '<value>'; falling back to a next-midnight reset. ... | A budget stored before validation existed has a period the gateway cannot read. | Update the budget with a valid budget_duration. |
Related
- Shared budgets for what a budget is, the fields and how to attach one.
- Budgets page for the same actions in the Admin UI.
- End users for the customers that budgets are most often attached to.
- Team budgets for budgets set directly on keys, teams, tags and organizations.
- Audit logs for the rows budget changes write when audit logging is on.