With the increasing amount of smartphone users all over the world, the wise thing to do is to have a mobile app strategy that can bring your service or product to this growing market. However, you may also wish to have a presence on the desktop via a web app, maximizing your overall reach. These days it is almost expected of any web app to have a mobile app version, and you can't always just rely on responsive design.
Why Create an API?
The case of having a web and a mobile app presents several complications. One of the issues is the need to have a standardized and centralized way to store data coming from both the web and the mobile. This can be useful, amongst other things, to avoid repetition. You can save some developer time. by just creating a storage layer once, as an API, and reusing it with multiple application versions. The only variation would be the presentation of the app. Whether on the web, iOS, Android or anything else, you'd only need a way to communicate with your API and presto, you have your centralized data storage.
An API is related to the concept of SOA (Service Oriented Architecture), which is basically a pattern for designing an application formed by various pieces, each one with a different, specific functionality. All the pieces communicate using a defined public interface to provide functionality to the whole application. This allows for high interoperability between applications, and a low coupling between the pieces, which means better maintainability.
These are sufficient reasons for aiming to create an API, even if you're only creating a web app. You're “eating your own dog food”. Maybe at first you will only use your API privately, but you'll be ready later for publishing a subset of that API to provide external developers a way of extending your ecosystem with their own web or mobile apps.
Why REST for Mobile Apps?
REST (Representational State Transfer), is a protocol for making requests to a web endpoint by using HTTP and it's verbs (GET, POST, PUT, DELETE). It's generally used in conjunction with JSON, which is a lightway data format. It offers the benefit of a lightway data transfer, in contrast with the way SOAP works using XML, which is far more verbose.
In addition, REST has the properties of being stateless, cacheable, and possibly layered, which all contribute to one of it’s key goals, scalability. You can easily have you REST service distributed between several load-balanced servers, and even save precious CPU cycles by reusing already generated data in some requests.
The Basics of REST
As mentioned, JSON is recommended for data transfer in a REST API, so you’d be sending and/or receiving JSON data in each request.
Let's say you have an accounts resource. You'd have the following REST API:
GET /accounts Returns the list of all accounts
GET /accounts/:id Returns a single account
POST /accounts Creates an account
PUT /accounts/:id Updates an account
DELETE /accounts/:id Deletes an account
You may need some form of authentication for a subset of your API. This can be HTTP Basic, or maybe token based authentication.
There are many possible error codes for an HTTP response, but the most common are these:
200 Response OK
201 Resource created
404 Resource not found
500 Server error
You can easily implement a full API using just these concepts. Although once you get going, you’ll want to dig deeper into what you can do. Maybe, you’ll need to use a few other HTTP response codes or maybe you need to implement some form of OAuth for the authentication part. You can figure this out for your software development and mobile app projects along the way.
REST is good for you. Currently, it’s almost a given that if you have a web app, you need to have a companion mobile app. Even if you only have the mobile version, it's nice to have a centralized data storage. REST can help you accomplish this.
About the Author
Lenin Yee is a Software Developer with 4+ years of experience in web development and general Linux server administration. He is an Appcelerator Titanium Certified Developer (TCD) and currently works at iTexico as a Software Engineer.