Build a Laravel REST API for a Mobile App: Versions, Resources and JSON Errors
A mobile app is a client you do not control. Once it is in the app stores, old versions stay in use for months, so the API behind it has to stay stable and predictable. This guide shows how to structure a Laravel API for a mobile app: versioned routes, resources for the responses, and errors that always come back as JSON.
What this article is, and is not
This is a structure guide for the API side only. It does not cover building the mobile app itself. The examples use a simple orders API; adapt the names to your platform. For login, see the Sanctum token guide in this series.
1. Put everything under a version
// routes/api.php
Route::prefix('v1')->group(function () {
Route::post('/login', [AuthController::class, 'login']);
Route::middleware('auth:sanctum')->group(function () {
Route::apiResource('orders', OrderController::class)->only(['index', 'show', 'store']);
});
});
Laravel already prefixes routes/api.php with /api, so this gives you /api/v1/orders. When you must make a breaking change, add /v2 and keep /v1 alive until old app versions have faded out.
2. Return resources, never raw models
public function show(Order $order)
{
$this->authorize('view', $order);
return new OrderResource($order->load('items'));
}
A resource fixes the shape of the JSON, so adding a database column cannot silently change what the app receives.
3. Make every error a JSON error
By default a failed request from a client that does not send Accept: application/json can be redirected or return an HTML page. Mobile code does not expect that. Tell Laravel to always render JSON for API routes:
// bootstrap/app.php
->withExceptions(function (Exceptions $exceptions) {
$exceptions->shouldRenderJsonWhen(
fn (Request $request, Throwable $e) => $request->is('api/*') || $request->expectsJson()
);
})
Validation errors then arrive as a 422 with an errors object, an unauthenticated call as a 401, and a missing record as a 404, all as JSON.
4. Use status codes properly
- 200 for a successful read, 201 when something was created, 204 for success with no body.
- 401 when the token is missing or invalid, 403 when the user is known but not allowed.
- 404 when the record does not exist, 422 for validation errors, 429 when rate limited.
The app can then decide what to do from the code alone, for example sending the user back to the login screen on a 401.
5. Add rate limiting
RateLimiter::for('api', fn (Request $request) =>
Limit::perMinute(60)->by($request->user()?->id ?: $request->ip()));
Apply it with throttle:api. Use a stricter limit on the login route.
6. Ask the app to send its version
Have the app send a header such as X-App-Version. You can then log it, and later return a clear "please update" response for versions you no longer support, instead of letting them break in confusing ways.
7. Test the contract
Sanctum::actingAs($user);
$this->getJson('/api/v1/orders')
->assertOk()
->assertJsonStructure(['data' => [['id', 'number', 'status', 'total']]]);
Common mistakes
- Shipping an API without a version and then being unable to change it.
- Returning models directly, so private columns leak.
- Returning 200 with an error message in the body. Use a real status code.
- Forgetting that old app versions keep calling your API for a long time.
Summary
Version the routes, return resources, force JSON errors, use proper status codes, rate limit, and test the shape of every response.