The Pages API empowers you to build automations and integrate Pages with your development workflow. At a high level, the API endpoints let you manage deployments and builds and configure projects. Cloudflare supports Deploy Hooks for headless CMS deployments. Refer to the API documentation ↗ for a full breakdown of object types and endpoints.
You can go to Edit Cloudflare Workers template > Use template or go to Create Custom Token > Get started. If you create a custom token, you will need to make sure to add the Cloudflare Pages permission with Edit access.
Make requests
After creating your token, you can authenticate and make requests to the API using your API token in the request headers. For example, here is an API request to get all deployments in a project.
Try it with one of your projects by replacing {account_id}, {project_name}, and <API_TOKEN>. Refer to Find your account ID for more information.
Examples
The API is even more powerful when combined with Cloudflare Workers: the easiest way to deploy serverless functions on Cloudflare’s global network. The following section includes three code examples on how to use the Pages API. To build and deploy these samples, refer to the Get started guide.
Triggering a new build every hour
Suppose we have a CMS that pulls data from live sources to compile a static output. You can keep the static content as recent as possible by triggering new builds periodically using the API.
After you have deployed the JavaScript Worker, set a cron trigger in your Worker to run this script periodically. Refer to Cron Triggers for more details.
Deleting old deployments after a week
Cloudflare Pages hosts and serves all project deployments on preview links. Suppose you want to keep your project private and prevent access to your old deployments. You can use the API to delete deployments after a month, so that they are no longer public online. The latest deployment for a branch cannot be deleted.
After you have deployed the JavaScript Worker, you can set a cron trigger in your Worker to run this script periodically. Refer to the Cron Triggers guide for more details.
Sharing project information
Imagine you are working on a development team using Pages to build your websites. You would want an easy way to share deployment preview links and build status without having to share Cloudflare accounts. Using the API, you can easily share project information, including deployment status and preview links, and serve this content as HTML from a Cloudflare Worker.