Tomo.Log()


[Vapor3] How to obtain environment variables

[09/13, 2018]

English / 日本語

You can obtain environment variables on Vapor3. I think this is very useful. So let's take a look how to do it.

How to obtain it?

It's very simple. It's just like this.

Environment.get("name")

How to set environment variables?

I'm not familiar to Linux so I also searched how to set the variables.

e.g. Set environment variables named "APIKEY" on Ubuntu.

export APIKEY=ABCD1234

Obtain it on Vapor3.

if let key = Environment.get("APIKEY") {
     //Use the key
}

What do I use Environment.get() for?

Vapor project became more simple

DB settings and API Key are suitable for environment variables. If variables are set on devices, you don't need to care about which environment is when you write codes.

It might be secure???

According to Vapor3's document, credential information shouldn't be in your project. So there might be some good points for security. (I'm not sure...)