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

DDSameera's avatar

How to Encrypt Sanctum REST API token ?

How to Encrypt Sanctum REST API token and Decrypt that token using Private Key ?

I tried , this way. but it doesn't work

Purpose : I want to protect bearer token , so i need to encrypt

$token = Crypt::encrypt($user->createToken('rmisbackend')->plainTextToken);
0 likes
5 replies
Shaden's avatar
Shaden
Best Answer
Level 4

Try

$token = Crypt::encrypt(base64_encode($user->createToken('rmisbackend')->plainTextToken)));

hope this works

Shaden's avatar

Crypt uses the APP_KEY in env file,

you can override Crypt Class with your alternative key. or what I do is have the same APP_KEY o both ends, sender and consumer

Shaden's avatar

Hi did you manage to get it to work?

you can change the Key in the config file

   'key' => env('APP_KEY'),

to what ever you want.. but not sure about other things using the same key.

Shaden's avatar

can you try

$token =   Crypt::encryptString($user->createToken('rmisbackend')->plainTextToken);

I think it does the same as base_64 but easier to encrypt and decrypt.

Please or to participate in this conversation.