cachingcloudflare

How can I get the age of the cache using the API?


I can purge the cache via Cloudflare's API https://developers.cloudflare.com/api/operations/zone-purge

using this function:

https://gist.github.com/Greg-Boggs/73796406278cd67334db08dc052931dd

and I get a success message with a purge.

But is it possible - using the Cloudflare API or another method - to get the timestamp of the last time the cache was purged? Or return the age of the cache? Or somehow find the last time the cache was successfully purged?


Solution

  • I think the simplest method is to review Audit Logs, I tested it right now, and have an event "Oct 6, 2024 Purge everything". Audit logs can be retrieved via API, and you can specify necessary event, something like this:

    curl -X GET "https://api.cloudflare.com/client/v4/accounts/{account_id}/audit_logs?action.type=purge_everything" \
    -H "Authorization: Bearer {your_api_token}" \
    -H "Content-Type: application/json"
    

    Second option is to use Cloudflare Logs, but it is available only for Enterprise plan.

    Also may be useful to inspect headers of HTTP response, usually they contain CF-Cache-Status and Age. These headers can be used to find the cache age per resource.