To build a REST API with token authentication in Laravel 13, install Sanctum with php artisan install:api , add the HasApiTokens trait to your User model, issue tokens with createToken() , and protect routes with the auth:sanctum middleware. Clients then send the token as a Bearer header. This guide builds a small login, profile and logout API and shows how to scope tokens with abilities.
What do you need before you start?
Laravel 13 (it requires PHP 8.3 or newer; PHP 8.3 to 8.5 are supported)
A database connection and a users table (a fresh Laravel app has both)
An HTTP client such as curl, Postman or the Laravel test client
How do you install Sanctum for API tokens?
Run one Artisan command. It publishes the API route file, creates the personal_access_tokens migration and prepares the app for token auth.
php artisan install:api php artisan migrate
Enter fullscreen mode
Exit fullscreen mode
Then add the trait to the User model so it can create and own tokens.
<?…