HS
Explore posts from serversCDCloudflare Developers
•Created by HS on 9/13/2024 in #workers-help
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.3 replies