Create access token for Laravel API

This post is mostly a small reminder to myself, but I assume that this is helpful to others. The basic context is that, I want to create an access token for a given user in Laravel.

This how to assumes that Laravel Passport in installed and configured on you installation.

The process is pretty simple:

  • Fetch the user
  • Create the access token

I run the following commands in php artisan tinker.

$user = User::find(1); // 1 is the ID of the user
// Replace name with the name of the token.
$token = $user->createToken('name')->accessToken;