Build Your First API in Laravel 12 (Step by Step Guide)
Laravel 12: Tutorials, Guides & Best Practices
Author
Richard Mendes
June 24, 2026 • 5 Mins

Build Your First API in Laravel 12 (Step by Step Guide)

How to Create API Routes in Laravel 12 from Scratch

In Laravel 12, API routes are no longer always available by default in every fresh installation. To enable API support, you need to use the php artisan install:api command, which sets up routes/api.php and installs Laravel Sanctum for authentication.

Run the API install command

php artisan install:api

What this command does

When you run this command in Laravel 12, Laravel automatically sets up everything needed for API development.

Installs Laravel Sanctum

Laravel 12 automatically installs the Laravel Sanctum package when you run php artisan install:api. This adds laravel/sanctum to your project, which is used for API authentication. Sanctum enables token-based login systems and allows you to protect API routes such as /api/user, ensuring that only authenticated users can access certain endpoints.

Updates Composer dependencies

Laravel also updates your project’s Composer dependencies during the API installation process. It runs the composer update laravel/sanctum command, which downloads and installs all required packages for Sanctum. At the same time, it updates the composer.lock file to ensure the correct version of the package is locked in your project. This guarantees that Sanctum is properly registered and fully integrated into your Laravel application.

Registers Laravel packages

Laravel refreshes the application after installation and automatically discovers all installed packages in the project. During this process, it registers packages such as Sanctum, Sail, Tinker, Carbon, and others. This step ensures that Laravel is fully aware of all available packages and their features, allowing them to work properly within the application without any manual configuration.

Published API routes file

Laravel created:

routes/api.php

This is where all your API routes will live.

Created database migration

Laravel generated a new migration:

create_personal_access_tokens_table

This table is used by Sanctum to store API tokens for users.

Asked to run migrations

One new database migration has been published. Would you like to run all pending database migrations? (yes/no) [yes]:

Type yes, it will create the table in your database.

You can also type the command:

php artisan migrate

Creating Your First API Route

Now that API support has been installed, let's create a simple API endpoint to verify everything is working correctly.

If you haven't created a Laravel 12 project yet, you can follow our Laravel 12 Docker setup guide before continuing.

Open the routes/api.php file that was created during the installation process and add the following route:

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
//Add ApiController to the list of imports
use App\Http\Controllers\ApiController;

Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:sanctum');

Route::get('/api', [ApiController::class, 'index']);

Creating an API Controller

Create a new API controller by running the following command:

php artisan make:controller ApiController

Open the controller and add the following method: in app/Http/ApiController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ApiController extends Controller
{
public function index()
{
return response()->json([
'success' => true,
'message' => 'Hello from Laravel 12 API!',
'framework' => 'Laravel 12'
]);
}
}

Testing the Endpoint

Start your application and visit:

http://localhost:8000/laravel/public/index.php/api/myapi

If everything has been configured correctly, Laravel will return the following JSON response:

{
"success": true,
"message": "Hello from Laravel 12 API!",
"framework": "Laravel 12"
}
Popular Posts

Comments (0)

No comments yet. Be the first to comment!

Latest Articles