# System.get\_env vs. Application.get\_env

**URL:** <https://forum.elixirforum.com/t/system-get-env-vs-application-get-env/11246>\
**Category:** Questions\
**Tags:** environment\
**Created:** [January 1, 2018, 6:28pm UTC](https://forum.elixirforum.com/t/system-get-env-vs-application-get-env/11246 "2018-01-01T18:28:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcarvalho](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@mcarvalho](https://forum.elixirforum.com/u/mcarvalho)\
**Post date:** [January 1, 2018, 6:28pm UTC](https://forum.elixirforum.com/t/system-get-env-vs-application-get-env/11246/1 "2018-01-01T18:28:46Z")

</div>

What is the difference between System.get\_env and Application.get\_env? For example, what are best practices to use one versus another.

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [January 1, 2018, 6:35pm UTC](https://forum.elixirforum.com/t/system-get-env-vs-application-get-env/11246/2 "2018-01-01T18:35:51Z")

</div>

`System.get_env/3` gets from the system environment, while `Application.get_env/3` gets from the applications environment.

The first is defined by the means of your operating system, while the later is mostly configured during build using the file(s) in `config` and local to your application.

The application environment is described in the docs: [Application — Elixir v1.20.2](https://hexdocs.pm/elixir/Application.html#module-application-environment)

The system-environment is not, since it is not any part of elixir or the BEAM. It’s just there…

---

<div class="post-metadata">

**Author:** ![mbuhot](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mbuhot/32/33444_2.png) [@mbuhot](https://forum.elixirforum.com/u/mbuhot)\
**Post date:** [January 2, 2018, 6:54am UTC](https://forum.elixirforum.com/t/system-get-env-vs-application-get-env/11246/3 "2018-01-02T06:54:43Z")

</div>

> [@mcarvalho](#):
>
> what are best practices to use one versus another.

My rule of thumb is to only use `System.get_env` during application initialization, eg in your `Application.start`, `Repo.init`, `Endpoint.init` callbacks.

Once the application is initialized, the environment should always be queried by `Application.get_env`, since `System.get_env` can only return strings.

I like the `dev` / `test` environment to be statically defined in the config.exs files, only reading from the system environment for `prod` builds.

---

<div class="post-metadata">

**Author:** ![zenw0lf](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zenw0lf/32/3716_2.png) [@zenw0lf](https://forum.elixirforum.com/u/zenw0lf)\
**Post date:** [December 2, 2018, 3:52pm UTC](https://forum.elixirforum.com/t/system-get-env-vs-application-get-env/11246/4 "2018-12-02T15:52:01Z")

</div>

Replies above explain the difference between `Application.get_env` and `System.get_env`.

As for best practices, have a look at this: [Configuration Demystified | Blog · Elixir School](https://elixirschool.com/blog/configuration-demystified/)

---

<div class="post-metadata">

**Author:** ![pmarreck](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/pmarreck/32/3631_2.png) [@pmarreck](https://forum.elixirforum.com/u/pmarreck)\
**Post date:** [August 30, 2023, 3:44pm UTC](https://forum.elixirforum.com/t/system-get-env-vs-application-get-env/11246/5 "2023-08-30T15:44:03Z")

</div>

The documentation for these should make it much clearer whether these values are read just once at build time, or every time at runtime. (I’m having to do some extra googling to figure this out, when I could have had the answer already.)

---

<div class="post-metadata">

**Author:** ![LostKobrakai](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/32/3072_2.png) [@LostKobrakai](https://forum.elixirforum.com/u/LostKobrakai)\
**Post date:** [August 30, 2023, 4:10pm UTC](https://forum.elixirforum.com/t/system-get-env-vs-application-get-env/11246/6 "2023-08-30T16:10:14Z")

</div>

> [@pmarreck](#):
>
> The documentation for these should make it much clearer whether these values are read just once at build time, or every time at runtime.

The documentation can’t do that, because that depends on where those functions are called, not which function is called.

---

<div class="post-metadata">

**Author:** ![pmarreck](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/pmarreck/32/3631_2.png) [@pmarreck](https://forum.elixirforum.com/u/pmarreck)\
**Post date:** [August 30, 2023, 4:24pm UTC](https://forum.elixirforum.com/t/system-get-env-vs-application-get-env/11246/7 "2023-08-30T16:24:51Z")

</div>

Ah… good point. a value retrieval is a value retrieval, whether it happens once (at build) or many times (at runtime) depends on the caller. Of course. 🤦‍♂️
