Laravel Sanctum is the framework's lightweight authentication package for APIs and single-page applications. For third-party or mobile clients it issues personal access tokens that the client sends with each request. This tutorial builds a small token-authenticated API from scratch.
1. Install API support
In a current Laravel application, one Artisan command installs Sanctum, publishes the API routes file and runs the needed migration:
php artisan install:apiThen add the HasApiTokens trait to your User model:
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
}2. Create a login endpoint that issues a token
In routes/api.php:
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Route;
use Illuminate\Validation\ValidationException;
Route::post('/login', function (Request $request) {
$credentials = $request->validate([
'email' => ['required', 'email'],
'password' => ['required'],
]);
$user = User::where('email', $credentials['email'])->first();
if (! $user || ! Hash::check($credentials['password'], $user->password)) {
throw ValidationException::withMessages([
'email' => ['The provided credentials are incorrect.'],
]);
}
return ['token' => $user->createToken('api')->plainTextToken];
})->middleware('throttle:5,1');The plain-text token is shown only once, at creation. Laravel stores a hash, so you cannot retrieve it later. The throttle:5,1 middleware limits login attempts to five per minute per client, which slows password guessing.
3. Protect routes
Use the auth:sanctum middleware:
Route::middleware('auth:sanctum')->group(function () {
Route::get('/user', fn (Request $request) => $request->user());
});4. Call the API with curl
# 1. Log in and note the token
curl -X POST https://example.test/api/login \
-H "Accept: application/json" \
-d "email=user@example.com" -d "password=secret"
# 2. Use the token
curl https://example.test/api/user \
-H "Accept: application/json" \
-H "Authorization: Bearer YOUR_TOKEN_HERE"Always send Accept: application/json so authentication failures return a 401 JSON response instead of a redirect.
5. Limit what a token can do with abilities
Give tokens only the permissions they need:
$token = $user->createToken('reports-client', ['reports:read'])->plainTextToken;Check abilities inside a route or controller:
if (! $request->user()->tokenCan('reports:read')) {
abort(403);
}Sanctum also provides middleware to require abilities at the route level; see its documentation for the current names.
6. Revoke tokens
// Log out: delete the token used for this request
$request->user()->currentAccessToken()->delete();
// Log out everywhere: delete all of the user's tokens
$request->user()->tokens()->delete();7. Set token expiration
By default tokens do not expire. Set an expiration, in minutes, in config/sanctum.php:
'expiration' => 60 * 24 * 7, // one weekPair this with a refresh or re-login flow in your client.
Security checklist
- Serve the API over HTTPS only. Bearer tokens are credentials.
- Give each client its own named token with the minimum abilities.
- Rate-limit authentication endpoints.
- Never log tokens.
- For first-party single-page apps on the same top-level domain, use Sanctum's cookie-based session authentication instead of tokens.
For the broader picture, read our OWASP Top 10:2025 guide, and check whether your application is ready for Laravel 13.