wasp deploy to fly.io not picking up secrets as env variables
I have added an environment variable in the fly.io console but my app isn't able to read it. This is a fresh deployment of a new app to fly.io. I tried an additional wasp deploy fly deploy after setting the secret and it's still not getting picked up.
5 Replies
Hi @wardbox. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The issue you're experiencing is likely due to how environment variables are handled in Wasp deployments to Fly.io, especially the distinction between server and client environment variables.
Key points:
- Server environment variables (such as secrets) must be set using the Fly.io CLI (
flyctl secrets set ...). These are available to your server code via process.env.VAR_NAME after you redeploy your server. Setting them in the Fly.io dashboard should also work, but you must ensure you redeploy the server after setting them so the new environment is picked up. If you set a secret and then run wasp deploy fly deploy, it should be available to your server code on the next deployment. If it's not, double-check the variable name and that you're accessing it from the server code, not the client code. See the docs for more: Defining Env Vars in Production.
- Client environment variables (those you want to access in your React code) must be embedded at build time and must be prefixed with REACT_APP_. These cannot be set via Fly.io secrets or dashboard after deployment. Instead, you need to provide them when building the client, e.g.:
or, if using the Wasp CLI deploy flow:
This ensures the variable is embedded into the client bundle and accessible via import.meta.env.REACT_APP_SOME_VAR_NAME in your client code. Setting these as Fly.io secrets will not make them available to the client code, as they are not injected at runtime but at build time. This is a common source of confusion—see the discussion here.
Summary of what to check:
- For server-side secrets: Set them with flyctl secrets set ..., then redeploy.
- For client-side env vars: Set them as part of the build command, not as Fly.io secrets.
- Make sure you are accessing the variable from the correct context (server vs. client).
If you follow these steps and still have issues, double-check the spelling of your environment variable and ensure you are accessing it correctly in your code. If you need to update a secret, set it again and redeploy your app.
For more details, see:
- Wasp Env Vars documentation
- Defining Env Vars in Production
- Discord thread on this issue
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @wardbox, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?@kapa.ai it is a server side secret. I double checked that the value is set up and the name of the secret is right. this is only happening with one secret. I added discord oauth client id and secret and those are working. Seems to be more of a fly.io issue
Let me check the knowledge base for you again. One moment! :books:
----------------------
If your Discord OAuth client ID and secret are working but one specific server-side secret is not being picked up, despite being set correctly in Fly.io, this suggests the issue may not be with Wasp's general handling of secrets but could be related to:
1. How the secret is being accessed in your code: Double-check that you are using the correct environment variable name in your server code (e.g.,
process.env.YOUR_SECRET_NAME). Typos or case mismatches can cause the variable to be undefined.
2. Deployment timing: Ensure you have redeployed your app after setting the secret. Environment variables are only available to the app after a new deployment or restart.
3. Fly.io secret propagation: Sometimes, Fly.io may require a short delay or a manual app restart after setting a new secret for it to be available to the running app. You can check which secrets are set using flyctl secrets list to confirm the secret is present and correctly named Set Up a Fly.io App.
4. Secret value formatting: If the secret contains special characters, ensure it is properly quoted or escaped when setting it via the CLI.
Since your other secrets are working, this is unlikely to be a Wasp-specific bug. The knowledge sources do not mention any known Wasp issues with individual secrets not being picked up if others are working. If the problem persists after checking the above, it may be a Fly.io-specific issue with that particular secret, and you might want to try deleting and re-adding the secret, or reaching out to Fly.io support.
If you need to confirm that the secret is available, you can add a temporary log statement in your server code:
Just be careful not to log sensitive values in production logs.
If you continue to experience issues, please provide more details about the secret name and how you are accessing it in your code, as the knowledge sources do not provide further troubleshooting steps for this specific scenario.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @wardbox, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?idk what happened, but manually doing the
flyctl secrets set seems to have made it workHm, that's odd 😄 it might be a couple of things:
- secrets were "staged" to be set after the next deployment
- Fly was being flaky
tried an additional wasp deploy fly deploy after setting the secret and it's still not getting picked up.This tells me that it might have been the second thing, I'm not sure. Let me know if you encounter this again and I'll try to reproduce it 🙂