Skip to content

Instantly share code, notes, and snippets.

@joscdk
Created December 5, 2018 14:45
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joscdk/400567669ba5ed468467d0f1aebd9d36 to your computer and use it in GitHub Desktop.
Save joscdk/400567669ba5ed468467d0f1aebd9d36 to your computer and use it in GitHub Desktop.
Using .env files in Vapor 3

.env files can be an easy way to setup Environment variables locally. You can start using a .env files in Vapor 3 by following this small guide.

First setup the vapor-ext package in your Package.swift file:

.package(url: "https://github.com/vapor-community/vapor-ext.git", from: "0.1.0"),

Next create a .env file in the root of your project:

FOO=Bar

And then we can read the FOO variable from configure.swift like this

import VaporExt

Environment.dotenv()

let foo = Environment.get("FOO", "")
print(foo) // Bar

!!! IMPORTANT Make sure to add the .env file in .gitignore so it won't be pushed to git.

@vinamelody
Copy link

Fyi, this won't work ... I asked in Vapor's Discord and recommended to get this file instead https://raw.githubusercontent.com/vapor-community/vapor-ext/master/Sources/ServiceExt/Environment%2BDotEnv.swift. Hope this helps someone out there :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment