Discover Our Blog

Implementing RESTful API in Ruby on Rails

Implementing RESTful API in Ruby on Rails

What is RESTful API

A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data. Representational state transfer (REST), which is used by browsers, can be thought of as the language of the Internet.

Steps in creating RESTful API: Step 1: Creating an API

While creating an API, the controller is kept in a separate folder named api inside app/controller of the ROR application.

Step 2: Creating a Versioning Folder

Once an api folder is created, we create a versioning folder under api folder. This versioning folder is created according to the API requirement. An existing API should never be modified, except for critical bugfixes. Rather than changing existing API we create another version of that API.

For ex., app/controller/api/v1 or app/controller/api/v2

Step 3: Creating a Controller File

Create a controller file inside the api folder like home_controller.rb

Step 4: Creating Controller Class

Inside the controller file we need to create the controller class. This controller class will contain the method which is used while calling an API. Basically, the method inside the controller is nothing but a business logic of the requirment of an API. So, this method is defined to run as per the requirement of an API. This method will return the JSON format data. For that we need to render the result into JSON format.

The above steps deals with the controller /api folder. Now, if someone wants to call a particular api method, for that we need to define in routes.

The routes is used for calling an API methods defined inside the config/ routes.rb file of the ROR application.

Route Example :

namespace :api do

resources :home, defaults: { format: 'json' } do

collection do

get 'business_details&:category_service&:city', to:'home#business_details'




So, our calling URL for above step should be http://localhost:3000/api/home/business_details&Food andRestaurants&USA

And result should be

{“name”:“MacDonalds”, “address”:“New York, USA”}


  1. This one is very helpful for beginners in ror technology. thank and put some other helpful like this.

Leave a comment: