Give your task list a JSON API

Turn a list you could show in Blade into data another app can read. Try paging through two tasks before writing the route.

Request preview

What changes on page two?

Pick a request and compare the example responses. Each page starts with one task.

Choose an example request
GET/api/v1/tasks?per_page=1&page=1
Response excerptOK 200
{
  "data": [ { "id": 2, "title": "Buy seeds", "done": false } ],
  "meta": { "current_page": 1, "per_page": 1, "total": 2 }
}

The list has two tasks. This page holds one, with the newest task first.

GET/api/v1/tasks?per_page=1&page=2
Response excerptOK 200
{
  "data": [ { "id": 1, "title": "Water the plants", "done": false } ],
  "meta": { "current_page": 2, "per_page": 1, "total": 2 }
}

The task ID and current page changed. The page size and total stayed the same.

GET/api/v1/tasks?per_page=51
Response excerptValidation error 422
{
  "errors": { "per_page": [ "The per page field must be between 1 and 50." ] }
}

The route accepts 1–50 items per page. Laravel rejects 51 before it reads any tasks.

Check your work

With one task per page, compare the task IDs on pages one and two. Request 51 per page and find the field named in the error.

Back to roadmap

Build it locally

Laravel 13 · 20 min

Blade turns a task into HTML for a person to read. An API can send the same task as JSON so another app can use its title and completion status. Look in data for the tasks and in meta for your page number.

To build the example locally, start with the task list and form. Keep this first endpoint local while you work through the course. You will add access controls on the next course.

Install the API route file

Run php artisan install:api and php artisan migrate. Laravel assigns the /api prefix to routes/api.php. Use /v1/tasks inside that file so callers reach /api/v1/tasks.

Expose an explicit resource

Run php artisan make:resource TaskResource and replace the generated class with this. New columns will stay private until you deliberately add them to the JSON contract.

<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class TaskResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'done' => (bool) $this->done,
        ];
    }
}

Add the route to routes/api.php. Keep a single import for classes already present.

use App\Http\Resources\TaskResource;
use App\Models\Task;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/v1/tasks', function (Request $request) {
    $data = $request->validate(['per_page' => ['sometimes', 'integer', 'between:1,50']]);

    $tasks = Task::query()->latest('id')->paginate((int) ($data['per_page'] ?? 15));

    return TaskResource::collection($tasks->withQueryString());
});

Practice and verify

Run php artisan route:list --path=api/v1/tasks. Start the app and request /api/v1/tasks?per_page=1 with Accept: application/json. With tasks in the table, data should contain one item and links and meta should be present. Follow the next link if there is another row. A value of 51 must return 422. Keep sorting explicit so callers see a repeatable order.

Check my work