Find Cloudflare Account ID

You can retrieve your Account ID either through the Cloudflare dashboard or via the Wrangler CLI.

Option 1: Via the Dashboard

  1. Log in to the Cloudflare Dashboard.
  2. Navigate to Workers & Pages in the left sidebar.
  3. Look at the right sidebar under Account Details to find and copy your Account ID (a 32-character hexadecimal string).
  4. Alternatively, select any registered domain or zone; the Account ID appears on the right sidebar under the API section.

Option 2: Via the Wrangler CLI

If you have already authenticated Wrangler via your browser, run:

npx wrangler whoami
This command displays your logged-in account name, email, and associated Account ID in the terminal.

Create Scoped API Token

Creating a scoped API token follows the principle of least privilege.

Step-by-Step Instructions

  1. In the Cloudflare Dashboard, click your User Profile icon (top-right corner) and select My Profile.
  2. Select API Tokens from the left navigation menu, then click Create Token.
  3. Under Custom Token, click Get started.
  4. Configure the token details:
  5. Token name: e.g., Wrangler Pages Deployment.
  6. Permissions: Select Account → Cloudflare Pages → Edit.
  7. (Optional) If your project uses Pages Functions with Workers KV, D1, or R2, add Workers KV Storage → Edit or D1 → Edit.
  8. Account Resources: Choose Include → Specific account → select your account.
  9. Click Continue to summary, review the permissions, and click Create Token.
  10. Copy and store the generated token immediately, as Cloudflare will not display it again.

Using cloudflare wrangler to deploy a pages project

Prerequisites and Setup

Install Node.js (v18+) and Wrangler, the official Cloudflare Developer Platform CLI. You can install Wrangler globally or run it via npx:

# Global installation (optional)
npm install -g wrangler

# Verify installation
wrangler --version

Authenticate Wrangler with your Cloudflare account:

wrangler login
# or
wrangler login  --device
This opens a browser window prompting you to authorize Wrangler with your Cloudflare credentials.

Create the Pages Project

Create a named Pages project beforehand to define your production branch:

wrangler pages project create <PROJECT_NAME> --production-branch main

Deploy to Cloudflare Pages

Deploy your build directory using the wrangler pages deploy command:

# General syntax
wrangler pages deploy <OUTPUT_DIRECTORY> --project-name=<PROJECT_NAME>

# Example for a Vite/React build output to 'dist'
wrangler pages deploy dist --project-name=my-website

Common Deployment Flags

  • Target a specific branch (Preview vs Production):

    wrangler pages deploy dist --project-name=my-website --branch=main
    
    Deploying to your designated production branch (e.g., main) publishes to your primary .pages.dev URL, while other branches generate unique preview URLs.

  • Attach a commit message:

    wrangler pages deploy dist --project-name=my-website --commit-message="Release v1.0.0"
    

CI/CD and Headless Deployments

For automated pipelines (e.g., GitLab CI or GitHub Actions), bypass browser authentication by exporting environment variables with an API token created in your Cloudflare dashboard:

export CLOUDFLARE_API_TOKEN="your_api_token"
export CLOUDFLARE_ACCOUNT_ID="your_account_id"

npx wrangler pages deploy dist --project-name=my-website --branch=main