Skip to main content

Installation

Laravel Breeze API provides a minimal authentication implementation for Laravel applications that serve as the backend for Next.js frontends.

Requirements

  • PHP 8.2 or higher
  • Composer
  • MySQL, PostgreSQL, or SQLite database

Install Dependencies

composer install

Environment Configuration

Copy the example environment file and configure your application:
cp .env.example .env

Application Settings

.env
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

APP_LOCALE=en
APP_FALLBACK_LOCALE=en
APP_FAKER_LOCALE=en_US

BCRYPT_ROUNDS=12
LOG_CHANNEL=stack
LOG_LEVEL=debug

Database Configuration

Configure your database connection:
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=backend
DB_USERNAME=root
DB_PASSWORD=

Session Configuration

For API authentication with Sanctum, configure database sessions:
.env
SESSION_DRIVER=database
SESSION_LIFETIME=120
SESSION_ENCRYPT=false
SESSION_PATH=/
SESSION_DOMAIN=null

Mail Configuration

Configure mail settings for password reset and email verification:
.env
MAIL_MAILER=log
MAIL_HOST=127.0.0.1
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"

Cache and Queue

.env
CACHE_STORE=database
QUEUE_CONNECTION=database
BROADCAST_CONNECTION=log
FILESYSTEM_DISK=local

Laravel Sanctum Configuration

Sanctum provides the authentication layer for your API. Configure stateful domains for cookie-based authentication.

Stateful Domains

Edit config/sanctum.php to allow your frontend domain:
config/sanctum.php
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
    '%s%s%s',
    'localhost,localhost:3000,127.0.0.1,127.0.0.1:3000,127.0.0.1:8000,::1',
    Sanctum::currentApplicationUrlWithPort(),
    env('FRONTEND_URL') ? ','.parse_url(env('FRONTEND_URL'), PHP_URL_HOST) : ''
))),
Add to your .env:
.env
FRONTEND_URL=http://localhost:3000
SANCTUM_STATEFUL_DOMAINS=localhost:3000

Authentication Guards

config/sanctum.php
'guard' => ['web'],

Token Configuration

config/sanctum.php
'expiration' => null,
'token_prefix' => env('SANCTUM_TOKEN_PREFIX', ''),

Middleware Configuration

config/sanctum.php
'middleware' => [
    'authenticate_session' => Laravel\Sanctum\Http\Middleware\AuthenticateSession::class,
    'encrypt_cookies' => Illuminate\Cookie\Middleware\EncryptCookies::class,
    'validate_csrf_token' => Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class,
],

CORS Configuration

Configure Cross-Origin Resource Sharing to allow requests from your Next.js frontend.
config/cors.php
return [
    'paths' => ['*'],
    
    'allowed_methods' => ['*'],
    
    'allowed_origins' => [env('FRONTEND_URL', 'http://localhost:3000')],
    
    'allowed_origins_patterns' => [],
    
    'allowed_headers' => ['*'],
    
    'exposed_headers' => [],
    
    'max_age' => 0,
    
    'supports_credentials' => true,
];
The supports_credentials option must be true to allow cookie-based authentication.

Database Setup

Generate your application key and run migrations:
php artisan key:generate
php artisan migrate

Running the Application

Development Server

Start the Laravel development server:
php artisan serve
The API will be available at http://localhost:8000.

With Queue Worker

Run the application with queue processing:
composer run dev
This starts:
  • Laravel development server on port 8000
  • Queue worker for background jobs
  • Vite development server for asset compilation

Automated Setup

Use the automated setup script:
composer run setup
This script will:
  1. Install composer dependencies
  2. Copy .env.example to .env if it doesn’t exist
  3. Generate application key
  4. Run database migrations
  5. Install and build npm dependencies

Verification

Test that your API is running correctly:
curl http://localhost:8000/api/user
You should receive a 401 Unauthorized response, confirming the authentication middleware is working.

Next Steps

API Routes

Learn about available API endpoints

Controllers

Understand controller structure

Database

Explore migrations and models

Testing

Set up and run tests

Build docs developers (and LLMs) love