To create API keys for a Laravel API that uses Jetstream with teams, you can use Laravel Sanctum. Here are the steps:
- Install Laravel Sanctum by running the following command in your terminal:
composer require laravel/sanctum
- Publish the Sanctum configuration file by running the following command:
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
- Run the Sanctum migrations by running the following command:
php artisan migrate
- In your
Usermodel, add theHasApiTokenstrait:
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
// ...
}
- In your
config/auth.phpfile, make sure that theapiguard is using thesanctumdriver:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'sanctum',
'provider' => 'users',
'hash' => false,
],
],
- Generate an API token for a user by running the following command:
php artisan sanctum:token
This will generate an API token for the default user. If you want to generate a token for a specific user, you can pass the user ID as an argument:
php artisan sanctum:token {user-id}
- Use the generated API token in your Postman requests by adding an
Authorizationheader with the valueBearer {api-token}.
That's it! You should now be able to make Postman calls to your Laravel API that uses Jetstream with teams.