Operations
| Operation | Description |
|---|---|
Cloudflare.UseCredentials | Uses the given API token (and account ID) for every Cloudflare step registered after this call, instead of $CLOUDFLARE_API_TOKEN and $CLOUDFLARE_ACCOUNT_ID from the environment. Not a step; call it at evaluation. Signature: UseCredentials(string apiToken, string? accountId = null). The values reach each command as per-command environment variables (by name, never on the command line) and are registered for redaction. EnsureAuthenticated neither prompts nor reads the environment for values given here. When accountId is null, $CLOUDFLARE_ACCOUNT_ID is used as before. Without this call nothing changes. Pair it with Secrets.EnvFile so the token is never exported to the whole build. |
Cloudflare.EnsureAuthenticated | Verify the Cloudflare API token with Cloudflare's token verification endpoint. Prompts interactively if environment variables are not set (not when Cloudflare.UseCredentials was called). See authentication for setup. |
Cloudflare.EnsurePagesProject | Verify that the current Cloudflare credentials can read a specific Pages project. It only checks; it never creates anything and fails if the project does not exist. PagesDeploy() registers this check automatically before deploying, so call it yourself only to fail early. To create a project use PagesCreateProject. |
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. Use it for first-time setup; PagesDeploy() does not create the project. To only verify that credentials can read an existing project, use EnsurePagesProject. |
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.UseCredentials
source
Uses the given API token (and account ID) for every Cloudflare step registered after this call, instead of $CLOUDFLARE_API_TOKEN and $CLOUDFLARE_ACCOUNT_ID from the environment. Not a step; call it at evaluation. Signature: UseCredentials(string apiToken, string? accountId = null). The values reach each command as per-command environment variables (by name, never on the command line) and are registered for redaction. EnsureAuthenticated neither prompts nor reads the environment for values given here. When accountId is null, $CLOUDFLARE_ACCOUNT_ID is used as before. Without this call nothing changes. Pair it with Secrets.EnvFile so the token is never exported to the whole build.
var cf = Secrets.EnvFile(".env.secrets", "CLOUDFLARE_API_TOKEN", "CLOUDFLARE_ACCOUNT_ID");
Cloudflare.UseCredentials(cf["CLOUDFLARE_API_TOKEN"], cf["CLOUDFLARE_ACCOUNT_ID"]);
Cloudflare.PagesDeploy(Directory("./dist"), "my-site");Cloudflare.EnsureAuthenticated
source
Verify the Cloudflare API token with Cloudflare's token verification endpoint. Prompts interactively if environment variables are not set (not when Cloudflare.UseCredentials was called). See authentication for setup.
Cloudflare.EnsureAuthenticated();Cloudflare.EnsurePagesProject
source
Verify that the current Cloudflare credentials can read a specific Pages project. It only checks; it never creates anything and fails if the project does not exist. PagesDeploy() registers this check automatically before deploying, so call it yourself only to fail early. To create a project use PagesCreateProject.
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. Use it for first-time setup; PagesDeploy() does not create the project. To only verify that credentials can read an existing project, use EnsurePagesProject.
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 varCloudflare.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 varAuthentication
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
| Variable | Description |
|---|---|
CLOUDFLARE_API_TOKEN | API token with Cloudflare Pages permissions |
CLOUDFLARE_ACCOUNT_ID | Your Cloudflare account ID (found in dashboard URL) |
CLOUDFLARE_PROJECT_NAME | Optional 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.
UseCredentials
Cloudflare.UseCredentials(apiToken, accountId = null) gives every Cloudflare step registered after it explicit credentials, so the token does not have to be exported to the whole build. Call it at evaluation, before the other Cloudflare operations. The values reach each command as per-command environment variables (by name, never on the command line) and are registered for redaction. EnsureAuthenticated() does not prompt for values given here. When accountId is null, CLOUDFLARE_ACCOUNT_ID is read as before. Without the call, nothing changes.
var cf = Secrets.EnvFile(".env.secrets", "CLOUDFLARE_API_TOKEN", "CLOUDFLARE_ACCOUNT_ID");
Cloudflare.UseCredentials(cf["CLOUDFLARE_API_TOKEN"], cf["CLOUDFLARE_ACCOUNT_ID"]);
Cloudflare.PagesDeploy(Directory("./dist"), "my-site");
Keep the credentials in a separate file such as .env.secrets (gitignored): .env.ando is still loaded and forwarded to every container command unless the run uses --no-env. See Secrets for Secrets.EnvFile and Secrets.UserSecrets.
EnsurePagesProject vs PagesCreateProject
| Operation | What it does | Use it when |
|---|---|---|
Cloudflare.EnsurePagesProject(name) | Only verifies that the current credentials can read an existing Pages project. It creates nothing and fails if the project does not exist. PagesDeploy() registers it automatically before deploying. | You want to fail early, before other steps, if the token cannot reach the project. You rarely call it yourself. |
Cloudflare.PagesCreateProject(name, branch?) | Creates a Pages project (production branch defaults to main). | First-time setup. PagesDeploy() does not create the project. |
Creating a Cloudflare API Token
- Go to Cloudflare Dashboard → Profile → API Tokens
- Click Create Token
- Click Create Custom Token
- Add the required permissions (see table below)
- Under Account Resources, select your account
- Under Zone Resources, select All zones or specific zones
- Click Continue to summary, then Create Token
- Copy the token and set it as
CLOUDFLARE_API_TOKEN
Required Token Permissions
| Permission | Required For |
|---|---|
Account → Cloudflare Pages → Read | Verifying Pages project access with EnsurePagesProject() |
Account → Cloudflare Pages → Edit | Deploying to Cloudflare Pages |
Zone → Cache Purge → Purge | Purging cache with PurgeCache() |
Zone → Zone → Read | Resolving 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
| Option | Description |
|---|---|
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. |