What's the difference between a Rails application and a Rails API ?
Tags: rails, ruby
Published: 2020-02-25 12:23:58

---
title: What's the difference between a Rails application and a Rails API ?
published: true
tags: rubyonrails, rails, ruby
---

Ruby on Rails provides the `new` command to create a web application; there's also the `--api` option available for creating [an API application](https://guides.rubyonrails.org/api_app.html), which won't need much of the frontend capabilities that a full-stack application would.

![Welcome screen of new Ruby on Rails applications](https://guides.rubyonrails.org/images/getting_started/rails_welcome.png)

You may wonder why Rails is still a good choice for an API, which are supposed to be lightweight and machine-orientated rather than comfortable and human-orientated. The answer given by _Rails Guides_ may resonate with anyone that has developed a Ruby application of a decent size without using Rails or other frameworks:

> You may not have thought about which parts of Rails are still applicable even if you remove the view layer, but the answer turns out to be most of it.

The features highlighted include:
* tools like Rack and the Rails Router to handle the HTTP layer smoothly
* configuration and integration of database connections
* dynamically generating URLs for resources
* caching, at various levels
* authentication
* instrumentation, both within Rails and from integrated, third-party tools
* auto re-loading
* multi-environment configuration

There's a lot that a Rails API can offer over a plain Ruby alternative! Some frameworks are built for lighter web and/or API applications, but here we're going to concentrate on the two Rails offerings.

## What are the differences? :mag:

Rails Guides describes the API application as:

> [a] Rails application that serves JSON resources to an API client, including client-side frameworks.

I've put together a Git repository to demonstrate:
* commit [6ced3f81](https://github.com/seawolf/rails-api-comparison/commit/6ced3f8152f11d976a129aac756740d4ff72a97e) shows what a new API application looks like
* commit [4d52ac32](https://github.com/seawolf/rails-api-comparison/commit/4d52ac3277f7e2e226bf98a46ff4293ce565b1cf) shows what a new web application looks like
* commit [04db2c29](https://github.com/seawolf/rails-api-comparison/commit/04db2c291993273ef84e41bb7e6984e094078d66) shows the difference between a web and an API application
* commit [2665ded0](https://github.com/seawolf/rails-api-comparison/commit/2665ded050253b4c72d2cf07a1337751ed5a4acd) shows the difference between an API and a web application

The last two are of course simply opposites, but this provides a useful guide for converting one into the other.

Let's take a look inside the commits to see what differences there are between the web and API applications!

### Gems :gem:

![Configuration diff from Git](https://dev-to-uploads.s3.amazonaws.com/i/3g5wjob7wwpi8d5qb5wy.png)

This may not be surprising, but there were the assets-related gems removed, and a hint for some CORS support added. Without the frontend driven by a browser, there's no use for the [`web-console`](https://rubygems.org/gems/web-console), nor testing with [`capybara`](https://rubygems.org/gems/capybara) and friends.

### Configuration :gear:

![Gemfile diff from Git](https://dev-to-uploads.s3.amazonaws.com/i/65v8xs2fjpwe9g8jinme.png)

Rails API applications are very similar to the full web counterpart. A noticeable difference is how individual components are specified in the application's boot sequence.

the main change is, of course, enabling `Rails::Application#api_only` to trigger some changes to the internals, like skipping sessions and cookies support unless otherwise required.

### Application Layers :link:

Following the removal from the Gemfile, the `app/assets` directory is removed. There is no need for a layout nor the `public` pages, as the controllers given an unfulfillable request will serve the 404/422/500 HTTP status, perhaps with the error in a plain-text body content or none at all.

The documentation shows how the main controller differs:
> inside app/controllers/application_controller.rb, instead of:
> 
> ```ruby
> class ApplicationController < ActionController::Base
> end
> ```
>  
> do:
> 
> ```ruby
> class ApplicationController < ActionController::API
> end
> ```

I'm not sure what impact this has, as we haven't built anything on top of the application scaffold. Maybe this can be an exploration to take in another post!

### Testing :white_check_mark:

It interested me that the [MiniTest](https://rubygems.org/gems/minitest) scaffolding was not present in a Rails API application. I presume this is to allow complete freedom for the developers, or the Rails team didn't agree on a default!

## Thanks For Reading! :heart:

I hope you've enjoyed exploring what impact the innocuous `--api` flag can have on a new Rails application!

Have you started with a Rails-based API and moved it to a full web application, or vice-versa? Do you consider Rails for API applications, or prefer other approaches? Let me know in the comments below!