Why Use Rails for JSON APIs?
The first question a lot of people have when thinking about building a JSON API using Rails is: "isn't using Rails to spit out some JSON overkill? Shouldn't I just use something like Sinatra?". [Or Express.js?]
For very simple APIs, this may be true. However, even in very HTML-heavy applications, most of an application's logic lives outside of the view layer.
The reason most people use Rails is that it provides a set of defaults that allows developers to get up and running quickly, without having to make a lot of trivial decisions.
- apiup (opens new window) - Rails project template to create an API pre-configured for JSON:API and OAuth 2 authentication
- How to JSON:API Sandbox (opens new window)—a JSON:API compliant web service with OAuth2 authentication. Try the app hosted or download it locally.
- Rails: The Easiest Way to Create a Web Service (opens new window)—two-part blog series
- Getting Started with JSONAPI::Resources
- Build APIs You Won’t Hate (opens new window)—guidelines on API design practices
- Programming Ruby, aka "The Pickaxe Book"—a popular introduction to the Ruby language
- Effective Testing with RSpec 3 (opens new window)—features and practices for testing with RSpec
- Practical Object-Oriented Design (opens new window) — a guide to writing changeable and maintainable code, written in Ruby
- Blog posts
- In Relentless Pursuit of REST (opens new window) - RailsConf 2017 talk by Derek Prior showing how the constaints of REST help lead to simpler code and a better domain model
The following pages in the Guides are relevant to API development:
- Active Record—all the sections are useful; the first is Active Record Basics (opens new window)
- Action Controller (opens new window) —especially check out the "Rendering XML and JSON Data" section
- Routing (opens new window)—especially check out the "Resource Routing" section
- Securing Rails Applications (opens new window)
- Command Line (opens new window)
- API-Only Applications (opens new window)—how to disable Rails server-rendered mode to simplify your API app
- Supplemental APIs
- rspec-rails (opens new window)—a popular Rails testing library. For APIs, especially check out request specs
- Active Model Serializers (opens new window)—centralizes your logic to serialize models to JSON
- Doorkeeper (opens new window)—authenticate users with the OAuth2 standard
- Pundit (opens new window)—authorize users' access to read and write records
- JSONAPI::Resources (opens new window)—helps creating an API following the JSON:API spec. Maintained by the creators of JSON:API.
- jsonapi-rails (opens new window)—alternate JSON:API gem
- GraphQL (opens new window)—gem for creating GraphQL APIs
- Heroku (opens new window)—"platform as a service" with great Rails support
- Docker (opens new window)—deploy apps in containers to isolate them
- Capistrano (opens new window)—automate deploying Rails to servers
- Digital Ocean (opens new window)—easy cloud server platform