Rails as an API

Amana
5 min readApr 5, 2021

I previously wrote about revisiting Ruby on Rails since graduating from bootcamp. I thought I would take a moment to go over another great feature that you can do with Rails. That is using it as an API only application.

Quick background:

Ruby on Rails is a model-view-controller framework written in *drumroll* Ruby. It’s a great framework to learn for early web application development because it gives you the ability to create your own backend as well as a frontend all in one framework. There’s no need to reach out and utilize two different languages because it’s all wrapped into Rails.

Now, if using a different frontend language is on your radar, or maybe you just want to pull from an API but you aren’t able to find one that is already built and fit your needs, Rails can handle that too.

Application Programming Interface, or API for short, is, put simply, code that receives requests and sends responses. A great example is actually from the very first program I created, Marijuana Strain CLI (command line interface). I wanted my program to search through multiple strains of Marijuana, find matching ones based on the users input and display that information back to them. I built this project in 2020 so you can imagine that are thousands of strains out there and for a first project, there’s no way I would have been able to gather all that information myself in two weeks time. So I did what any other Software Engineer would do, I went to Google.

There are tons of free API’s available online that provide developers with organized/detailed lists of many things. From shoes to restaurants and even Marijuana Strains. I found the perfect one at The Strain API’s site. Many of the API’s available will require you to create an account so that you can receive a unique API key. This key helps the creators of the API keep track of who is using their program, for what and also how often they might be pulling this information from their servers. If you have an API that you want to make public, those are a few things you would also want to take into consideration. If a user is making thousands of calls to your API daily, that could absolutely slow your server down and trickle down to other users. Ultimately impacting the user experience. Keep that in mind and also make sure to thoroughly read the sites instructions and rules for using their API. Also, don’t share your API Key!

The Strain API is a very detailed list of thousands of marijuana strains so I was easily able to filter out what I wanted for my program, which was to provide random strains to users based on type (Indica or Sativa), flavor and even by the desired effect for the user. All of this information was provided with the Strain API and I was easily able to filter through those results and return only what I wanted.

Now, what if you search the web endlessly and are unable to find a suitable API for your project? That’s where Rails comes in. You can create your own! Whether you only have a small amount of information you want to collect in your API or an extensive amount, you can easily do it with Rails.

The usage of Rails as an API starts immediately at creation. When you create your project, you want to flag it as an api only application. From the Rails documentation, it should look something like this:

The --api is the flag you want to use to cut out the features of rails you will not be using. This makes your Rails application even a little more lightweight. Feel free to remove any files and folders you know you will not need to use to enhance the organization of your API. I won’t dive into that now but you’ll be able to easily tell which ones you’re making use of and which ones are just hanging around as extra fluff.

After you create your project, the next big step is setting up your database. (I won’t go into much detail on this because it’s the same as a regular database with Ruby on Rails.)An API that I created for a Mood Room application (collection of music mixes I made), I had a videos table. Within that table I had things like vid_url, image_url, title, etc. Then I seeded those details into my seed.rb file. Simple enough.

The most important thing to do with your API is to display its contents.

This is taken from the projects Video Controller. Looks familiar but the render line is a bit new. With an API, you’re no longer using a View file to display information. You’re instead displaying your API collection and you’re rendering it as JSON data. This helps make the information look more presentable to the user. There are a few gems, like fast_json, that can help you dig a little deeper into how exactly your API is displayed, however, for this project I just used what was built into rails.

When you have your database and controller(s) up and running and rendering json correctly, you should have something like this, of course, it will vary with the information you choose to collect:

--

--