Guide for setting up a Contentful webhook to purge Cloudflare cache via payload customization.
Contentful provides a content infrastructure for digital teams to power content in websites, apps, and devices. Unlike a CMS, Contentful was built to integrate with the modern software stack. It offers a central hub for structured content, powerful management and delivery APIs, and a customizable web app that enable developers and content creators to ship digital products faster.
Contentful is registered trademark of Contentful GmbH.
Teemu Tammela
- [email protected]
- www.auralcandy.net
- github.com/teemutammela
- www.linkedin.com/in/teemutammela
- t.me/teemutammela
This guide comes with absolutely no warranty. The author assumes no responsibility of data loss or any other unintended side-effect. This guide is based on Cloudflare API V4 and as such is subject to change.
This guide assumes you have already created a Cloudflare account and configured at least one domain.
1) Login to your Cloudflare account and select the domain you wish to apply the cache purge.
2) Select the Overview page and find your Zone ID under the section API.
3) Click the link Get your API key under the section API on the Overview page.
4) Find Global API Key under the section API Keys and click the View button.
1) Install Contentful CLI.
2) Login to Contentful CLI and select the target space.
$ contentful login
$ contentful space use
3) Download webhook.json and import it to target space.
$ contentful space import --content-file webhook.json
4) Go to Space settings → Webhooks in your target space and confirm a webhook called Purge Cloudflare Cache has been installed.
5) Select View details → Webhook settings.
6) Look for the URL https://api.cloudflare.com/client/v4/zones/ZONE_ID/purge_cache
and replace the string ZONE_ID
with your Zone ID.
7) Look for the fields X-Auth-Email and X-Auth-Key under the section Headers and replace them with Cloudflare account e-mail address and API Key.
8) Click Save and test the webhook by publishing any entry. Verify that the webhook is working as intended under the section Activity log.
NOTE! By default the webhook is configured to engage when an entry is updated. If you wish to change that behavior, you may edit the settings under the section Triggers.
NOTE! By default the webhook is configured to apply to master environment. If you wish to change that behavior, you may edit the settings under the section Filters.
NOTE! If you are delivering content from Contentful to multiple Cloudflare-enabled domains, you need to install a separate webhook for each domain that have unique Zone IDs and API Keys. If you are installing multiple webhooks to the same space, it's highly recommend to rename them to e.g. Purge Cloudflare Cache (Site Name) after installation.