API with Ruby on Rails

API with Ruby on Rails: useful tricks

This article is about gotchas you should be aware of while building your API with Ruby on Rails.

Controller tricks: API on Metal

Sooner or later each Rails developer come to a point when he wants to build his first API. Among the first things you have to take care of are your controllers. If you want your API to be fast (and I bet you do) then you should consider using ActionController::Metal. The trick is that ActionController::Base has many Middlewares that are not necessary for the API, by using Metal controller with minimum modules included, the one can achieve up to 40% speedup. Lets see what your basic metal controller may looks like:Unfortunately NewRelic doesn’t support Metal by default, so you have to add monitoring manually.

Routing: use versioning

Nobody’s perfect. So are we. Your API will definitely be changed and extended multiple times in the future so you better take care of your versioning at the beginning. As you noticed, BaseController wrapped in V1 namespace. Use something like this in your routes:

Views tricks: RABL ’em all

You don’t want to burden your code with logic of exposing different model fields for different API actions, right? In this case you should use some template engine. RABL is at your service. Here’s an example of your view:Also that will save you time by getting rid of ugly respond_to blocks. Instead ofYou can simple do literally NothingJust make sure you have a RABL view in a corresponding directory.


There’re plenty of articles about securing your API with OAuth. Another convenient way is to simply use token passed in the query string. If you ended up with token keep in mind that you can easily generate it by calling SecureRandom.urlsafe_base64. To make sure token is unique you can use something like this:

Hiding your IDs with GUIDs

By default Rails uses incremental integer for primary key. Common practice is not to expose these kind of IDs to the public via your API because users can guess other IDs in your database and that might be a potential risk. To solve this you can come up with a simple algorithm that will convert your IDs into some “safe” form and back. But still it’s not super safe because someone can find out what the algorithm is. Another possible solution is to expose GUIDs to the public. It’s a 128 entity that typically looks like this:To generate it in Ruby use SecureRandom.uuid (generates V4 GUID). You can store it as a simple string column but if you are using Postgresql then you can utilize it’s built in uuid type – this can save you a lot of space. Rails, however falls back to :string for the uuid Postgresql native type. To workaround this you can create column manually in your migration (index should be added as well):To use uuid_generate_v4 function you have to add Postgresql extension:And don’t forget to change your schema format to :sql in config/application.rb

Testing your API

You definitely want to cover up your new shiny API with some sort of tests. See example below that uses Rack::Test::Methods


We’ve just gone through the following tricks:
  • Minimum usable Metal controller
  • Simple versioning
  • Easy API views with RABL
  • Postresql native uuid type
  • API spec sample
Sure, you’ll face a lot more issues in the wild but these basics intended to help you start up quickly. The last, but not the least – don’t forget about good documentation. Your API users will definitely appreciate it.If you want to try all these tricks in the wild see recently released API for our dummy SMTP server – Mailtrap.ioUPDATE If you want to use render json: your_json you should include ActionController::Renderers::All in your base API controller. Thanks Divya Kunnath for pointing that out.