DEV Community

loading...
Cover image for Mint ๐Ÿƒ: Environment Variables

Mint ๐Ÿƒ: Environment Variables

gdotdesign profile image Szikszai Gusztรกv ใƒป2 min read

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

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>
  }
}

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()

...

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

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 ๐Ÿ‘‹

Discussion (0)

pic
Editor guide