Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

How is it possible to add additional informations in the Sanctum API token ?

Hello,

I need to add additional informations inside the Sanctum API token.

I have read the vendor code and it doesn't seem possible to add something else than a name, abilities and an expiration date.

I'd like to add an IP address inside this token.

$user->createToken();

Is it possible to do that and if yes, how ?

Thanks for your help.

V

0 likes
4 replies
vincent15000's avatar

@s4muel I've seen that, but I really don't have any idea how to do.

<?php

namespace App\Models\Sanctum;

use Laravel\Sanctum\PersonalAccessToken;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class CustomAccessToken extends PersonalAccessToken
{
		$fillable = [
				'ip_address',
		];
}

What do I have to write in this extended model ?

I have added the field in the personal_access_tokens table in the database.

But how to save the ip_address in the database ?

Is it so simple as $user->currentAccessToken->update(['ip_address' => $ip_address]); ?

1 like
s4muel's avatar
s4muel
Best Answer
Level 50

@vincent15000 do not forget to add other fillable fields from the original PersonalAccessToken class

use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken;

class PersonalAccessToken extends SanctumPersonalAccessToken
{
    protected $fillable = [
        'name',
        'token',
        'abilities',
        'expires_at',
        'ip',
    ];

    public function __construct(array $attributes = [])
    {
        $attributes['ip'] = request()->ip();

        parent::__construct($attributes);
    }
}

do not forget to add ip column to the personal_access_tokens table

and then it is added when created


$user = App\Models\User::first();

$token = $user->createToken('token');

$token->accessToken->ip; //127.0.0.1
1 like
LaryAI's avatar
Level 58

To add additional information to a Sanctum API token, such as an IP address, you can use the personal_access_tokens table to store custom data. While Sanctum does not natively support adding arbitrary data directly into the token itself, you can leverage the tokenable relationship to store and retrieve additional information.

Here's a step-by-step guide on how to achieve this:

  1. Create a Migration to Add Custom Fields: First, create a migration to add a custom field to the personal_access_tokens table. For example, you can add an ip_address column.

    php artisan make:migration add_ip_address_to_personal_access_tokens_table --table=personal_access_tokens
    

    Then, update the generated migration file:

    // database/migrations/xxxx_xx_xx_xxxxxx_add_ip_address_to_personal_access_tokens_table.php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class AddIpAddressToPersonalAccessTokensTable extends Migration
    {
        public function up()
        {
            Schema::table('personal_access_tokens', function (Blueprint $table) {
                $table->string('ip_address')->nullable();
            });
        }
    
        public function down()
        {
            Schema::table('personal_access_tokens', function (Blueprint $table) {
                $table->dropColumn('ip_address');
            });
        }
    }
    

    Run the migration:

    php artisan migrate
    
  2. Create the Token with Additional Information: When creating the token, you can now store the IP address in the personal_access_tokens table.

    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Auth;
    
    public function createTokenWithIp(Request $request)
    {
        $user = Auth::user();
        $token = $user->createToken('token-name');
    
        // Store the IP address
        $token->accessToken->forceFill([
            'ip_address' => $request->ip(),
        ])->save();
    
        return response()->json(['token' => $token->plainTextToken]);
    }
    
  3. Retrieve the Token with Additional Information: When you need to retrieve the token and its additional information, you can access the personal_access_tokens table.

    use Laravel\Sanctum\PersonalAccessToken;
    
    public function getTokenInfo($tokenId)
    {
        $token = PersonalAccessToken::find($tokenId);
    
        if ($token) {
            return response()->json([
                'token' => $token->token,
                'ip_address' => $token->ip_address,
                // Add other fields as needed
            ]);
        }
    
        return response()->json(['message' => 'Token not found'], 404);
    }
    

By following these steps, you can store and retrieve additional information, such as an IP address, associated with a Sanctum API token. This approach leverages the existing database structure and relationships provided by Sanctum.

Please or to participate in this conversation.