1. What is an API-only Application ?
Instead of using Crails to generate HTML that communicates with the server through forms and links, some developers are treating their web application as just an API client delivered as HTML with JavaScript that consumes a JSON API.
This guide covers building a Crails application that serves JSON resources to an API client, including client-side frameworks.
2. The Basic Configuration
If you're building a Rails application that will be an API server first and foremost, you can start with a more limited subset of Rails and add in features as needed.
2.1 Creating a webservice application
You can generate a new api Crails app:
$ crails new -n my_api --configuration webservice --formats json
The webservice
configuration creates an application with a more limited set of middleware
than the default configuration. Specifically, it will not include any middleware primarly useful for
browser applications, such as cookie support, or form and multipart parsers.
The formats
option allows you to pick which formats your webservice will use to interact
with its clients: parsers and view generators for the given formats will be included in your application.
Available options are html
, json
and xml
. You
can combine these options, such as: --formats json,xml
.
2.2 Creating a barebone application
Crails also come with a barebone option for application generation. This will take away any non essential part of the crails framework from your new application, and leave it to you to add the components needed by your application. You can create a new barebone Crails app as such:
$ crails new -n my_barebone --configuration barebone
The generated application will only feature the Request Pipeline, which can be useful if you don't even plan on relying on a Router: you are free to add your own request parsers and handlers, or to manually add crails parseres and handlers as you see fit.