How do hide secret key information in React application

The application makes API calls which API calls need secret keys. These keys are not committed to the code repository.

This tutorial covers how to hide the secret keys hiding in React applications created with the create-react-app command.

For example, If you are creating react Twilio plugin, you need secret keys such as ACCOUNT_SID and AUTH_TOKEN details.

For example, React application is created with the create-react-app command.

Hide API key details in react application

first, install the latest dotenv npm library using the npm command

npm install dotenv

Create a .env file in the application root directory

.env file


Next, add the .env file to the .gitignore file that is not committed to the git repository.

.gitignore file


Once you created the env file and added API key details, you can access the using process.env file. process.env is a process object for accessing environment variables.

In react components you can access the API keys as seen below.


Hide API key information with the create-react-app command

In this dotenv npm, dependency is not required, As the create-react-app tool handles the adding environment files differently.

Assume the application is created with the create-react-app CLI command.

At the root of the project, Create a .envfile



each key is appended with REACT_APP_notation for creating a key, if you don’t add REACT_APP_ to keys, It returns empty while accessing in react components.

Access the key using the process.env object.


Finally, You can add the .env file to the .gitignore file to ignore the file while committing changes.

Wrap up

Hiding API key details can be achieved with the .env file which is not committed to the git repository.

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.

Related posts

Basic React Interview questions and answers

Best ways to add an element to an array in React state with examples

Difference between Development and Production Builds in React

Difference between pureComponent and component in React

Difference between super and super props in React constructor class?

Fixing an error You are running create-react-app 4.0.3, which is behind the latest release (5.0.0) in React

Getting started with ReactJS Bootstrap Tutorials with examples | reactstrap tutorials