# How long does a user api key last. What is the validity period?

**URL:** <https://discourse.getcockpit.com/t/how-long-does-a-user-api-key-last-what-is-the-validity-period/1861>\
**Category:** Support\
**Created:** [March 10, 2021, 9:58pm UTC](https://discourse.getcockpit.com/t/how-long-does-a-user-api-key-last-what-is-the-validity-period/1861 "2021-03-10T21:58:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stoeptegel](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@Stoeptegel](https://discourse.getcockpit.com/u/Stoeptegel)\
**Post date:** [March 10, 2021, 9:58pm UTC](https://discourse.getcockpit.com/t/how-long-does-a-user-api-key-last-what-is-the-validity-period/1861/1 "2021-03-10T21:58:45Z")

</div>

I’m wondering if the user api key is regenerated. I don’t want to manually regenerate the key once in a while. It would be nice that it’s at least every login session.

I cannot find the answer anywhere.

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [March 23, 2021, 9:55am UTC](https://discourse.getcockpit.com/t/how-long-does-a-user-api-key-last-what-is-the-validity-period/1861/2 "2021-03-23T09:55:34Z")

</div>

The user api key doesn’t regenerate, but you can build your own logic and call it with a cronjob or apply it to an event. Something like this (not tested):

```php
$app->on('admin.init', function() {

    // regenerate user api key after each login
    $this->on('cockpit.authentication.success', function($user) {

        $user['api_key'] = 'new random string';

        $this->storage->save('cockpit/accounts', $user);

    });

});

```
