What is Sanctum?
Laravel Sanctum is a lightweight authentication package for SPAs (single-page applications), mobile apps, and simple APIs. Without needing deep knowledge of OAuth, you can issue and manage multiple API tokens per user. Sanctum solves two authentication problems:Use SPA authentication when calling your API from your own SPA. Use API token authentication when mobile apps or third-party clients need to access your API. You can use either mode independently or both together.
Sanctum vs. Passport
Choose Passport only when you need to act as an OAuth2 provider for external services. For most applications, Sanctum is the right choice.
Installation and configuration
Install
Run theinstall:api Artisan command to set up Sanctum in one step:
- Installs the
laravel/sanctumpackage - Publishes the
personal_access_tokenstable migration - Runs the migration
Add the HasApiTokens trait
Add theHasApiTokens trait to your User model:
$user->createToken() and $user->tokens.
API token authentication
Token flow
Issue a token
Use thecreateToken() method to issue a token. Retrieve the plain-text token value from the plainTextToken property. The plain-text token is never stored in the database, so you must return it to the user immediately after creation.
Set scopes (abilities)
Assign abilities (scopes) to a token to restrict which operations it can perform:Check abilities with middleware
Register middleware aliases inbootstrap/app.php:
Token expiration
By default, Sanctum tokens do not expire. Set an expiration time (in minutes) via theexpiration option in config/sanctum.php:
Revoke tokens
SPA authentication
SPA authentication uses session cookies, so there is no need to issue or manage tokens. It is ideal when calling your API from a first-party frontend (Vue, React, Next.js, etc.).Enable the Sanctum middleware
Enable thestatefulApi() middleware in bootstrap/app.php:
Configure first-party domains
Set your SPA’s domain in thestateful option in config/sanctum.php:
Configure CORS
If the SPA is on a different subdomain, configure CORS:supports_credentials to true in config/cors.php:
SPA authentication flow
1
Retrieve the CSRF cookie
Before logging in, call the
/sanctum/csrf-cookie endpoint to initialize CSRF protection:2
Send the login request
POST your credentials to the
/login endpoint:3
Make authenticated requests
After login, subsequent requests are automatically authenticated via the session cookie:
Protecting authenticated routes
Apply theauth:sanctum middleware to your routes. Unauthenticated requests will receive a 401 Unauthorized response. This single middleware handles both API token authentication and SPA authentication.
Practical example: login API with token response
A complete API token authentication example for a mobile app:1
Create the login endpoint
2
Create authenticated routes
3
Send requests from the client
Testing
UseSanctum::actingAs() in your tests to authenticate a user and specify which abilities to grant.
- Pest
- PHPUnit
Summary
Installation steps
Installation steps
HasApiTokens trait to your User model:Common API reference
Common API reference
Choosing between API token auth and SPA auth
Choosing between API token auth and SPA auth
- API token authentication: Use when clients without a session — such as mobile apps, third-party services, or CLI tools — need to access your API.
- SPA authentication: Use when your first-party Vue, React, or Next.js frontend calls the API from the same domain (or subdomain). More secure and requires no token management.