Populating your tables using data from an external API with REST Client and Ruby on Rails.

Alex Mendes
3 min readOct 17, 2019

--

APIs are a ubiquitous component in almost every software developer’s toolbox

APIs, or application program interfaces, are vital tools for businesses in all industries and as software programmers learning how to fetch and manipulate data from an API, from a technical standpoint, it’s a very powerful skill.

In today’s blog, I’ll walk you through the steps to use fetch to get a list of doctors, insurance providers, and specialties from BetterDoctor, Inc. API using Ruby on Rails and a Ruby gem called REST Client to populate my database with that information.

Choosing your API and reading its documentation

There are plenty of websites that offer free API to use in your application. You can find some ideas at https://rapidapi.com/ or by simply doing a quick Google search.

Most of them will require signup, so they can provide you an API Key that has to be used at the moment you are performing the request. And the way you use that key varies from API to API, that’s why it’s so important to read the documentation even before you get started coding.

you’re not only going to learn how to actually fetch data from that specific API but also learn about its endpoints, limits of requests per day, amount of data per request, where to place the key, data types, etc. and understand how valuable that data is going to be for your app.

Doctor’s API Documentation

Based on the data you’re getting you may even rethink some functionalities in your app. And once you have that figured it out, it comes part 2.

Setting up your environment

Before writing any line of code, let’s make sure we have the right gem to perform the call. In my case, I used REST Client, so let’s go on a step-by-step in how to install it in our project

On the website, rubygems.org look for the newest version of the REST Client gem. Copy and paste its name and version inside your gemfile.

Go to your terminal, inside your working folder directory, and bundle install to perform all the changes.

Now that you have all the requirements to perform a fetch, let’s decide when it’s the best moment in your app to actually do it.

Actually writing some code

Because updates won’t happen frequently on the API, I choose to fetch the data only once in my seed.rb file and store that on the database at the same time.

So, let’s cover how to create the method that will allow our app to make the call and handle the response.

first, we are going to assign the variable response to the helper method RestClient.get and the URL + apiKey and parse the response into a JSON object.

Once we have the response I just had to .map() through each element and populate the data on the table using the method .create() from Active Record. Just make sure you had the right keys in order to access each value, run rails db:seed on your terminal inside your folder directory and that’s it! You are ready to start working on your application with real data!

--

--

Alex Mendes

Web and Mobile Developer. Software Engineering graduated from Flatiron School, Sports Lover, From Planet Earth.