Environment variables are useful for feeding your application information from outside the running of that application. It is typically used to set things like database connection details and API keys.

Environment variables for your application.
Environment variables for your application.

Adding Environment Variables

Environment variables can be added in the Application details step when adding an application or on your application’s Settings page after deployment.

In Application Details

To add environment variables when adding your application, expand the Environment variables section, enter the key-value pairs, and select if the variables are to be available during runtime and/or the build process.

Add application details.
Add application details.

After Deployment

To add environment variables after deployment, go to your application’s Settings page, scroll down to the Environment variables section, and click Add environment variable. Add the key-value pairs in the Add environment variable modal/pop-up window and select if the variables are to be available during runtime and/or the build process.

Add an environment variable key-value pair.
Add an environment variable key-value pair.

Using Environment Variables

How you use environment variables is up to your application. In Node, for example, you can access a variable named API_KEY with process.env.API_KEY. In PHP, you would use getenv('API_KEY').

Environment Variables Set By Kinsta

Kinsta always sets PORT as the port used by the web server. If you’d like your application to interact with the web server, you will need to use this environment variable. For example, in Node, this is how you would start a server:

app.listen(process.env.PORT, () => {
console.log("Weather server is up and running")
})

Environment Variable Language Examples

How you use environment variables is up to your application. The following table shows how to call an environment variable named API_KEY in various languages:

Language Code
Ruby ENV["API_KEY"]
Node.js process.env.API_KEY;
Python os.environ.get('API_KEY')
Java System.getenv("API_KEY");
Scala System.getenv("API_KEY");
PHP getenv('API_KEY');
Go os.Getenv("API_KEY")