You can define environment variables and use them in redocly.yaml
, and Markdown and React pages in the project. Environment variables are used during both build and run time for the project. By default, environment variables are not available in the browser. To access environment variables in React or Markdown pages, you have to prefix them with PUBLIC_
.
Define environment variables using one of the following ways:
- Create
.env
file in the root of the project - Use the shell (in other words, in your local environment, CI)
- Add them individually on the Settings page for the project
Example .env
file can look like this:
PUBLIC_CUSTOM_VARIABLE=Hello
PUBLIC_IS_PRODUCTION=true
PUBLIC_BUILD_NUMBER=50
Using the REDOCLY_ENV
variable, you can set up different environments and use separate .env
files like .env.production
, .env.development
, and .env.preview
based on your needs.
When hosting your project at Redocly, we will automatically set the proper REDOCLY_ENV
variable:
- For production builds, the
REDOCLY_ENV
variable will be set to"production"
- For previews, the
REDOCLY_ENV
variable will be set to"preview"
You can also add environment variables individually through the Redocly interface:
- Once logged in to Redocly, select the project.
- Select Settings > Environment variables.
- Select Add environment variable.
- Enter the Environment variable name and value.
- If the variable is a secret, select the secret checkbox to store it in an encrypted format and exclude it from the logs.
Do not use environment variables with sensitive information like passwords or API keys on the pages because all users of the project can see them.
To use environment variables in redocly.yaml
, use the curly brace syntax {{ process.env.<env_var_name> }}
.
For example:
logo:
srcSet: '{{ process.env.LIGHT_LOGO_PATH }} light, {{ process.env.DARK_LOGO_PATH }} dark'
altText: Redocly logo
link: '/'
navbar:
items:
- page: index.md
label: '{{ process.env.HOME_LABEL }}'
- page: config/index.md
label: Config
Environment variables in redocly.yaml
only support string values.
To use environment variables in your React code, refer to them using the following syntax: process.env.<env_var_name>
The following is an example of using an environment variable in a Typescript file:
import * as React from 'react';
export default function () {
const buildNumber: number = parseInt(process.env.PUBLIC_BUILD_NUMBER || '')
return (
<div>
<h1>My custom variable value is {process.env.PUBLIC_CUSTOM_VARIABLE}</h1>
<h2>Is this running in production? - {process.env.PUBLIC_IS_PRODUCTION}</h2>
<h3>Is build number high? - {buildNumber > 40 ? 'Yes' : 'No'}</h3>
</div>
);
Environment variables always have String
type. PUBLIC_IS_PRODUCTION
and PUBLIC_BUILD_NUMBER
from example above will become "true"
and "50"
when used in React components. To get them in desired type, you'll have to do manual conversion.
In Markdown files, access environment variables using Markdoc variables syntax under env
namespace:
# My custom variable is {% $env.PUBLIC_CUSTOM_VARIABLE %}
The following environment variables are available by default:
PUBLIC_REDOCLY_BRANCH_NAME
: The name of the branch that the project is built from
You can manage environment variables through the Settings page in your project.
- Once logged in to Redocly, select the project.
- Select Settings > Environment variables.
- Hover over the environment variable you want to update.
- Select the Edit icon next to the variable you want to change.
- In the Edit environment variable dialog that opens, make changes to your variable value select to make the variable secret.
- Select Save.
- Once logged in to Redocly, select the project.
- Select Settings > Environment variables.
- Select the Delete icon next to the variable you want to remove. The variable is removed from the list immediately.
When defining custom names, avoid commonly reserved environment variables, such as HOME
and PATH
, and those related to the Node environment such as NODE_ENV
.
The PUBLIC_
prefix is shared with the client side, and should not be used for any secrets.
The REDOCLY_
prefix is reserved for future use by Redocly.
- Markdoc variables syntax - Learn the syntax for using environment variables within Markdown content and templates
- Projects overview - Explore other project configuration options and settings available in Reunite