Config module - correct set-up in Phx app

Hi, following this guide https://phxroad.com/phoenix/environment-variables-in-phoenix but running into headwind as usual.

I’ve changed out use Mix.Config to import Config in all my config files
I’ve created a dev.secret.exs file and added it to .gitignore
I’ve import_config "dev.secret.exs at the bottom of dev.exs

I’m trying to use a single api key to an external service. Very simple.

Here’s the code of dev.secret.exs

import Config
config :my_app,
api_key: “the key”

Firstly, I don’t know what rules govern the atoms used in this config statement. Would like to read some guidance on those.

Anyway, when I type Application.fetch_env!(:map_app, :api_key) into iex I get

(ArgumentError) could not fetch application environment :api_key for application :my_app because configuration at :api_key was not set (elixir 1.10.3) lib/application.ex:653: Application.fetch_env!/2

What have I missed?

Well, I’m not sure what it was that I missed, perhaps I needed to restart, anyway it started working … now to the challenge of deploying … heaven help the novice

1 Like

The first atom is the name of the otp application the config belongs to. The app env will be loaded with respective otp application (and therefore be unavailable if the otp app is not loaded).

config :app, :key, test: something is just a nicer syntax for config :app, key: [test: something] because often one doesn’t want to put config directly at the root, but scope them to a module or some other key.

1 Like