Using an API in Rails

Shevaughn Grant
4 min readJan 3, 2021

There’s one thing in coding that I absolutely dreaded and that was having to deal with apis. They allow you to access real time data and ultimately make an application much more dynamic.

API Key

Once you have found what api you want to work with you will most likely need to create an account to access your api key. Once you have received your key it is time to slap it in your app. The most important thing to note is that you must mask your api key in case you push to github and risk exposing your secrets.

There are two gems I recommend to use:

  • gem dotenv-rails
  • gem figaro

In this example we will be utilizing the figaro gem.

In your Gemfile add the figaro gem.

located in the root folder.

We can then proceed and run bundle install to add this gem to our Gemfile.lock. In the config folder we will create a new YAML file called ‘application.yml’. This file is where we will store our api key and then add to ‘.gitignore’ later. Follow this format below.

config/application.yml

Let’s now hide our api key by adding our file path to the ‘.gitignore file’. If the ‘.gitignore’ file doesn’t exist you must create one. Now assuming both the config folder and the ‘.gitignore’ are on the same level we can add the file path like this:

located in .gitignore file

Voila, our api key is now hidden.

Calling the API

Let’s start by adding the gem rest-client to our Gemfile and run bundle install. At the top of our ‘seeds.rb’ file we will require rest-client like so:

db/seeds.rb

We will be using this to ease through the process making things much easier. To make the request we will write the following code:

db/seeds.rb

Allow me to breakdown what is happening here. We created a variable called key to hold our api key. We then used an environment variable that called our constant that we declared in the ‘application.yml’ file. In the variable movieResp we see RestClient.get() . The method .get comes with the rest-client gem and it takes an argument that accepts the api’s url. Finally, we parsed the response in json format.

The final steps here are to grab the specific data you want from the api and then seed it to the database.

json response in Postman

Above is a json response I got from Postman after entering the api link into their get request field. To grab the name of the movie we have to first access the “results” array.

db/seeds.rb

Here we set the parsed response to a variable called movieArr and append ["results"] to the end to grab those values. Now we can iterate through this array and grab the name of each movie.

db/seeds.rb

Note: title is the key that is in my database. You should change this depending on what you have in yours. In brackets we have ["name"] , this is the key from the api response. Make sure each field is aligned properly to get the correct info returned.

Final Touches

Lastly, we can now run rails db:seed to populate our database with the api data. After successfully seeding, run the server ; rails s and navigate to the route of the model you created the data for.

Conclusion

APIs are an incredibly powerful tool to use in your application but can be tedious to get the hang of at first. I hope this tutorial will help you further your curiosity with apis.

https://hackernoon.com/introducing-figaro-module-rails-configuration-gem-ih8736td

https://developers.themoviedb.org/3/getting-started/introduction

--

--