Edit this page
Learn how to use environment variables in EAS in practical scenarios.
In Environment variables in EAS we described concepts related to using environment variables in EAS and differences in handling them with Expo CLI and EAS CLI. This guide will show you how to use environment variables in EAS with practical examples.
In this example, we'll look at a simple project that uses these 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 serverSENTRY_AUTH_TOKEN
: a sensitive variable that holds the authentication token for Sentry used to upload source maps after builds and updatesAPP_VARIANT
: a plain text variable to select an app variantGOOGLE_SERVICES_JSON
: a secret file variable to supply your gitignored google-services.json
file to the build job1
The environment variables with the EXPO_PUBLIC_
prefix are available as process.env
variables in your application code.
This means you can use them to dynamically configure your app behavior based on the environment variables' values.
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 this example EXPO_PUBLIC_API_URL
is used to dynamically set the API URL for the fetch request.
Do not store sensitive info, such as private keys, inEXPO_PUBLIC_
variables. These variables will be visible in plain-text in your compiled application.
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 bundle identifier/package name based on the selected app variant.
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%%
},
};
In this example we additionally consider the SENTRY_AUTH_TOKEN
as an example of a variable that can be treated as a sensitive environment variable.
It is used to authenticate with Sentry to upload source maps after builds and updates, so we want to be able access it outside of EAS servers as well.
The GOOGLE_SERVICES_JSON
is a secret file variable in our example setup 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, we can use the process.env
variable and provide a fallback value in case the variable is not set (for local development when we usually have it in our repo).
export default {
%%placeholder-start%%...%%placeholder-end%%
android: {
googleServicesFile: process.env.GOOGLE_SERVICES_JSON ?? '/local/path/to/google-services.json',
%%placeholder-start%%...%%placeholder-end%%
},
};
2
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 form on the Environment Variables page.
Based on the environment variables in this example, the list would look like this:
3
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 use the Export option on the website as well to download the file.
After running the following command:
-
eas env:pull --environment development
a .env file will be created in the root of your project with the following content:
# 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.
4
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.
{
"build": {
"production": {
"environment": "production"
%%placeholder-start%%... %%placeholder-end%%
},
"preview": {
"environment": "preview"
%%placeholder-start%%... %%placeholder-end%%
},
"development": {
"environment": "development"
%%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 secret type environment variables are not available during build configuration resolution in EAS CLI as they are not readable outside of the EAS' servers.
In our 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 it's available in EAS CLI so it is able to generate credentials for the correct bundle identifier and package name.
5
The most convenient way to use EAS environment variables with EAS Update is to run the eas update
command with the --environment
flag specified:
-
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.
The --environment
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 theeas update
command is to use .env files if the--environment
flag is not specified.
6
One way to supply non-secret EAS environment variables to other EAS commands is to use the eas env:exec
command.
-
eas env:exec --environment production 'echo $APP_VARIANT'
It can be useful when uploading source maps to Sentry using a SENTRY_AUTH_TOKEN
variable after an update bundle is created.
-
eas env:exec --environment production 'npx sentry-expo-upload-sourcemaps dist'