Seeding a Rails backend with an external API.

Kevin Gleeson
3 min readJan 30, 2021
That’s it, that’s the blog.

Seeding is an ever-important task when you’re building out a backend in any language. Since my primary backend language is Ruby, and because Rails is just plain awesome, I want to detail the above picture a bit more and show you how you can seed your database with information from an external api with Ruby on Rails.

What you’ll need:

Gems:

1. ‘rest-client’ — Our http/rest client gem for accessing the url for our external api via get request.

2. ‘dotenv-rails’ — Our gem that will help us hide our api key.

Don’t forget to do a fresh bundle install after adding these to your gemfile.

An API Key:

For the purpose of this blog we’re going to use the NewsAPI to seed some top headline articles about coronavirus (since we know there will be tons of articles to test our seeds with). All you need to do is sign up with them and you can get a personal API key.

Hiding your API Key:

Once you’ve received your api key, you’ll want to add it to your backend. If you don’t already have a .env file in your root folder, create one.

Next, you’ll want to name your api key inside that .env file.

THE_BEST_NEWS_API_KEY = your_api_key_here

After that, you’re ready to move into your seeds.rb file.

Your Seed File:

Okay, those first few steps have been the set up, and now we’re getting into the nitty-gritty. We can get started by putting require ‘rest-client’ at the top of our seeds. Next, we’ll need to bring in that api key that we just defined in our .env file. The following method should do the trick.

def best_news_secret_key
ENV["THE_BEST_NEWS_API_KEY"]
end

We’ll need this to be a string so we can put it properly into our URL.

Now, let’s build out our method that will do the seeding. The numbers added at the beginning of the lines will be detailed below.

def news_dataset    1. api_data = { key: best_news_secret_key }    2. news = RestClient.get("https://newsapi.org/v2/top-headlines?   q=coronavirus&apiKey=#{api_data[:key]}")    3. news_array = JSON.parse(news)["articles"]
4. news_array.each do |s|
Story.create(title: s["title"], author: s["author"], content: s["content"], url: s["url"])
end
end
5. news_dataset()
  1. We create an object with a key that’s set to the method we created in the previous step.
  2. Here is the fetch to our api. The url I put here is shaped to search top headlines, with a query of coronavirus, and then an interpolation of our api_data object we created in step one. Definitely check out the NewsAPIs Documentation because they give a ton of great examples on how to build your url.
  3. We create a variable that is our response, parsed into JSON at the “articles” array. (You might want to console.log/byebug your response here to see how your data is coming through before moving on.)
  4. Now that you can see the data you’re receiving in your console/terminal, let’s go through that response and take out the article data that we wanted. Here we’re using the .each method to do so.
  5. Invoke the news_dataset() method so it triggers on rails db:seed.

If everything works out, you should be able to use rails db:seed and get a whole bunch of article data seeded from the NewsAPI! I hope you found this useful! Happy Planting!

--

--

Kevin Gleeson

Software Engineering Graduate from Flatiron School. Former expatriate and music teacher.