Manage Variables with the Public API
Here are examples to help you manage your environment variables using the Public API.
Get variables
Fetch variables for a service in an environment:
Response:
Omit serviceId to get shared variables for an environment instead.
Get unrendered variables
Get variables with references intact (not resolved):
This returns variables like ${{Postgres.DATABASE_URL}} instead of the resolved value.
Create or update a variable
Upsert a single variable:
Omit serviceId to create a shared variable instead.
Upsert multiple variables
Update multiple variables at once:
replace: true will delete all variables not included in the variables object.Delete a variable
Delete a single variable:
Get rendered variables for deployment
Get all variables as they would appear during a deployment (with all references resolved):
Variable references
Railway supports referencing variables from other services using the syntax:
For example, to reference a database URL from a Postgres service:
Common patterns
Copy variables between environments
- Fetch variables from source environment
- Upsert to target environment using
variableCollectionUpsert
Import from .env file
Parse your .env file and use variableCollectionUpsert to set all variables at once.
Rotate secrets
Use variableUpsert with skipDeploys: true for all services, then trigger deployments manually when ready.