10 months ago
How can I set an environment variable / secret (like OPENAI_API_KEY) using the CLI?
In my current workflow I deploy the app on railway as a project and then set the environment variable via the web interface. I'd like to do this last step via the CLI so that it just builds and works from the start.
Do I have to use something like railway run export OPENAI_API_KEY=...
or something like that?
16 Replies
10 months ago
Hey Gautam,
Going to need more info so that I can give you an appropriate answer -
In my current workflow I deploy the app on railway as a project
Do you mean you deploy a template from the CLI?
Additionally, Would you mind if I made this thread public as I'm not seeing any sensitive information and the topic being discussed could be helpful to other users.
Status changed to Awaiting User Response railway[bot] • 10 months ago
10 months ago
Hi Brody,
I deploy the app from the command line like this:
railway init -n <app-name>
railway up -c
railway domain
railway link ...
railway volume add -m /app/data
Hope that answers your question(?)
Sure, you can make this public.
Status changed to Awaiting Railway Response railway[bot] • 10 months ago
10 months ago
This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.
Status changed to Open brody • 10 months ago
10 months ago
Yes it does, Does this Project also exist in GitHub? If so you could make a whole template from it, it would be much faster to deploy from the CLI that way and you can pass in environment variables.
10 months ago
Thanks Brody.
Yes it does exist on Github. Ok, I will follow the template route. Could you point me to the docs that might help me? I found this: https://docs.railway.app/guides/create
10 months ago
Thats exactly what I would have linked! you are on the correct path.
Let ne know if you need any help with the template.
9 months ago
I have the same exact question as it happens. I'd rather not convert to a template, since I use CLI to deploy all my projects. So I'm looking for a way to set variables from the CLI.
9 months ago
That is not something the CLI supports right now, but we would be happy to accept a PR that adds that functionality.
9 months ago
Got it. Could you outline the general approach needed for such a PR? Is there something in the graphql API already that would support this?
9 months ago
Yes the public API does support this -
https://docs.railway.app/reference/public-api
As for the PR, implement the feature and make a PR, nothing much to that.
9 months ago
I gave up on the template route too since I want to stick to using the CLI.
I doubt I'll manage to make a PR because I don't know Rust.
I guess this guide is relevant: https://docs.railway.app/guides/manage-variables
9 months ago
Yep you would have to use the API if using a template / dashboard is out of the question for you.
8 months ago
Ok, Claude helped me get this working. This is what works for fetching the variables:
curl --request POST \
--url https://backboard.railway.app/graphql/v2 \
--header 'Authorization: Bearer <API_TOKEN_GOES_HERE>' \
--header 'Content-Type: application/json' \
--data '{
"query": "query variables($projectId: String!, $environmentId: String!) { variables(projectId: $projectId, environmentId: $environmentId) }",
"variables": {
"projectId": "8df3b1d6-2317-4400-b267-56c4a42eed06",
"environmentId": "9fb4baf0-809a-40ec-af32-751f50890802"
}
}'
8 months ago
Sorry that it's so inconvenient to copy-paste but for the life of me I couldn't figure out how to format that!!!
8 months ago
Triple backticks followed by the language (bash) and then press enter.
curl -I "https://www.google.com"
8 months ago
Ok, and this is how you can set a variable (without serviceID):
curl --request POST \
--url https://backboard.railway.app/graphql/v2 \
--header 'Authorization: Bearer <API_TOKEN>' \
--header 'Content-Type: application/json' \
--data '{
"query": "mutation variableUpsert { variableUpsert(input: { projectId: \"8df3b1d6-2317-4400-b267-56c4a42eed06\", environmentId: \"9fb4baf0-809a-40ec-af32-751f50890802\", name: \"NEW_VARIABLE\", value: \"SECRET_VALUE\" }) }"}'
Hope this helps everyone trying to do it with the CLI.
Status changed to Solved brody • 8 months ago