A JSON API in Rails
rails new --api
--api generates a smaller Rails: no views, no asset pipeline, and a base controller that expects JSON. The commands need the gem, the network and a compiler, so they ran on the host in the ruby:3.4 image; the gem install rails output is in lesson 10.11.
gem install rails
rails new api --api
cd api
The model and the route
bin/rails generate model Todo title:string done:boolean
bin/rails db:migrate
The generator writes a model with no rules. The block adds the base class the API application supplies, so it runs on its own:
require "active_record" class ApplicationRecord < ActiveRecord::Base primary_abstract_class end class Todo < ApplicationRecord end
With that model an empty title is a valid row, and the error branch of the controller never runs. The host sent one; the id is 2 because a valid row with id 1 existed in the same run:
curl -s -w ' [%{http_code}]' -X POST localhost:3000/todos -H 'Content-Type: application/json' -d '{"todo":{"title":""}}'
{"id":2,"title":"","done":null,"created_at":"2026-11-05T00:43:05.177Z","updated_at":"2026-11-05T00:43:05.177Z"} [201]
Add the rule the API promises:
require "active_record" class ApplicationRecord < ActiveRecord::Base primary_abstract_class end class Todo < ApplicationRecord validates :title, presence: true end
Two routes are enough for a list and a create. The only: option keeps the CRUD surface from growing by accident:
Rails.application.routes.draw do resources :todos, only: [:index, :create] end
A controller that renders JSON
render json: serialises the value with to_json and sets the content type. The status is the part that a client checks, so the success and the failure each say it:
class ApplicationController end class TodosController < ApplicationController def index render json: Todo.order(:created_at) end def create todo = Todo.new(todo_params) if todo.save render json: todo, status: :created else render json: { errors: todo.errors.full_messages }, status: :unprocessable_entity end end private def todo_params params.require(:todo).permit(:title, :done) end end
status: :created is 201 and :unprocessable_entity is 422. A client that looks only at the body cannot tell a created row from a rejected one, which is why the status is not optional. The empty base class stands in for the application's own ApplicationController, so the excerpt runs on its own.
Two requests
bin/rails server
The validation run started from an empty database, so the first list is empty:
curl -s localhost:3000/todos
[]
A valid title creates a row. The -w option appends the status, which is the part a client checks:
curl -s -w ' [%{http_code}]' -X POST localhost:3000/todos -H 'Content-Type: application/json' -d '{"todo":{"title":"write the lesson"}}'
{"id":1,"title":"write the lesson","done":null,"created_at":"2026-11-05T00:45:53.484Z","updated_at":"2026-11-05T00:45:53.484Z"} [201]
The same request with an empty title now reaches the error branch:
curl -s -w ' [%{http_code}]' -X POST localhost:3000/todos -H 'Content-Type: application/json' -d '{"todo":{"title":""}}'
{"errors":["Title can't be blank"]} [422]
curl -s localhost:3000/todos
[{"id":1,"title":"write the lesson","done":null,"created_at":"2026-11-05T00:45:53.484Z","updated_at":"2026-11-05T00:45:53.484Z"}]
The done column is null because the generated migration gives it no default. Model the column as t.boolean :done, default: false and a new row starts as not done, which is what the application means.
The server log shows the pair as Completed 201 Created in 103ms and Completed 422 Unprocessable Content in 13ms. A body whose title is empty returns the 422 from the controller with the validation messages, while a body that is not valid JSON fails before the controller runs and returns a 400. An unknown route returns the Rails 404. All are worth one curl each, because a client that only sees the happy path discovers them in production.
What to watch for
API mode does not include CORS. A browser client on another origin needs
rack-cors and an explicit list of origins; the
failure looks like a network error in the console, not a 4xx.
render json: model exposes every column the model
has. A column added later is published without a decision, so a public
API renders an explicit hash or a serializer and keeps the schema private.
This is the last lesson of the chapter. The next chapter starts with Gems and Bundler.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does this program print?
require "json" puts({ id: 1, title: "write the lesson" }.to_json)Answer
{"id":1,"title":"write the lesson"}
to_json writes compact JSON, and the symbol keys become quoted names.
-
What does this program print?
require "json" errors = ["Title can't be blank"] puts({ errors: errors }.to_json)Answer
{"errors":["Title can't be blank"]}
The array stays an array in JSON, which is the shape the error branch renders.
-
What status does
:createdstand for, and what does:unprocessable_entitystand for?Answer
201 and 422
A created row answers 201, and a body that fails validation answers 422.
-
What does
rails new api --apileave out compared with a full application?Answer
Views and the asset pipeline
API mode generates a smaller app whose base controller expects JSON instead of rendering views.
-
What does
resources :todos, only: [:index, :create]route?Answer
Only the list and the create actions
The only option keeps the CRUD surface small, so the other actions are not routed.
-
What does
render json: todo, status: :createddo?Answer
Writes the model as JSON and sets the 201 status
The call serialises the value and sets the status, which is the part the client checks.
-
What does this program print?
require "json" body = '{"todo":{"title":"write the lesson"}}' data = JSON.parse(body) puts data["todo"]["title"]Answer
write the lesson
The nested object becomes a hash with string keys, and the title is read by its name.
-
Why is the
donevalue null in the responses of the example?Answer
Because the generated migration gives the column no default
A column with no default starts as null, which is why the page suggests default: false.
-
What does the page warn about
render json: model?Answer
It publishes every column, including one added later
A new column is exposed without a decision, so a public API renders an explicit hash or a serializer.
-
What does an API-mode application need before a browser client on another origin can call it?
Answer
The rack-cors gem with an explicit list of origins
API mode leaves CORS out, and the failure looks like a network error in the browser console.
Article author: Arthur Isaev