Owens

Owens

Hello all,

I’ve set up an API key in my dev.exs config but my Phoenix app isn’t reading it. It works in iex console.

sendgrid_api_key =
  System.get_env("SENDGRID_API_KEY") ||
    raise """
    environment variable SENDGRID_API_KEY is missing.
    """

Leads to

$ iex -S mix phx.server

** (RuntimeError) environment variable SENDGRID_API_KEY is missing.

But in IEX console

iex> System.get_env("SENDGRID_API_KEY")  

Outputs the correct key.

I set this variable using

$ export SENDGRID_API_KEY='MY_API_KEY'

Showing Posts 1 to 10

axelson

axelson

Scenic Core Team

If this is in your config/dev.exs:

sendgrid_api_key =
  System.get_env("SENDGRID_API_KEY") ||
    raise """
    environment variable SENDGRID_API_KEY is missing.
    """

Then everything would generally appear to be correct. How are you starting the iex session that is working? You could also verify with env | grep SENDGRID_API_KEY in the terminal before running iex -S mix phx.server.

If that doesn’t work then it will probably be necessary to look at the rest of your config.exs and config/dev.exs. Another debugging step would be to add IO.inspect(System.get_env()) right before you try to read the environment variable.

Owens

Owens OP

Correct, that is in my config/dev.exs

env | grep SENDGRID_API_KEY works propery

I added IO.inspect

sendgrid_api_key =
  IO.inspect(System.get_env("SENDGRID_API_KEY")) ||
    raise """
    environment variable SENDGRID_API_KEY is missing.
    """

This outputs nil to the console.

derek-zhou

derek-zhou

how about just

mix phx.server
Owens

Owens OP

Thanks for the idea, just tried it and still doesn’t work. :frowning:

NobbZ

NobbZ

I assume this is copied from a website?

Have you tried to delete and manually enter the argument to System.get_env/1 to make sure it doesn’t contain any unprintable charaters from copying?

Owens

Owens OP

this is copied from prod.secret.exs

secret_key_base =
  System.get_env("SECRET_KEY_BASE") ||
    raise """
    environment variable SECRET_KEY_BASE is missing.
    You can generate one by calling: mix phx.gen.secret
    """

I tried IO.inspecting System.get_env/0 and that worked, but the environment variables are all different than what I am seeing in iex console. I am wondering if it has something to do with the new zsh console on mac. It looked to me like the things output in development were from my bash environment.

Will report back as I dig deeper.

dimitarvp

dimitarvp

So, do you have the env vars in your ~/.zshrc file? Or, do you use the direnv program with a local .envrc file (in your repo)?

Owens

Owens OP

I keep them in my ~/.zshrc

wingyplus

wingyplus

I think that zsh has env. So should see SECRET_KEY_BASE in the output from env command.

Owens

Owens OP

So I took a recommendation from @NobbZ and changed the code to output all the env variables:

sendgrid_api_key =
  System.get_env() |> Enum.each(fn {k, v} -> IO.puts("#{k} => #{v}") end)

When I run iex -S mix phx.server 59 environment variables show (NOT including the one I want), but when I run iex -S mix only 39 show (including the one I want).

Solution
I took a gamble and just ran $ export SENDGRID_API_KEY='MY_API_KEY' again, now everything is working! :rofl:

Not sure why it didn’t work the first time, oh well. My guess is quitting terminal and reloading it might have also fixed this.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews