Step by Step guide to Hiding API key in Rails and React

  1. Install dotenv-rails gem. This enables us to access the variable from anywhere in the app. You can either add it to your gemfile directly like below. Or type, gem install dotenv in your applications terminal.
If you used create-react-app to create your application, dotenv is already part of the script, so double check your gemfile to see if it is already exists.
Above are examples of what your env. variables might look like. (not real keys, I made them up!)
Your application might have a .gitignore file already established, if so, simply just add .env to it like above.
In the first image, I was making calls to AlphaVantage API. In the second image, I was using a stock_quote gem
stock_controller.rb and Dashboard.js

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

8 Tips to Write Cleaner Code

AMA Recap: Metaxy x Gamerse

A Guide to Scaling Software Development Teams

Simple Example of Reusability in C++

GameDevHQ | Day 2

How To Pass The AWS Solutions Architect Associate Exam

CS371p Spring 2022: Erika Tan

Interview with Torkel Ödegaard: Creator and Project Lead, Grafana Labs

The Craft of Open Source — Grafana

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Allison Anzalone

Allison Anzalone

More from Medium

Upgrading to React Router version 6

Matrix Spiral in JS

React: arrays in state and how the component re-renders

React Components Just Make Sense