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.

Special Characters in Environment Variables

Environment variables are applied literally, with the exception of commas and double quotes. Commas are interpreted as delimiters, and unescaped double quotes are either disregarded or will cause the rollout process to fail.

  • For example: "my_example_variable" will be applied as: my_example_variable.
  • If double quotes are inside of a string (e.g. my_exampl"e_text), the rollout process will fail.
  • To keep double quotes around a variable, escape them with a backslash (\). For example: \"my_example_var\" will be applied as: "my_example_var".
  • Double quotes inside strings can also be handled with escaping, so the rollout process can be completed. For example: my_examp\"le_var will be applied as: my_examp"le_var.

Base64 Encoded Variables

If your environment variable is Base64 encoded and you experience issues (e.g. 500 errors in the browser, build errors, runtime errors, etc.), try wrapping the value of the variable with single quotes.

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').

Internal Connections and the Build Process

Internal connections are only available during runtime; they are not available during the build process.

If your application tries to connect to a database using an internal connection during the build process, this causes an error that says the database is not running, which makes the build fail. This is expected because the internal connection is not live during the build; it can only be used during runtime.

There are a couple of ways to work around this.

Option 1: Move the logic that connects to the database from the application’s build command to the start command. For example: if you have a command like prisma migrate in the build process and move that command to the start command, your application will only access the database during runtime, and the build will be successful.

Option 2: Add separate environment variables as needed for the database connection, one available for the build process, and the other only for runtime. The keys can be the same (e.g. DB_CONNECTION_URL) as long as one is only available during the build process and the other is only available during runtime. Use the database’s External connection details (Applications > dbname > Info > External connections) for the values of any variables to be used in the build process.

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.js, this is how you would start a server:

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

Environment Variables Not Set By Kinsta

By default, the NODE_ENV environment variable is not set to production for Node.js applications; you must add this environment variable manually.

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