Using EAS environment variables

Edit this page

Learn how to use environment variables in EAS with examples.


Environment variables in EAS describes concepts related to using environment variables in EAS and differences in handling them with Expo CLI and EAS CLI. In this guide, you will learn how to use environment variables in EAS with practical examples.

The example used in this guide is a simple project that uses common environment variables during the build process and for configuration purposes:

  • EXPO_PUBLIC_API_URL: a plain text EXPO_PUBLIC_ variable that holds the URL of the API server
  • APP_VARIANT: a plain text variable to select an app variant
  • GOOGLE_SERVICES_JSON: a secret file variable to supply your git ignored google-services.json file to the build job
  • SENTRY_AUTH_TOKEN: a sensitive variable that holds the authentication token for Sentry used to upload source maps after builds and updates

Use environment variables in your code

The environment variables with the EXPO_PUBLIC_ prefix are available as process.env variables in your app's code. This means you can use them to dynamically configure your app behavior based on the values from environment variables.

import { Button } from 'react-native';

function Post() {
  const apiUrl = process.env.EXPO_PUBLIC_API_URL;

  async function onPress() {
    await fetch(apiUrl, { ... })
  }

  return <Button onPress={onPress} title="Post" />;
}

In above example, EXPO_PUBLIC_API_URL is used to dynamically set the API URL for the fetch request.

Do not store sensitive information in EXPO_PUBLIC_ variables, such as private keys. These variables will be visible in plain-text in your compiled app.

Other variables, without the EXPO_PUBLIC_ prefix, can be used during app config resolution. An example of this is the APP_VARIANT variable used to determine the app name and package name or bundle identifier based on the selected app variant.

app.config.js
const IS_DEV = process.env.APP_VARIANT === 'development';
const IS_PREVIEW = process.env.APP_VARIANT === 'preview';

const getUniqueIdentifier = () => {
  if (IS_DEV) {
    return 'com.yourname.stickersmash.dev';
  }

  if (IS_PREVIEW) {
    return 'com.yourname.stickersmash.preview';
  }

  return 'com.yourname.stickersmash';
};

const getAppName = () => {
  if (IS_DEV) {
    return 'StickerSmash (Dev)';
  }

  if (IS_PREVIEW) {
    return 'StickerSmash (Preview)';
  }

  return 'StickerSmash: Emoji Stickers';
};

export default {
  name: getAppName(),
  %%placeholder-start%%... %%placeholder-end%%
  ios: {
    bundleIdentifier: getUniqueIdentifier(),
    %%placeholder-start%%... %%placeholder-end%%
  },
  android: {
    package: getUniqueIdentifier(),
    %%placeholder-start%%... %%placeholder-end%%
  },
};

The GOOGLE_SERVICES_JSON is a secret file variable in that is not readable outside of EAS servers and is used to provide the git ignored google-services.json file to the build job. To use it in the app config, you can use the process.env variable and provide a fallback value in case the variable is not set (for local development when you usually have it inside your project's repository).

app.config.js
export default {
  %%placeholder-start%%...%%placeholder-end%%
  android: {
    googleServicesFile: process.env.GOOGLE_SERVICES_JSON ?? '/local/path/to/google-services.json',
    %%placeholder-start%%...%%placeholder-end%%
  },
};

Create environment variables

To create environment variables on EAS servers, you can use the environment variables creation form page or eas env:create command.

In the form, you can specify the name, value, environment(s) and visibility for the variable.

Created environment variables will be available in the list on the Environment Variables page on the Expo website.

Based on the environment variables in this example, the list will look like this:

In above example, the SENTRY_AUTH_TOKEN can be treated as a sensitive environment variable. It is used to authenticate Sentry to upload source maps after builds and updates, so it has to be accessible outside of EAS servers.

Pull environment variables for your local development

The easiest way to use the EAS environment variables for local development is to pull them into a .env file using the eas env:pull --environment environment command. You can also use the Export option on the Expo website to download the file and store it inside your project.

Run the following command to create a .env file in the root of your project:

Terminal
eas env:pull --environment development

The created .env file will look like this:

.env.local
# Environment: development

APP_VARIANT=development
EXPO_PUBLIC_API_URL=https://staging.my-api-url.mycompany.com
# GOOGLE_SERVICES_JSON=***** (secret variables are not available for reading)
SENTRY_AUTH_TOKEN=token

The downloaded EXPO_PUBLIC_ variables are available for local development when using the npx expo start command. The GOOGLE_SERVICES_JSON variable is not available to be pulled to the local environment since it is a secret variable.

It is best to add all of the .env files to .gitignore to avoid committing them to your repository and exposing sensitive information. Committing a .env file may additionally lead to environment variables resolution conflicts.

Use environment variables with EAS Build

To have a full control over the environments used for your builds, you can specify the environment field in the build profiles settings in the eas.json file.

eas.json
{
  "build": {
    "development": {
      "environment": "development"
      %%placeholder-start%%... %%placeholder-end%%
    },
    "preview": {
      "environment": "preview"
      %%placeholder-start%%... %%placeholder-end%%
    },
    "production": {
      "environment": "production"
      %%placeholder-start%%... %%placeholder-end%%
    },
    "my-profile": {
      "environment": "production"
      %%placeholder-start%%... %%placeholder-end%%
    }
  }
}

All of the environment variables from the selected environment will be used during the build process. Plain text and sensitive variables will be available when resolving build configuration based on the dynamic app config in EAS CLI as well.

The environment variables of secret type are not available during build configuration resolution in EAS CLI as they are not readable outside of the EAS servers.

In this example app, all of the environment variables except GOOGLE_SERVICES_JSON (which is secret) will be available during build configuration resolution in EAS CLI.

It's especially important in this context to have the correct visibility set for the APP_VARIANT variable so that the variable available in EAS CLI and is able to generate credentials for the correct package name and bundle identifier.

Use environment variables with EAS Update

The most convenient way to use EAS environment variables with EAS Update is to run the eas update command with the --environment flag specified:

Terminal
eas update --environment production

When the --environment flag is used, only the environment variables from the specified EAS environment will be used during the update process. This flag allows you to use the same environment variables while creating updates as with creating builds.

The secret variables will not be available during the update process as they are not readable outside of EAS servers.
The default behavior of the eas update command is to use .env files if the --environment flag is not specified.

Use environment variables for other commands

One way to supply non-secret EAS environment variables to other EAS commands is to use the eas env:exec command.

Terminal
eas env:exec --environment production 'echo $APP_VARIANT'

For example, it can be useful when uploading source maps to Sentry using a SENTRY_AUTH_TOKEN variable after an update bundle is created.

Terminal
eas env:exec --environment production 'npx sentry-expo-upload-sourcemaps dist'