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

thomasm789's avatar

Generate API key on registration

Hey all,

I'm having a painful time trying to generate an API key on registration.

I plan to only allow users to generate one key and this should be done automatically however I'm having a hard time achieving this.

Once this is done, before anyone asks. The key will always be shown on a new API Settings page.

Any help would be highly grateful :)

0 likes
10 replies
dawiyo's avatar

Assuming you have a column in the users table for api_token, you could do this in your User model. Might not be the best solution, but it works.

public static function boot() {
    parent::boot();

    static::created(function($user) {
        $user->api_token = str_random(50);
        $user->save();
    });
}
MalaniDerrick's avatar

public static function boot() { parent::boot();

static::creating(function($user) {
    $user->api_token = Uuid::generate();
});

}

thomasm789's avatar

Sorry, I didn't explain myself well. All I wish to do is at the point of registration already generate an api key that the users would normally do via the settings page.

Snapey's avatar

Thats what I thought you said, but all answers so far would create a key each time a user model object was created!

If using 5.3, in the Auth/RegisterController is this;

    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

You could change this like;

    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'token' => $this->createToken
        ]);
    }

and then add a createToken method in the same controller which creates and returns UUID or random token, whatever you need.

thomasm789's avatar

I want to use the existing API class and functions, and that's what I'm having trouble implementing. Its not meant to sit within the User table but within the API tokens table.

Snapey's avatar
Snapey
Best Answer
Level 122

So create a new entry in the API tokens table, but within the create function shown?

Once you have created a personal access client, you may issue tokens for a given user using the createToken method on the User model instance. The createToken method accepts the name of the token as its first argument and an optional array of scopes as its second argument:

    protected function create(array $data)
    {
        $user=User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

        $token = $user->createToken('Token Name')->accessToken;

        return $user;

    }
1 like
thomasm789's avatar

Thanks @Snapey your help is very much appreciated. Its very weird - it all seems logical that it should be working as expected.

However, the token just doesn't seem to generate.

Please or to participate in this conversation.