# Refresh token in background

**URL:** <https://discourse.julialang.org/t/refresh-token-in-background/74830>\
**Category:** Web Stack\
**Created:** [January 18, 2022, 8:40pm UTC](https://discourse.julialang.org/t/refresh-token-in-background/74830 "2022-01-18T20:40:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertdj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertdj/32/103_2.png) [@robertdj](https://discourse.julialang.org/u/robertdj)\
**Post date:** [January 18, 2022, 8:40pm UTC](https://discourse.julialang.org/t/refresh-token-in-background/74830/1 "2022-01-18T20:40:27Z")

</div>

I access a REST API that rely on a temporary token. The token is short lived, but does come with a refresh token. I store it in a struct like

```julia
mutable struct MyToken
    AccessToken::String
    RefreshToken::String
    ExpiresIn::UInt64
end

```

I have a method `refresh!(token)` that updates `AccessToken` using `RefreshToken`.

I would like to keep the token valid throughout the lifetime of my Julia session. Something like this works

```julia
function auto_refresh!(token)
    @async begin
        sleep(token.ExpiresIn)
        @info "Updating token"
        refresh!(token)
        auto_refresh!(token)
    end
end

```

But I wonder if this is a good approach?
