Skip to content

What's a good way to cache an API token for reuse? #4156

Answered by styfle
danechitoaie asked this question in General
Discussion options

You must be logged in to vote

You could use a globally scoped variable in the API function like the following:

let token = ''

module.exports = async (req, res) => {
  if (!token) {
    token = await generateToken()
  }
  const result = await fetchData(token)
  res.send(result)
}

If you need to persist the token longer, then I would recommend Redis which can assign a TTL.

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@danechitoaie
Comment options

@roelvan
Comment options

@ghost
Comment options

Answer selected by styfle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants