Building a JSON API with Rails – Part 1: Getting Started


Foreword:

This series has been rewritten as of November 11, 2016 based on the new API features in Rails 5. Formerly, this post covered the use of the rails-api gem, which has now been merged into core Rails 5.

If you want to skip all the reading and just see an example API built using the exact technologies I’ll be discussing in this series of posts, check out my GitHub repo over a Ruby talk I gave where we live-coded a full API.


Table of Contents


How can you go about supporting your Angular/Ember/Backbone app, iOS app, Android app, Windows phone app, etc., all with a single back-end? Here’s how: by breaking up your backend server into its own API service, and Rails is just the framework to help make it simple and awesome.

This service separation is dubbed Service Oriented Architecture (SOA), and by following it and building a JSON API that’s separate from the front-end, you’ll be able to support all of your related apps with this single service and keep your back-end incredibly simple, logical, and easily maintainable.

In order to build a solid API, we’re going to break our goal up into 3 different tasks:

  • Build out our models and our data
  • Serialize our data
  • Add Authentication

Adding authentication is always an optional step, but most of the time you’re going to want to prevent unauthorized users from accessing your API. We’re going to tackle just the first task in this post, and save the rest for the next two parts. Ready to start? Good.

Starting Out

First off, we install rails similarly to how you’ve always done it – but with the new Rails 5 API flag.

As you can probably see, we’re going to be creating a blog, and it will have the following database structure:

  • User has many Posts
  • User has many Comments
  • Posts has many Comments

Building the API

Just a simple 3-table database will suffice, so let’s use the rails generators to scaffold out our 3 models:

If you’re familiar with Rails, then this looks very familiar to you. In fact, you can probably tell very easily what each attribute’s purpose is – except for auth_token perhaps. We’re going to make use of that attribute later on when we discuss and build in authentication, so don’t worry about it for now.

After you migrate your database, then you have a fully functioning API! Start up your local rails server and navigate to

to see your API live. It should look like an empty array set, which is what we want – just pure JSON.

Seeding the Database

If you want to add some seeds to populate your database, you can add this code to your db/seeds.rb file:

Now, all you need to do to run these seeds is update your User and Post model files with the necessary has_many relationships like so:

And then run the seed command to pre-populate your database:

Now your database has real data!

Issuing Requests

Because we scaffolded our resources, we created controllers that are fully capable of handling the standard HTTP requests types: GETPOSTPUT/PATCH, and DELETE. If you’re a little unfamiliar with these names, you can map it to the common CRUD acronym:

  • Create (POST)
  • Read (GET)
  • Update (PUT/PATCH)
  • Delete (DELETE)

The URLs for issuing any of these requests are:

You can obviously issue GET requests by visiting these pages in your browser, or you can use the curl command from your terminal (or similar command) to issue any of these requests. Everything will work as expected.

That’s it?

No, of course that’s not it, but look at what we’ve done so far – we’ve built a relational database with a fully functioning JSON API on top that can handle any of the 4 main request types, and we did it in practically no time flat. We have a lot more to talk about such as serialization, authentication, and an overview post discussing some of the bigger questions that come up when you’re building an API, so if you’re ready, feel free to move onto part 2.

Happy API Building!

A Genuine Request

Hello there - thank you for reading my post! If you enjoyed it, I'd love it if you considered supporting me. I'm not asking for money or any of that "buy me a beer" crap; I would just love it if you would consider downloading (and using) the Brave browser using my referral code!

The Brave Browser Brave has a focus on security and privacy - and is hardcore against ads. That means that sites like mine wouldn't generate any revenue if you viewed them in Brave - but that's okay; I only make about $10 / month with ads, and that goes right to hosting. If you use my referral code to download Brave and use it for a bit though, I get about $5 with Brave's currency - Basic Attention Token, or BAT for short.

Whether you consider using it or not, I'm just glad you read this post. Thanks again!

Referral Code: https://brave.com/the124