Build Your First API in Laravel 12 (Step by Step Guide)
Laravel 12: Tutorials, Guides & Best Practices
Author
Richard Mendes
June 24, 2026 • 10 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.


Popular Posts

Comments (0)

No comments yet. Be the first to comment!

Latest Articles