Laravel 12: Adding API Routes the Official Way

πŸ“– 2 minutes read

Laravel 12 removed routes/api.php from the default skeleton to keep new projects lean. When you’re ready to build APIs, here’s the modern way to add API support.

The Old Way (Laravel 10 and Earlier)

Every fresh Laravel install came with routes/api.php pre-registered with the /api prefix. Most projects never used it, but it was always there.

The Laravel 12 Way

Run one command:

php artisan install:api

This installs:

  • routes/api.php with automatic /api/* prefix
  • Route registration in bootstrap/app.php
  • Laravel Sanctum for token-based authentication
  • Sanctum database migrations

After running it, any route you add to routes/api.php is automatically accessible at /api/{route}.

What Gets Generated

Inside bootstrap/app.php, you’ll see:

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',  // ← Added by install:api
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    // ...

It’s the same structure as before, just opt-in instead of default.

Manual Alternative (No Sanctum)

If you don’t need Sanctum (e.g., public APIs or custom auth), you can manually add API routes without installing the full package:

// bootstrap/app.php
return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',  // ← Add manually
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )

Then create routes/api.php yourself:

<?php

use Illuminate\Support\Facades\Route;

Route::get('/status', function () {
    return ['status' => 'ok'];
});

Routes in this file are automatically prefixed with /api.

Why the Change?

Most Laravel apps don’t need API routes. Removing them from the skeleton reduces decision fatigue for beginners and clutter for everyone else.

When you do need APIs, install:api gives you the full setup in one step instead of hunting for documentation.

Bottom line: Laravel 12 defaults to simple, adds complexity on demand.

Daryle De Silva

VP of Technology

11+ years building and scaling web applications. Writing about what I learn in the trenches.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *