I am trying to load an API key as a system env from my mac when starting up the phoenix server. What am I getting wrong? these are my steps:
On my mac terminal:
export API_NOTIFICATION_KEY=1234
in my config.exs
config :app, App.Notifications,
notification_api_key: {:system, "API_NOTIFICATION_KEY"}
in my module where I use it
@api_notification_key Application.get_env(:app, App.Notifications)[:notification_api_key]
start my phoenix server
mix phx.server
And then When I try to make the API call it is showing as nil. Is there a step I am missing to get it properly loaded ?
Attributes are evaluated during compilation, so:
@api_notification_key Application.get_env(:app, App.Notifications)[:notification_api_key]
will have its value set at compile time. I assume that's not what you want, so you'll be better off using a function:
defp api_notification_key() do
case Application.get_env(:test, App.Notifications)[:notification_api_key] do
{:system, var_name} -> System.get_env(var_name)
value -> value
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With