Laravel Sanctum API Tokens: Issue, Protect and Revoke
When a mobile app, a script or another service needs to call your Laravel API, a session cookie is the wrong tool. Laravel Sanctum gives you simple API tokens: the client sends a token in a header, and Laravel knows which user it belongs to. This guide sets it up and covers token abilities and revoking.
What this article is, and is not
This is a how-to for current Laravel versions using Sanctum's personal access tokens. It does not cover Sanctum's cookie-based login for single-page apps on the same domain, which works differently. The examples use a simple posts API, so adapt the names to your own project.
1. Install API support
php artisan install:api
On recent Laravel versions this installs Sanctum, creates routes/api.php and adds the migration for the personal_access_tokens table. Run php artisan migrate afterwards. Make sure your User model uses the HasApiTokens trait.
2. Issue a token
Route::post('/login', function (Request $request) {
$request->validate(['email' => 'required|email', 'password' => 'required']);
$user = User::where('email', $request->email)->first();
if (! $user || ! Hash::check($request->password, $user->password)) {
return response()->json(['message' => 'Invalid credentials.'], 422);
}
return ['token' => $user->createToken('mobile-app')->plainTextToken];
});
The plain-text token is shown only once. Laravel stores a hash of it, so you cannot read it back later. Tell your client to save it securely, and add rate limiting to the login route so it cannot be guessed at speed.
3. Protect routes
Route::middleware('auth:sanctum')->group(function () {
Route::get('/me', fn (Request $request) => $request->user());
Route::apiResource('posts', PostController::class);
});
The client sends the token as a bearer token:
curl https://example.com/api/me \
-H "Accept: application/json" \
-H "Authorization: Bearer YOUR_TOKEN"
Always send Accept: application/json. Without it, a failed login can redirect to a login page instead of returning a 401 response.
4. Limit what a token can do with abilities
$token = $user->createToken('report-bot', ['posts:read'])->plainTextToken;
Check the ability inside a controller:
abort_unless($request->user()->tokenCan('posts:read'), 403);
Or protect a route with Sanctum's ability middleware. On Laravel 11 and later you register its aliases in bootstrap/app.php:
use Laravel\Sanctum\Http\Middleware\CheckAbilities;
use Laravel\Sanctum\Http\Middleware\CheckForAnyAbility;
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
'abilities' => CheckAbilities::class,
'ability' => CheckForAnyAbility::class,
]);
})
Then use ->middleware(['auth:sanctum', 'abilities:posts:read']) (every listed ability required) or ability:... (any one is enough). Give each integration its own token with the smallest set of abilities it needs.
5. Revoke and expire tokens
// Log out: delete the token used for this request
$request->user()->currentAccessToken()->delete();
// Delete every token for a user, for example after a password change
$request->user()->tokens()->delete();
Tokens do not expire unless you set it. Set the expiration value (in minutes) in config/sanctum.php if you want them to.
Test it
use Laravel\Sanctum\Sanctum;
Sanctum::actingAs($user, ['posts:read']);
$this->getJson('/api/posts')->assertOk();
Common mistakes
- Sending requests without the
Accept: application/json header and getting redirects instead of 401 responses.
- Giving every token full access instead of using abilities.
- Logging or storing the plain-text token anywhere in your own code.
- Forgetting rate limiting on the login route.
Summary
Install API support, issue named tokens with specific abilities, protect routes with auth:sanctum, and revoke tokens when a user logs out or changes their password.