Cloudflare

Deploy static sites to Cloudflare Pages using the wrangler CLI.

Operations

Operation Description
Cloudflare.EnsureAuthenticated
Verify the Cloudflare API token with Cloudflare's token verification endpoint. Prompts interactively if environment variables are not set. See authentication for setup.
Cloudflare.EnsurePagesProject
Verify that the current Cloudflare credentials can read a specific Pages project. PagesDeploy() registers this check automatically before deploying.
Cloudflare.PagesDeploy
Deploy a directory to Cloudflare Pages. The directory should be the build output folder. Automatically verifies Pages project access before running wrangler deploy.
Cloudflare.PagesListProjects
List all Cloudflare Pages projects.
Cloudflare.PagesCreateProject
Create a new Cloudflare Pages project.
Cloudflare.PagesListDeployments
List deployments for a Cloudflare Pages project.
Cloudflare.PurgeCache
Purge the entire Cloudflare cache for a zone. Accepts either a Zone ID or domain name (domain is resolved automatically). Useful after deployments to ensure visitors see the latest content.

Operation Details

Cloudflare.EnsureAuthenticated source

Verify the Cloudflare API token with Cloudflare's token verification endpoint. Prompts interactively if environment variables are not set. See authentication for setup.

Cloudflare.EnsureAuthenticated();

Cloudflare.EnsurePagesProject source

Verify that the current Cloudflare credentials can read a specific Pages project. PagesDeploy() registers this check automatically before deploying.

Cloudflare.EnsurePagesProject("my-site");

Cloudflare.PagesDeploy source

Deploy a directory to Cloudflare Pages. The directory should be the build output folder. Automatically verifies Pages project access before running wrangler deploy.

var frontend = Directory("./frontend");
Cloudflare.PagesDeploy(frontend / "dist", "my-site");
Cloudflare.PagesDeploy(frontend / "build", o => o
  .WithProjectName("my-site")
  .WithBranch("main"));

Cloudflare.PagesListProjects source

List all Cloudflare Pages projects.

Cloudflare.PagesListProjects();

Cloudflare.PagesCreateProject source

Create a new Cloudflare Pages project.

Cloudflare.PagesCreateProject("my-site");
Cloudflare.PagesCreateProject("my-site", "develop");

Cloudflare.PagesListDeployments source

List deployments for a Cloudflare Pages project.

Cloudflare.PagesListDeployments("my-site");
Cloudflare.PagesListDeployments(); // Uses CLOUDFLARE_PROJECT_NAME env var

Cloudflare.PurgeCache source

Purge the entire Cloudflare cache for a zone. Accepts either a Zone ID or domain name (domain is resolved automatically). Useful after deployments to ensure visitors see the latest content.

Cloudflare.PurgeCache("example.com"); // Resolves domain to Zone ID
Cloudflare.PurgeCache("zone-id-123"); // Direct Zone ID
Cloudflare.PurgeCache(); // Uses CLOUDFLARE_ZONE_ID env var

Authentication

Cloudflare operations require an API token and account ID. Set these as environment variables or enter them when prompted. Authentication, Pages project checks, and cache purges use the Cloudflare API directly. Pages deployments use Wrangler.

Environment Variables

VariableDescription
CLOUDFLARE_API_TOKENAPI token with Cloudflare Pages permissions
CLOUDFLARE_ACCOUNT_IDYour Cloudflare account ID (found in dashboard URL)
CLOUDFLARE_PROJECT_NAMEOptional default project name for Pages operations

Interactive Prompting

When running locally without environment variables set, Cloudflare.EnsureAuthenticated() will prompt for credentials interactively. The API token input is hidden for security. Credentials are only stored for the current build session and are cleared when the process exits.

Cloudflare.EnsureAuthenticated() verifies the API token with Cloudflare’s token verification endpoint. Cloudflare.PagesDeploy() also registers Cloudflare.EnsurePagesProject(projectName) automatically, which checks that the same token can read the target Pages project before Wrangler starts the deployment.

Creating a Cloudflare API Token

  1. Go to Cloudflare Dashboard → Profile → API Tokens
  2. Click Create Token
  3. Click Create Custom Token
  4. Add the required permissions (see table below)
  5. Under Account Resources, select your account
  6. Under Zone Resources, select All zones or specific zones
  7. Click Continue to summary, then Create Token
  8. Copy the token and set it as CLOUDFLARE_API_TOKEN

Required Token Permissions

PermissionRequired For
Account → Cloudflare Pages → ReadVerifying Pages project access with EnsurePagesProject()
Account → Cloudflare Pages → EditDeploying to Cloudflare Pages
Zone → Cache Purge → PurgePurging cache with PurgeCache()
Zone → Zone → ReadResolving domain names to Zone IDs (for PurgeCache("example.com"))

Finding Your Account ID

Your account ID is in the Cloudflare dashboard URL: https://dash.cloudflare.com/ACCOUNT_ID/...

Or find it on any zone’s overview page in the right sidebar under API → Account ID.

Example

Deploy a static site to Cloudflare Pages and purge the cache.

// Create a directory reference
var website = Directory("./website");

// Ensure we're authenticated with Cloudflare
Cloudflare.EnsureAuthenticated();

// Build the site
Npm.Ci(website);
Npm.Run(website, "build");

// Deploy to Cloudflare Pages (deploy the dist folder)
Cloudflare.PagesDeploy(website / "dist", "my-website");

// Purge the cache (accepts domain name or Zone ID)
Cloudflare.PurgeCache("my-website.com");

Options Reference

Cloudflare.PagesDeploy Options

OptionDescription
WithProjectName(string)Cloudflare Pages project name. Must match an existing project in your Cloudflare account. Can also be set via CLOUDFLARE_PROJECT_NAME environment variable.
WithBranch(string)Git branch name for the deployment. Used by Cloudflare to determine if this is a production or preview deployment. Production branch deploys to the main URL.
WithCommitHash(string)Git commit hash to associate with the deployment. Displayed in Cloudflare dashboard for tracking which code version is deployed.
WithCommitMessage(string)Git commit message for the deployment. Displayed in Cloudflare dashboard alongside the commit hash.