Environments and Git promotion
Environments let you keep the config people build in the gateway in your own Git repository and promote it between gateways the way you promote infrastructure code. You export a bundle of files, commit it, review the pull request, plan it against the target gateway, have a second person approve the plan and then apply it. The gateway never holds Git credentials and gateways never talk to each other, so Git, review and CI stay in your own tools.
Environments need a Spanway enterprise license that includes the environments feature. Promoting custom agents also needs the custom_agents feature, saved workflows need workflow_definitions (and workflow_subworkflows when one calls another) and approval policies need human_approval. An export that does not name its kinds leaves out the kinds your license lacks, and naming one, or planning a bundle that lists one, answers 403.
The commands below use the env group of the spanwayctl CLI, which lists every option and exit code. The CLI ships inside the gateway image, and the CLI reference shows how to run it from there.
Scenario
A team builds and tests custom agents, saved workflows and triggers on a staging gateway. Production must get the same objects, reviewed in a pull request, with no secrets in Git and no one pasting JSON by hand. The staging admin exports a bundle, commits it, CI plans it against production on every pull request, a second person approves the plan in the production gateway, and CI applies it after merge. Nothing reaches production that was not planned, reviewed and approved.
Steps
-
Name each gateway with
general_settings.environments.name, for examplestagingandprod, and on production setrequire_approval: true. -
On staging, in the Admin UI open Workflow Runs under Agentic, switch to the Environments tab, click Show this scope and then Export bundle, or export from the API.
curl -X POST http://localhost:4000/v1/environments/export \-H "Authorization: Bearer sk-staging-admin" -H "Content-Type: application/json" \-d '{"kinds": ["CustomAgent", "WorkflowDefinition", "ApprovalPolicy"]}' -
Commit the bundle and plan it against production. On the production gateway's Environments tab pick the file under Bundle file to plan (JSON from an export) and click Plan against prod, or let CI do it with a deploy key.
curl -X POST https://gateway.example.com/v1/environments/plan \-H "Authorization: Bearer $SPANWAY_DEPLOY_KEY" -H "Content-Type: application/json" \-d '{"bundle": '"$(cat spanway/bundle.json)"', "target_environment": "prod", "prune": false, "source": {"commit": "'"$GIT_SHA"'", "ref": "origin/main", "path": "spanway/bundle.json"}}' -
A second person opens Plans, clicks Review plan ..., reads every change and problem, and clicks Approve plan, or calls
POST /v1/environments/plans/approvewith theplan_idand a note. The plan list shows the first eight characters of each id, and the review panel shows the full id with a Copy button. The API and the CLI need the full id. -
Apply the approved plan with Apply plan, or from CI with
POST /v1/environments/plans/apply. The CLI wraps the same calls asspanwayctl env export,plan,approveandapply.
Expected result
The plan call answers 200 with the plan, its changes and whether it can be applied.
{
"summary": {"plan_id": "6f1c0e2a-9b7d-4c3e-8a21-5d4f3b2a1c09", "scope_key": "gateway", "environment_name": "prod", "status": "planned", "bundle_digest": "sha256:...", "prune": false, "requires_approval": true, "appliable": true, "counts": {"create": 2, "update": 1, "unchanged": 4, "delete": 0}, "source": {"commit": "9f3c...", "ref": "origin/main", "path": "spanway/bundle.json"}, "created_by_user_id": "ci-deploy"},
"changes": [{"kind": "CustomAgent", "name": "release-notes-writer", "action": "create", "drifted": false, "changes": [], "problems": []}],
"secret_refs": [{"ref": "os.environ/TICKETS_TOKEN", "resolvable": true}],
"result": []
}
After approve and apply the plan's status is applied, the Apply result lists each object with its outcome, and Managed objects shows what the apply wrote. A later plan of the same bundle reports no changes.
What a bundle holds
A bundle is one YAML file per object plus a spanway-env.yaml manifest that lists the kinds the repository owns.
| Kind | Name in the bundle | What it carries | Folder |
|---|---|---|---|
Toolset | the toolset name | description and tools, each as an MCP server name and a tool name | toolsets/ |
Policy | the policy name | the production version's guardrails, inheritance, condition and pipeline, plus its full set of attachments | policies/ |
RoutingPreset | the preset slug | a global routing preset's models, provider preferences and parameters | routing_presets/ |
RouterSettings | global | the router settings that can change at runtime, such as retries, fallbacks, aliases, weights and tag routing | router_settings/ |
CustomAgent | a per-environment name | everything a custom agent has except ids, owner, revision and timestamps, with its toolset, write tools, skills and team referenced by name | agents/ |
WorkflowDefinition | a per-environment name | a team or org saved workflow with its display name, description, team by name and every version, where a step's custom_agent_id holds the custom agent's bundle name and a sub-workflow step's workflow_id holds the called workflow's bundle name | workflows/ |
ApprovalPolicy | a per-environment name | an approval policy with its scope, match and approvers, where teams, organizations, MCP servers and custom agents are named instead of given by id | approval_policies/ |
api_version: spanway.ai/env/v1
kind: CustomAgent
name: researcher-3f9a1c2b
spec:
display_name: Researcher
instructions: Read the ticket and draft a summary.
model: claude-sonnet
toolset: repo-read
write_tools: []
limits: {max_steps: 10, max_run_budget: 1.0, max_wall_seconds: 300}
visibility: team
owner_team: platform
A bundle never holds ids, owners' user ids, spend, run history, keys or secrets. Objects refer to each other and to MCP servers, teams and skills by name, so the same files work on a gateway where every id is different. MCP servers, model deployments, credentials, keys, teams, users, budgets and prompts are not promoted. Create them in the target first, and the plan tells you about every one that is missing.
A custom agent's name in the bundle is the name it had when it was first exported. Applying it to another gateway creates a new agent with its own generated agent name, and that gateway remembers which agent the bundle name maps to, so later applies update the same agent. Private agents are never exported. Saved workflows and approval policies work the same way, and their bundle name is their display name in lower case with the first characters of their id, for example weekly-report-3f9a1c2b.
Saved workflow versions never change, so a plan only ever adds the versions the target does not have yet. A bundle that rewrites a saved version, leaves one out, repeats the version before it or changes a saved workflow's display name, description or who can see it is a problem on the plan. Halting is not promoted, so a halted workflow stays halted where it was halted. A step that calls an A2A agent cannot be exported, because A2A agents are registered per gateway. Private saved workflows are never exported, and saved workflows that call each other in a cycle are a problem on the plan.
An approval policy is written with proxy admin authority, because only a proxy admin can apply the gateway scope. Its approvers must sit inside its scope in the target, exactly as when you create it through its own route. A policy that names a person as an approver, at any escalation level, cannot be exported, so a bundle never holds a user id. Name the admins or members of a team instead. A workflow step match can only name custom agents, so a policy on an A2A agent cannot be exported.
Workflow triggers are not promoted. A webhook, schedule or event trigger runs as one person's virtual key and a webhook has a signing secret, and neither belongs in Git, so create triggers in each environment after the saved workflow they start is applied.
Two objects are refused instead of guessed. A name that matches more than one MCP server, team, skill or toolset in the target is a problem on the plan, and an object that cannot be exported as it is, such as a toolset that uses an MCP server without a name, fails the export with its path.
Secrets stay out of Git
Every field is typed, so a field the exporter does not know cannot ride along, and an unknown field in a file fails the plan with its path. Export and plan also scan every string. A secret-looking key with a literal value, a value encrypted by the gateway, a provider key, a cloud access key id, a private key or a bearer token refuses the whole export or plan with the JSON path of each finding and never echoes the value.
Where a field needs a secret, write an os.environ/NAME reference instead, which reads the target gateway's environment. The plan reports whether each reference resolves on the target, without reading the value into the response. Organization scopes cannot use these references at all, because they read the gateway's own environment.
Scopes
A plan writes to one scope.
- The gateway scope covers every kind and needs a proxy admin. A self-hosted environment is usually one gateway. Saved workflows and approval policies are only promoted in the gateway scope.
- An organization scope covers the team custom agents of that organization, only of the teams where the applier is a team admin, and the
orgcustom agents that belong to that organization. It never exports or changes anorgagent of another organization or one that belongs to none, and anorgagent it creates belongs to that organization. Passorganization_idto use it. Its environment name is the organization's alias, and only an admin of that organization or a proxy admin can plan, approve or apply in it. A plan remembers the teams its planner administers, and another organization admin can only read, approve, reject or apply it when they administer all of those teams too. Plans, managed objects and locks of different scopes never see each other.
The gateway's own environment name comes from the config file, and a plan must name it in target_environment, so a staging plan cannot land on prod through a wrong base URL.
general_settings:
environments:
name: prod
require_approval: true
plan_ttl_minutes: 60
Export
spanwayctl env export --out ./spanway
Export writes the files with sorted keys, LF line endings and a trailing newline, so the same state always gives the same bytes and Git diffs only show real changes. It removes the files of exported kinds that the gateway no longer has. Every export is recorded with who ran it and what it held, and it is in the audit log. Treat the repository as sensitive, because it holds agent instructions and policy content.
Plan
spanwayctl env plan --dir ./spanway --env prod
A plan compares each object with the target and marks it create, update, unchanged or, with --prune, delete. It runs the same checks as the feature's own create and update routes with your identity, so a team admin's plan that enables a custom agent write tool fails exactly like the UI would refuse it. An object someone changed in the target since the last apply is marked as drifted. An object of the same name that you cannot change is a problem that does not say who owns it. Prune only deletes objects an earlier apply created or last wrote, and only of kinds the manifest lists.
The plan is stored with the bundle and a digest of every object it touches in the target, expires after plan_ttl_minutes and supersedes the open plans of the same scope. The command exits 0 when nothing changes, 2 when the plan has changes and 1 when it has problems, and --git (on by default) records the commit and CI run on the plan.
--git records the commit of the repository that holds --dir, the bundle's path inside it and whether the bundle folder had uncommitted changes. The CLI warns when it did, and the plan shows it, so a reviewer can tell a plan of a reviewed commit from one of local edits. To plan exactly what a commit holds, pass --git-ref with a branch, tag or commit. The CLI then reads the manifest and object files of --dir from that commit with your local git, ignores the working tree and records the commit it resolved, so the plan matches the pull request byte for byte.
spanwayctl env plan --dir ./gateway/prod --env prod --git-ref origin/main
Only the manifest and the YAML files in the kind folders are read, so other files in the repository never reach the gateway. The gateway itself never talks to Git.
Approve
spanwayctl env approve <plan_id> --note "Reviewed in PR 42"
Approval is on by default. The approver must be a person signed in with their own key and cannot be the person whose key made the plan, even a proxy admin. A plan made by a key that belongs to no user can never be approved, so CI should plan with a deploy key owned by a user. Anyone with scope authority can reject a plan.
Apply
spanwayctl env apply <plan_id>
Apply writes exactly the planned bundle through each feature's own write path, so custom agent, toolset, policy and routing preset rules and cache refreshes are the same as through their own routes. Every kind gets its own audit row per object as the person who applied: custom agents, toolsets, policies, routing presets, router settings, saved workflows and approval policies. The toolset, policy, routing preset, saved workflow and approval policy rows also carry the plan id, and the toolset rows keep the tools before and after. The apply row of the plan lists every object with its outcome and id, so each change traces back to its plan even after retention deletes the plan itself. It is refused while the plan waits for approval, after it expires, when it has problems, when a secret reference it uses does not resolve on this gateway and when any object it touches changed since the plan, in which case you plan again.
A plan whose bundle names a secret reference, such as os.environ/TICKETS_TOKEN, that this gateway cannot resolve is saved so you can read it, with that reference shown as not resolvable and appliable false. Define the secret on the target gateway, then plan again.
One apply runs per scope at a time. The apply holds a lease with a fencing token that only ever grows. It renews the lease right before every write and records each write only under its own token, so an apply that stalls and loses its lease stops at its next object once another one took over, and the next apply marks a crashed one as failed. The first object that fails stops the apply and earlier writes stay. Every write makes the object equal to its spec, so planning and applying again converges. A policy and its attachments always change together in one transaction, as a new production version that keeps the version history.
Admin UI
The Environments tab of the Workflow Runs page under Agentic shows the environment's name, scope, whether a second person must approve and how long plans last, with a line under each that says what the setting does and where to change it. Proxy admins and admin viewers see the gateway scope, and an org admin types the id of an organization they administer to see its scope. Export bundle downloads the promotable config as one JSON bundle file, the same content the CLI writes as YAML files. Plan against takes that file or a pasted bundle, with an option to delete objects an earlier apply made that the bundle no longer has, and shows every problem the gateway names with its path.
Plans lists each plan with its status, how many objects it creates, changes and deletes, and who made it. Review plan opens the full plan id with a Copy button, the commit, ref and bundle path the plan came from, with a warning when the bundle had uncommitted changes, and every object change with the fields that change, drift since the last apply, problems and whether each secret reference resolves here. The Next step section says what can happen now. A second person approves the plan with an optional note, and the person who made it sees why they cannot. An approved plan, or one in an environment that does not require approval, can be applied, and the apply result lists every object with its outcome. Anyone with scope authority can reject an open plan. Managed objects lists what applies created or last wrote and whether someone changed or deleted it since. Read-only roles see plans and managed objects but cannot export, plan, approve, reject or apply.
CI deploy keys
A deploy key is an ordinary virtual key owned by an admin user with allowed_routes: ["environment_deploy_routes"]. That route set opens plan, apply and the reads, never approve, and the key cannot call models. A typical pipeline plans on every pull request and applies after merge once a second person approved the plan in the gateway.
- run: spanwayctl env plan --dir ./spanway --env prod --git-ref "$GITHUB_SHA"
env:
SPANWAY_PROXY_URL: https://gateway.example.com
SPANWAY_PROXY_API_KEY: ${{ secrets.SPANWAY_DEPLOY_KEY }}
Limits
| Limit | Value |
|---|---|
| Objects in one bundle | 500. An export of a scope with more objects answers 422, so export fewer kinds at a time. |
| Size of a plan request | 2 MiB (2,097,152 bytes), whether or not the client sends a Content-Length header. |
| Decision note | 1,000 characters. |
| Environment name | 128 characters. |
plan_ttl_minutes | 1 to 1,440 minutes (one day). |
| Plans listed | The newest 50 plans of the scope. |
Settings
| Setting | What it does | Default |
|---|---|---|
general_settings.environments.name | Names this gateway, for example prod. A plan must name it in target_environment, so a plan meant for staging cannot land here by mistake. Without a name, planning answers 409. | Not set |
general_settings.environments.require_approval | When on, a second person must approve every plan before it can be applied, so no one can push a change to this environment alone. When off, the person who planned can apply their own plan right away. Turn it off only on development gateways. | On |
general_settings.environments.plan_ttl_minutes | How long a plan stays valid, from 1 to 1,440 minutes. A short time keeps people from applying a plan reviewed long ago against a gateway that has since changed. | 60 |
Roles, retention and data subject requests
A custom role with environments.read opens the reads, and environments.write lets a role that is otherwise read-only reach export, plan, reject and apply. The grant never adds scope authority, so the caller still needs to be a proxy admin for the gateway scope or an admin of the organization for its scope. No grant opens approval, which stays with admins.
Settled plans and export records are deleted after maximum_environment_record_retention_period when you set it in the compliance settings. A plan that is being applied is kept. A data subject export lists the plans a user made, decided or applied and the exports they ran. Erasure replaces their user id with the pseudonym, clears their decision notes and rejects the open plans they made.
API
| Method | Path | Summary |
|---|---|---|
| GET | /v1/environments/info | Read this environment |
| GET | /v1/environments/plans/list | List plans |
| GET | /v1/environments/plans/info | Read one plan |
| GET | /v1/environments/managed/list | List the objects applies wrote |
| POST | /v1/environments/export | Export the scope's config as a bundle |
| POST | /v1/environments/plan | Plan a bundle against this environment |
| POST | /v1/environments/plans/approve | Approve a plan |
| POST | /v1/environments/plans/reject | Reject a plan |
| POST | /v1/environments/plans/apply | Apply an approved plan |
Errors
| Status | What it means | Fix |
|---|---|---|
| 401 | No valid key was sent. | Send a key in the Authorization header. |
403 environments license message | The license lacks the environments feature. | Add the feature to the license or ask sales@spanway.ai. |
403 "Promoting <Kind> objects needs a Spanway license that includes the <feature> feature" | The bundle lists a kind your license does not cover, for example custom agents. | Add the feature, or leave the kind out of the manifest. |
| 403 "Your role can read environment plans but not change them" | A view-only role tried to export, plan, approve, reject or apply. | Use a write role, or give the role environments.write. |
| 403 "Only proxy admins promote gateway-wide config. Organization admins pass organization_id" | A caller who is not a proxy admin used the gateway scope. | Pass organization_id for an organization you administer, or use a proxy admin key. |
| 403 "The master key can only use the gateway scope" | The master key was sent with an organization_id. | Drop organization_id, or use the key of an organization admin. |
403 "This scope cannot manage <Kind>" or "This scope cannot export <Kind>" | An organization scope named a kind only the gateway scope holds, such as toolsets or policies. | Use the gateway scope for those kinds. |
| 403 "Only a person signed in with their own key can approve a plan" | An agent key, the master key or a key with no user tried to approve. | A person approves with their own key. |
| 403 "The person who made a plan cannot approve it" | The planner tried to approve their own plan. | A second person approves. |
404 "Organization <id> not found" | The organization does not exist, or you do not administer it. | Enter an organization you administer. |
404 "Plan <id> not found" | The plan id is wrong, shortened or belongs to another scope. | Copy the full id from the plan's review panel. |
| 409 "This environment has no name. Set general_settings.environments.name on the gateway" | The gateway has no environment name, so it cannot check target_environment. | Set the name and restart. |
409 "This is environment '<name>', not '<target>'" | The plan names a different environment than this gateway, for example a staging plan sent to prod. | Send the plan to the right gateway, or fix target_environment. |
| 409 "This plan does not need approval, apply it directly" | Approval is off on this gateway. | Apply the plan. |
| 409 "The key that made this plan belongs to no user, so nobody can approve it. Plan again with a user's key" | CI planned with a key no user owns. | Plan again with a deploy key owned by a user. |
| 409 "These secret references do not resolve on this gateway: ..." | The bundle uses a secret this gateway does not have. | Define the secret on the target, then plan again. |
| 409 "This plan has problems, fix them and plan again" | The plan lists problems with their paths. | Fix them in the bundle and plan again. |
409 "The plan is <status>, so it cannot be ..." or "This plan is waiting for approval by a second person" | The plan expired, was rejected, applied or superseded, or still waits for approval. | Make a new plan, or wait for the approval. |
| 409 "The target changed since this plan (...), plan again" | Someone changed an object after the plan was made. | Plan again and review the new changes. |
| 409 "Another apply is running in this environment, try again when it finishes" or "The plan changed while you decided it, read it again" | Two people acted on the same scope at once. | Wait, read the plan again and retry. |
| 413 "A bundle may be at most 2097152 bytes" | The plan request is larger than 2 MiB. | Export fewer kinds, or split the bundle. |
422 {"detail": {"error": "...", "problems": [{"code", "message", "path"}]}} | The bundle holds an inline secret, uses a gateway environment reference in an organization scope, or an export holds an object that cannot be exported or more than 500 objects. | Fix each problem at its path, move secrets to references, or export fewer kinds. |
422 {"detail": [...]} | The request body failed validation, for example a field of the wrong type. | Fix the field the loc names. |
| 500 "DB not connected" | The gateway has no database. | Set DATABASE_URL and restart. |
The error reference lists the same errors next to the rest of the gateway's.
Logs
The feature logs one line of its own. Everything else it does shows up in the audit log.
| Level | Line | What it means | Action |
|---|---|---|---|
| DEBUG | environment secret reference could not be resolved: <ExceptionType> | While planning, a secret reference could not be read on this gateway. The plan shows the reference as not resolvable. Only the error type is logged, never the reference value. | Define the secret on the gateway, then plan again. |
Related
- Workflow Runs page describes the Environments tab.
- Custom agents, workflow definitions, approval policies, toolsets, policies and routing presets are the kinds a bundle holds.
- CLI documents
spanwayctl env. - Route allow-lists hold the
environment_deploy_routes,environment_read_routes,environment_write_routesandenvironment_approve_routessets. - Audit logs record every plan decision and apply.