Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir Phoenix not loading env variable at runtime

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:

  1. On my mac terminal:

    export API_NOTIFICATION_KEY=1234
    
  2. in my config.exs

    config :app, App.Notifications,
    notification_api_key: {:system, "API_NOTIFICATION_KEY"}
    
  3. in my module where I use it

    @api_notification_key Application.get_env(:app, App.Notifications)[:notification_api_key]
    
  4. 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 ?

like image 583
john Avatar asked Mar 08 '26 01:03

john


1 Answers

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
like image 168
Paweł Obrok Avatar answered Mar 09 '26 22:03

Paweł Obrok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!