DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Cover image for Mint ๐Ÿƒ: Environment Variables
Szikszai Gusztรกv
Szikszai Gusztรกv

Posted on

Mint ๐Ÿƒ: Environment Variables

This is the next post in a series that showcases the features of Mint, you can find the previous posts here:

In this post I will show you how to use environment variables.


In any application, being able to define variables which are deployment environment specific is a necessity. Let's say you might want to connect to a local API endpoint during development and remote API on production.

Defining environment variables

Mint uses .env files to store variables specific to the environment, which usually looks like this:

ENDPOINT=http://localhost:3001
WSENDPOINT=ws://localhost:3001
GATRACKINGID=google-analytics-tracking-id
Enter fullscreen mode Exit fullscreen mode

Here we declared three variables WSENDPOINT, ENDPOINT and GATRACKINGID that we want to use in our code.

Using environment variables

In Mint you can use the at (@) symbol followed by the name of the variable to refer to it:

module Main {
  fun render : Html {
    <div>
     <{ @ENDPOINT }>
    </div>
  }
}
Enter fullscreen mode Exit fullscreen mode

Essentially the value of the variable will be inlined during compilation with the type of String.

In an other example you can see how to use it when making a request:

...

response =
 @ENDPOINT + "/api/planets"
 |> Http.get()
 |> Http.send()

...
Enter fullscreen mode Exit fullscreen mode

If an environment variable is not defined in the application, then a nice error message is shown:

Error

Using a different .env file

By default the .env file in the root of the application is loaded, but you can specify a different file by using the --env (or -e) flag like this:

mint build --env .env.production
Enter fullscreen mode Exit fullscreen mode

That's it for today, thank you for reading ๐Ÿ™


If you like to learn more about Mint check out the guide ๐Ÿ“–

In the next part I'm going to tell you about stores ๐Ÿ˜‰ see you there ๐Ÿ‘‹

Top comments (0)

๐Ÿค” Did you know?

ย 
๐ŸŒš You can turn on dark mode in Settings