Stop pushing your React API Key on GitHub 😪

Have you ever create an application with React, in this application you use external API’s and you pushed these API keys on GitHub ?

Oh no bad practice now everybody can use your API key.

We will see how we can avoid this and hide your API’s keys on…


This content originally appeared on DEV Community and was authored by Mansour Mahamat

Have you ever create an application with React, in this application you use external API’s and you pushed these API keys on GitHub ?

Oh no bad practice now everybody can use your API key.

We will see how we can avoid this and hide your API’s keys on Github, you need .env file.

How to setup .env file inside React app ?

You don’t need to install env package, this feature is available with react-scripts@0.2.3 and higher.

  • Make a file called .env in your project root

  • Inside the env file, add your variables and API keys value like this :

REACT_APP_GITHUB_API_KEY=Hello world 12345
REACT_APP_MOOVIE_API=0123456789

You should prefix all your variables name by REACT_APP if not it will be ignored

  • Now you need to restart your React server with npm start to access these variables

  • Inside your React application, you can now access these variables in using this syntax :

{process.env.REACT_APP_GITHUB_API_KEY}
{process.env.REACT_APP_MOOVIE_API}

API Usage

Now you have zero excuse to push your API key in your React application.

Here you have the React documentation about using environment variables


This content originally appeared on DEV Community and was authored by Mansour Mahamat


Print Share Comment Cite Upload Translate Updates
APA

Mansour Mahamat | Sciencx (2021-12-03T07:48:07+00:00) Stop pushing your React API Key on GitHub 😪. Retrieved from https://www.scien.cx/2021/12/03/stop-pushing-your-react-api-key-on-github-%f0%9f%98%aa/

MLA
" » Stop pushing your React API Key on GitHub 😪." Mansour Mahamat | Sciencx - Friday December 3, 2021, https://www.scien.cx/2021/12/03/stop-pushing-your-react-api-key-on-github-%f0%9f%98%aa/
HARVARD
Mansour Mahamat | Sciencx Friday December 3, 2021 » Stop pushing your React API Key on GitHub 😪., viewed ,<https://www.scien.cx/2021/12/03/stop-pushing-your-react-api-key-on-github-%f0%9f%98%aa/>
VANCOUVER
Mansour Mahamat | Sciencx - » Stop pushing your React API Key on GitHub 😪. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/03/stop-pushing-your-react-api-key-on-github-%f0%9f%98%aa/
CHICAGO
" » Stop pushing your React API Key on GitHub 😪." Mansour Mahamat | Sciencx - Accessed . https://www.scien.cx/2021/12/03/stop-pushing-your-react-api-key-on-github-%f0%9f%98%aa/
IEEE
" » Stop pushing your React API Key on GitHub 😪." Mansour Mahamat | Sciencx [Online]. Available: https://www.scien.cx/2021/12/03/stop-pushing-your-react-api-key-on-github-%f0%9f%98%aa/. [Accessed: ]
rf:citation
» Stop pushing your React API Key on GitHub 😪 | Mansour Mahamat | Sciencx | https://www.scien.cx/2021/12/03/stop-pushing-your-react-api-key-on-github-%f0%9f%98%aa/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.