How to hide your API keys from public in ReactJS?
Last Updated :
07 Aug, 2024
API keys are used for authenticating requests to various online services and APIs. However, exposing these keys in your ReactJS application can lead to security risks, including unauthorized access and misuse. To protect your API keys, follow these best practices to keep them secure.
Why Hiding API Keys is Important
API keys are unique identifiers that allow access to APIs. Exposing them publicly can lead to unauthorized use, potential data breaches, and misuse of your API quota. It is essential to keep these keys hidden from public view to ensure the security and integrity of your application.
One of the most common practices to secure the API key when using ReactJS is to hide the API key using env variables. Create a .env file in your root directory and make an env variable using the prefix REACT_APP. For example, as shown below:
REACT_APP_KEY = hello_world
Note: The declaration should be exact, with no space in-between.
Now, you can use the key using process.env object by importing the file to your App.js file. And before pushing the code to GitHub or Heroku, delete the .env file and use the key management system of the platform.
How to Secure API Keys in a ReactJS Project
One of the most effective methods to hide API keys in a ReactJS project is by using environment variables. Here’s a step-by-step guide to securely manage your API keys:
Steps to keep your API keys safe:
- Creating .env file:Just create a .env named file in the root directory of your React project as shown below:

- Creating env variables in .env file: Using the prefix REACT_APP, create your own env variables and assign the key as shown below: Filename: .env
REACT_APP_API_KEY = Your_api_key
// Example
REACT_APP_API_KEY = 6341454121
Note: Make sure to not use any space in-between as it may cause errors.
- Add .env file to your .gitignore file: This prevents Github from pushing the .env file to a remote repository. Avoid pushing your API keys in a public repository. Filename: .gitignore
# API keys
.env
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
...
- Accessing the API key: The API key can be accessed easily using the process.env object. Just simply import the file to your main file and access it using process.env.your_key_name.Filename: App.js
import React, { Component } from 'react';
import './App.css';
// Accessing your defined KEYS in .env file
console.log(process.env.REACT_API_KEY)
// Rest of your program
function App()
{ ... }
Now, you're good to go and use the online servers to push your code without worrying about your keys getting public.
Similar Reads
How to use files in public folder in ReactJS ? In React, the files store public folder contains static files such as index.html, javascript library files, images, and other assets, etc. which you don't want to be processed by Webpack. Files in this folder are copied and pasted as they are directly into the build folder. Files inside the `public`
2 min read
How to hide API key in Node ? When building applications that interact with third-party services or APIs, using API keys for authentication and authorization is common. However, exposing these keys in your codebase can pose significant security risks. Malicious actors could misuse your API keys, leading to unauthorized access, d
2 min read
How to log-out user from app using ReactJS ? The objective of this article is to how we can set up, log out a user, and retrieve data from local memory User Browser storage in the React app. During the sign-in session, we will save the user details to the browser's local storage, and also during the time of logout, we will remove the user's de
3 min read
How to show and hide Password in ReactJS? To show and hide passwords in React JS we can simply use the password input with an input to select the show and hide state. The user might need to see what has he used as the password to verify which is an important concept in login and registration forms. We can create this type of password input
3 min read
How to Fetch Data From an API in ReactJS? ReactJS provides several ways to interact with APIs, allowing you to retrieve data from the server and display it in your application. In this article, weâll walk you through different methods to fetch data from an API in ReactJS, including using the built-in fetch method, axios, and managing the st
8 min read