Cache access token requested from an API and use it in the subsequent requests
Hi,
I'm looking for a way to cache an access token which is returned from an external API by making a
POST
request (by passing the username and password in the request body). This access token is valid for 1 week (604800 seconds). Is there a way to cache this token that is being returned as part of the response body?
There are two additional API calls (1 POST
+ 1 GET
) within the same worker that uses this access token to get additional data from the API server (that is never cached). At this moment, every time I call worker endpoint, it generates a new token during the 1st call. I'm looking to avoid that and reuse the same token for those additional 2 calls.
I tried setting cacheTtl
in cf
object in the fetch request, but it doesn't seem to do anything, which I assume is due to it being a POST
request (CF-Cache-Status
always returns DYNAMIC
for this response).
Should I try to leverage a KV
store instead or is there a more simpler or better option for caching this access token?
Thanks.2 Replies
Hi, just happened to see this while browsing. I think KV is a good bet if the requestor is uniquely identifiable. Will it always be you making the requests or is this a multi-tenant app situation?
Hi @steyblind, Yes, it will always be just my application making the request, no multi-tenant scenario in our case.