Rails as an API

Ruby on Rails

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:

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!!