Rails as an API

Ruby on Rails

Jonathan Zavala
4 min readJul 1, 2021

Ruby on rails is a framework built on top of Ruby the programming language. It was made to make developing web applications easier and faster. Rails can be used to make full stack applications, but it’s not very scalable there for it is mainly used as an backend API. Now you might ask what is an API well we’ll jump into that!

What is an API?

API stands for “Application Programming Interface” if we look at the acronym broken down we can kind of get an idea of what it is. The main word would be “Interface” the definition of interface is “a point where two systems, subjects, organizations, etc. meet and interact.” which means an API is a software that allows two applications to talk to each other! You use APIs’ everyday wether it be on the weather app or Facebook. You can also think of an API as the waiter/waitress at a restaurant they communicate with the “system” or kitchen tells them what you want and brings it back to you!

Rails as an API

Now that we understand what an API is let’s look at why and how we use rails as an API. The reason we use it as an API is because in heavy HTML applications the logic of the application would like outside of the views and as well all know separation of concerns is something we all would like to follow. So using rails for JSON APIs is the better way to go if you’re thinking of a big web application. Rails just provides so many things right off the bat that can be used for JSON APIs. Some of the things include:

  • Parameter Parsing: Want to specify your parameters as JSON instead of as a URL-encoded String? No problem. Rails will decode the JSON for you and make it available in params. Want to use nested URL-encoded parameters? That works too.
  • Reloading: Rails applications support transparent reloading. This works even if your application gets big and restarting the server for every request becomes non-viable.
  • Logging: Rails applications log every request, with a level of verbosity appropriate for the current mode. Rails logs in development include information about the request environment, database queries, and basic performance information.
  • Conditional GETs: Rails handles conditional GET (ETag and Last-Modified) processing request headers and returning the correct response headers and status code. All you need to do is use the stale? check in your controller, and Rails will handle all of the HTTP details for you.
  • Generators: It is often handy to generate a resource and get your model, controller, test stubs, and routes created for you in a single command for further tweaking. Same for migrations and others.
  • Resourceful Routing: If you’re building a RESTful JSON API, you want to be using the Rails router. Clean and conventional mapping from HTTP to controllers means not having to spend time thinking about how to model your API in terms of HTTP.
  • Caching: Rails provides page, action, and fragment caching. Fragment caching is especially helpful when building up a nested JSON object.
  • Basic, Digest, and Token Authentication: Rails comes with out-of-the-box support for three kinds of HTTP authentication.

These are just some of the things that rails gives you!!

How To Start Up a Rails API

We all know how to start a new rails application:

rails new some_project

but what if we want it to just be an API and not have any of that extra stuff we don’t need (views, etc.). Welp it’s not that much different.

rails new some_api --api

As little as the command changed the rails backend changed a-lot! It configured your application to start with a limited set of middleware. Make the ApplicationController inherit from ActionController::API and configured the generators to skip views, helper, and assets when generating resources.

Conclusion

In this blog we briefly went over rails, explained what an API is and gave some example. Then we dove deep into rails as an API, why to use it and all the things it changes in the set up and generation of the application. I hope this cleared up and questions you might’ve had concerning rails an API!!

--

--