API_KEY secret is not available on deployed app

My app uses an api that requires an api key, which has to be secret.
While developing my app, I did
export API_KEY=...
and loaded it in config.exs:
config :app_name, :api_key, System.get_env("API_KEY")
to be used later in code via:
Application.get_env(:app_name, :api_key)

After deploying the app via fly.io, I’ve run
fly secrets set API_KEY=...
and I can see it after running
fly secrets list

But the api stopped working on the deployed app complaining on the absence of the api key. And when logging Application.get_env(:app_name, :api_key) it returns empty string indeed.

Please advise how to resolve this issue.

@Wannabe hi!

You need to put those at at runtime.exs since config.exs is compiled at build time and at that point secrets are not available.

The problem is, when I have it in runtime.exs, then it’s not available when I run the app locally. I am not sure why…

I’d often see a bit of code duplication between runtime.exs and config/dev.exs/test.exs for example. Phoenix does that for example for database env variables.

Im not sure if there’s a better way but Id be happy to hear from anyone else too

Ok, having it in both config and runtime helped.
Thanks!

1 Like