How to Use an API Key in Rails

Part 2 of 2: Using a hidden API key

Wendy Raven McNair
7 min readJun 29, 2021
Photo by Pixabay from Pexels

Now that I’ve obtained a movie API key from TMDb and I’ve stored my key in my .env file and hidden that file by including its name in my .gitignore file, I’m ready to use the key in my app. If you haven’t done these steps yet, refer to my blog Rails API — Part 1 of 2: Obtaining and hiding an API key.

Picking up where I left off in Part 1, let’s check to see if passing my API key variable, KEY, into ENV[ ] will reveal that my key number is truly hidden. Use byebug or pry in your seed.rb file. To use pry, first add the pry gem to your Gemfile:

Remember to install it with:

Put byebug or binding.pry in my seed.rb file:

Then run the following to drop into the pry:

Now check for my secret API key number using ENV[“KEY”]:

This returns my secret API key number which I’m not going to share with you because it’s a secret. So now that I know my key is available, I can build my movie table. In Part I, I detailed how my sample API address returned a movie object with many attributes. To keep things simple, I’ll just use the title and overview attributes on my movie table so my users can view a movie title and read a brief movie summary.

After I’m done using my pry, I always exit out of pry with:

Before I forget, at the top of my seeds.rb file, I place a call method to destroy all created movie objects so that each time I re-seed my database, I won’t have repeat data in my database:

Rails has generators that will build my tables and the related controllers and models. The scaffold generator does this in addition to creating migrations for the table. I create a movie table with attributes (title and overview) with the terminal command:

Notice I’m using the same attribute names that the JSON object contains. However, I could use different attribute names and still populate them with corresponding data from the JSON object.

I test that everything is set up properly to persist data in my database by dropping into my console:

And hardcode the creation of a movie:

To see if it persisted type Movie.all:

It returns my single newly created movie object. So now that my movie creation functionality is working and my API key is primed and ready, how do I use this API key to access movie data and turn it into movie objects stored in my database?

Your API specifications or documentation should give details on how to use your API key. The TMDb details can be found at the following URL address:
https://developers.themoviedb.org/3/getting-started

Scroll down the menu on the left to see a list of a variety of ways to retrieve data using the API key. There are a variety of ways I can use my key to get different movie data. I decided to include only popular movies so I was instructed to use my API key directly in the URL address in the following way:

https://api.themoviedb.org/3/movie/popular?api_key=#{ENV["KEY"]}

Notice how I use ENV[“KEY”] instead of my actual API key number. Using either returns the exact same data, however using ENV[“KEY”] keeps my secret number hidden.

To make a request call to my API from my back end seeds file, you need to include a gem in your Gemfile that will enable that feature. I’m using rest-client:

And also include the json gem to parse the data that rest-client retrieved:

Remember to bundle install:

In my seeds.rb file, I make a request call to the API and store the returned array of JSON objects in a response variable:

Then I parse the response and store that in a movies_array variable:

It’s significant to note that the returned JSON data can be structured in a variety of ways. So how you manipulate the returned JSON object to access the data will vary. To see how my data is structured, I use binding.pry to see what’s in my movies_array variable:

I re-seed to drop into the pry and then check my data:

And this is what it returns:

Note, I’m only showing the first two movie data items (Luca and Infinite) in the results array.

So I can see that results is a key pointing to an array of JSON movie data objects. So I can access this data with:

To see how many objects are in the array, I use:

And it returns the number 20. So the API returned 20 JSON popular movie data objects.

Now I can iterate over movies_array[“results”] and use the movie data to create and store movie objects in my database. To make sure, I iterate using .each and use binding.pry to check that the movie variable is pulling a single JSON object through each iteration:

I re-seed again to drop back into the pry, then check my movie variable:

Which does indeed return the first single JSON movie data object (Luca):

Now I can use the data captured in each iteration to finally create my movie instances and store them in my database:

I re-seed a final time:

Then I check that I now have movie objects in my database by dropping into the console:

And check my movie table:

Success! It’s full of newly created movie instances!

To see exactly how many, I use:

And it returns the number 20, indicating that all 20 JSON movie data objects have been used to create and store 20 movie instances.

Now my Rails back end is ready to display title and overview movie data for 20 popular movies. All I need to do is decide which UI (user interface) I want to use.

--

--