Designing REST APIs for mobile applications
What a mobile client actually needs from an API, and what makes an app feel slow when it does not get it.
An API designed for a web dashboard and an API designed for a phone are not the same API. The phone is on a slower network, has a battery, gets backgrounded mid-request and sometimes has no connection at all.
These are the things I care about when I am on either side of that contract.
Fewer round trips beats a pure resource model
A product screen needs the product, its images, its variants, its stock state and maybe the seller. Three or four requests to render one screen is fine on a laptop on wifi. On a 3G connection it is a visibly slow app.
One endpoint that returns what the screen needs is not "unRESTful design", it is knowing your client. If you want it to stay flexible, let the client ask:
GET /products/123?include=variants,seller
Cursor pagination, not offsets
{
"data": [ ... ],
"next_cursor": "eyJpZCI6MTk4fQ"
}Offset pagination breaks the moment new items are inserted while the user scrolls. They see duplicates, or miss items entirely. It also gets slower the further down they go. Cursors have neither problem, and infinite scroll never needs to jump to page 40 anyway.
Consistent errors, always the same shape
{
"error": {
"code": "insufficient_stock",
"message": "Only 2 left in stock",
"field": "quantity"
}
}A machine-readable code the client switches on, a human message it can show, and an
optional field for forms. What kills mobile clients is an API where a validation error
is one shape, a server error is another, and a gateway timeout is HTML.
Pick a shape. Never break it.
Do not send formatted strings
Send "total_cents": 45000 and "currency": "BDT", not "total": "৳450.00". Send ISO
8601 UTC timestamps, not "2 hours ago". Formatting is a client concern, and the moment
you add a second locale the server-formatted string becomes a bug.
Version from day one
/v1/ in the path. It costs nothing today. Without it, the first breaking change means
either breaking every app version still installed on people's phones, or a very awkward
header scheme retrofitted under pressure.
Remember that mobile clients do not update together. Old versions of your app will be calling your API for years.
Idempotency for anything that costs money
The network drops after the server processed the payment but before the client saw the response. The client retries. Now there are two charges.
POST /v1/orders
Idempotency-Key: 8f14e45f-ea0f-4b8e-9f2e-1c3d5b7a9c11
The server stores the key with the response and returns the same response for a repeat. The client generates the key once per user intent, not once per attempt. This is not optional for payments. It is the difference between a retry and a refund.
Nulls and optional fields
Adding a field is fine. Removing one, renaming one, or changing a type is a breaking change for every installed app. Clients should ignore unknown fields, and servers should never repurpose an existing field for a new meaning.
The worst version of this I have seen: a field that was a string in one response and an object in another, depending on the state. Every client ends up with a special case, and the parsing code becomes something nobody wants to touch.
Think about what happens offline
The API cannot fix offline, but it can make it easier: stable ids generated by the
client for created resources, updated_at on everything so a client can sync deltas,
and endpoints that are safe to call twice.
The short version
Design for the screen, keep the error shape fixed, never send formatted values, version early, and make writes safe to retry. Almost every "the app feels slow" complaint I have chased traced back to one of those.