Skip to content

Builds API reference

This guide shows you how to use the Workers Builds REST API to connect a Worker to a Git repository, manage build settings and Previews, and monitor builds. The REST examples use curl. Some examples pipe output through jq ↗︎ to filter JSON responses. You can also update build settings with the Cloudflare CLI (cf).

Before you start

Create an API token

The Workers Builds API requires requests to act on behalf of a Cloudflare user. An account-owned API token does not represent a user, so the Builds API returns Invalid token even if the token has the required account permissions.

For the curl requests in this guide, create a user-owned API token from My Profile > API Tokens.

Under Permissions, select Account for each permission in the following table. Under Account Resources, include the account that owns your Worker.

Permission Access level Why you need it
Workers Builds Configuration Edit Manage build settings and Previews, and work with builds
Workers Scripts Read Needed to get your Worker tag and list Worker deployments

Get your Worker tag

The Builds API identifies Workers by their tag, an immutable ID assigned by Cloudflare, not by their name. In API requests and responses, this value appears as script_tag.

To find your Worker's tag, call the Workers Scripts API:

curl -s "https://api.cloudflare.com/client/v4/accounts/{account_id}/workers/scripts" \
  --header "Authorization: Bearer <API_TOKEN>" \
  | jq '.result[] | {name: .id, tag: .tag}'
{
	"name": "my-worker",
	"tag": "1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d"
}

Connect a Worker to a repository

One request connects your Worker to a repository and sets the build settings for your production branch and your Previews.

Prerequisites

  1. Install the Cloudflare GitHub App. In the Cloudflare dashboard ↗︎, go to Workers & Pages, select any Worker, and go to Settings > Builds > Connect. Select GitHub and authorize the app for your account or organization. You only need to do this once.

  2. Get your GitHub account and repository IDs:

    curl -s "https://api.github.com/users/<GITHUB_USERNAME>" | jq '.id'
    curl -s "https://api.github.com/repos/<GITHUB_USERNAME>/<REPO_NAME>" | jq '.id'
  3. Get a build token UUID. Select or create a build token in your Worker's Settings > Builds > API token, or list your build tokens with the GET /builds/tokens endpoint:

    curl -s "https://api.cloudflare.com/client/v4/accounts/{account_id}/builds/tokens" \
      --header "Authorization: Bearer <API_TOKEN>" \
      | jq '.result[] | {build_token_uuid, build_token_name}'

Create the build configuration

curl -s "https://api.cloudflare.com/client/v4/accounts/{account_id}/builds/workers" \
  --header "Authorization: Bearer <API_TOKEN>" \
  --header "Content-Type: application/json" \
  --request POST \
  --data '{
    "script_tag": "<WORKER_TAG>",
    "git_repository": {
      "provider_type": "github",
      "provider_account_id": "<GITHUB_USER_ID>",
      "provider_account_name": "<GITHUB_USERNAME>",
      "repo_id": "<GITHUB_REPO_ID>",
      "repo_name": "<REPO_NAME>",
      "branch": "main"
    },
    "production_settings": {
      "build_command": "npm run build",
      "deploy_command": "npx wrangler deploy",
      "build_token_uuid": "<BUILD_TOKEN_UUID>",
      "environment_variables": {
        "NODE_ENV": {"value": "production", "is_secret": false}
      }
    },
    "previews_enabled": true,
    "previews_base_config": {
      "build_command": "npm run build",
      "deploy_command": "npx wrangler preview",
      "build_token_uuid": "<BUILD_TOKEN_UUID>",
      "environment_variables": {
        "NODE_ENV": {"value": "development", "is_secret": false}
      }
    }
  }'

Pushes to main now deploy your Worker with wrangler deploy. Pushes to any other branch create or update a Preview with wrangler preview, without affecting your live deployment. To connect the Worker without Previews, set previews_enabled to false.

Field Description
git_repository The repository to connect. branch is your production branch.
production_settings Build settings for pushes to your production branch
previews_enabled Whether pushes to other branches create or update Previews
previews_base_config Build settings copied to each new Preview. deploy_command is your Preview command.

production_settings and previews_base_config accept the same fields:

Field Type Description
build_command string Command to build your project (for example, npm run build)
deploy_command string Command to deploy your Worker (for example, npx wrangler deploy or npx wrangler preview)
build_token_uuid string UUID of the build token used to deploy your Worker
root_directory string Path to your project, relative to the repository root. Defaults to /.
path_includes array File path patterns that start a build
path_excludes array File path patterns that do not start a build
build_caching_enabled boolean Whether builds reuse cached dependencies and build artifacts. Defaults to true.
environment_variables object Build-time variables. Set is_secret to true to mask a value in build logs.

View and update build settings

Get build settings

curl -s "https://api.cloudflare.com/client/v4/accounts/{account_id}/builds/workers/{worker_tag}" \
  --header "Authorization: Bearer <API_TOKEN>"

The response has the same git_repository, production_settings, previews_enabled, and previews_base_config fields you set when creating the build configuration.

The response includes plain build variable values but omits secret values. If you use these settings to connect another Worker, you need to supply the secret values separately and meet the connection prerequisites, including repository authorization and a valid build token UUID. To inspect settings from the Cloudflare CLI, refer to View build settings with cf.

Update build settings

Send only the fields you want to change. For example, to change the production build command and add a Preview build variable:

curl -s "https://api.cloudflare.com/client/v4/accounts/{account_id}/builds/workers/{worker_tag}" \
  --header "Authorization: Bearer <API_TOKEN>" \
  --header "Content-Type: application/json" \
  --request PATCH \
  --data '{
    "production_settings": {
      "build_command": "npm run build:prod"
    },
    "previews_base_config": {
      "environment_variables": {
        "API_URL": {"value": "https://staging.example.com", "is_secret": false},
        "OLD_VARIABLE": null
      }
    }
  }'
  • To delete an environment variable, set it to null.
  • To change your production branch, send {"git_repository": {"branch": "<BRANCH_NAME>"}}.
  • To turn Previews on or off, send {"previews_enabled": true} or {"previews_enabled": false}.
  • Changes to previews_base_config apply to Previews created afterward. To also update every existing Preview, add ?patch_existing_previews=true to the URL.

Update build settings with cf

Use cf to update an existing Builds configuration. First, look up your Worker tag and set worker_tag. For example, change the production build command and set a non-secret build variable:

cf builds workers edit "$worker_tag" \
  --production-settings-build-command 'npm run build' \
  --production-settings-environment-variables '{"BUILD_FLAG":{"value":"production","is_secret":false}}'

Manage Previews

When Previews are enabled, the first push to a branch that is not your production branch creates a Preview for that branch. Each Preview gets its own copy of previews_base_config.

List Previews

curl -s "https://api.cloudflare.com/client/v4/accounts/{account_id}/builds/workers/{worker_tag}/previews" \
  --header "Authorization: Bearer <API_TOKEN>" \
  | jq '.result[] | {preview_id, branch, auto_build, auto_delete}'
Field Description
preview_id Identifier of the Preview
branch Git branch the Preview tracks
auto_build Whether pushes to the branch build this Preview. Manual builds and Deploy Hooks run regardless.
auto_delete Whether deleting the Git branch also deletes this Preview

To get a single Preview and its build settings, send a GET request to /builds/workers/{worker_tag}/previews/{preview_id}.

Update a Preview

Send only the fields you want to change. For example, to stop pushes from building a Preview and change its build command:

curl -s "https://api.cloudflare.com/client/v4/accounts/{account_id}/builds/workers/{worker_tag}/previews/{preview_id}" \
  --header "Authorization: Bearer <API_TOKEN>" \
  --header "Content-Type: application/json" \
  --request PATCH \
  --data '{
    "auto_build": false,
    "settings": {
      "build_command": "npm run build:preview"
    }
  }'

Build a Preview

curl -s "https://api.cloudflare.com/client/v4/accounts/{account_id}/builds/workers/{worker_tag}/previews/{preview_id}/builds" \
  --header "Authorization: Bearer <API_TOKEN>" \
  --header "Content-Type: application/json" \
  --request POST \
  --data '{"branch": "feature-branch"}'

To list a Preview's builds, send a GET request to the same URL.

Work with builds

Start a production build

To start a production build without pushing a commit, create a Deploy Hook for your production branch and send a POST request to its URL.

List builds

Use the GET /builds/workers/{worker_tag}/builds endpoint:

curl -s "https://api.cloudflare.com/client/v4/accounts/{account_id}/builds/workers/{worker_tag}/builds" \
  --header "Authorization: Bearer <API_TOKEN>" \
  | jq '.result[] | {build_uuid, status, build_outcome, branch: .build_trigger_metadata.branch, created_on}'

Builds older than 90 days are not returned. Refer to Limits & pricing.

Find the commit for a deployed version

The latest successful build in List builds does not always match the version serving live traffic — for example, after a rollback or during a gradual deployment. To trace the live version or versions back to their source commits, read the active deployment first and then look up builds by version ID:

  1. Read the active deployment's version IDs and traffic percentages using List Worker Deployments (GET /accounts/{account_id}/workers/scripts/{script_name}/deployments):

    LIVE_VERSIONS=$(curl -s "https://api.cloudflare.com/client/v4/accounts/{account_id}/workers/scripts/{script_name}/deployments" \
      --header "Authorization: Bearer <API_TOKEN>" \
      | jq '.result.deployments[0].versions')
    
    echo "$LIVE_VERSIONS"

    Keep every version_id and its percentage from .result.deployments[0].versions[] rather than substituting the newest uploaded version.

  2. Look up builds by version ID with GET /accounts/{account_id}/builds/builds?version_ids={version_ids} (up to 20 comma-separated version UUIDs per request). The response keys .result.builds by version_id:

    VERSION_IDS=$(echo "$LIVE_VERSIONS" | jq -r '[.[].version_id] | join(",")')
    
    BUILDS=$(curl -s "https://api.cloudflare.com/client/v4/accounts/{account_id}/builds/builds?version_ids=$VERSION_IDS" \
      --header "Authorization: Bearer <API_TOKEN>" \
      | jq '.result.builds')
    
    jq -n --argjson versions "$LIVE_VERSIONS" --argjson builds "$BUILDS" '
      $versions[] | {
        version_id,
        percentage,
        build_uuid: ($builds[.version_id].build_uuid // null),
        commit_hash: ($builds[.version_id].build_trigger_metadata.commit_hash // "commit unknown")
      }'

With the Cloudflare CLI (cf), you do not need to construct request URLs or headers manually. cf unwraps the API result envelope on stdout.

  1. Read the active deployment's version IDs and traffic percentages with cf workers deployments list:

    LIVE_VERSIONS=$(cf workers deployments list --worker <SCRIPT_NAME> \
      | jq '.deployments[0].versions')
    
    echo "$LIVE_VERSIONS"

    Keep every version_id and its percentage from .deployments[0].versions[] rather than substituting the newest uploaded version.

  2. Look up builds by version ID with cf builds versions get (up to 20 comma-separated version UUIDs per request). The response keys .builds by version_id:

    VERSION_IDS=$(echo "$LIVE_VERSIONS" | jq -r '[.[].version_id] | join(",")')
    
    BUILDS=$(cf builds versions get --version-ids "$VERSION_IDS" \
      | jq '.builds')
    
    jq -n --argjson versions "$LIVE_VERSIONS" --argjson builds "$BUILDS" '
      $versions[] | {
        version_id,
        percentage,
        build_uuid: ($builds[.version_id].build_uuid // null),
        commit_hash: ($builds[.version_id].build_trigger_metadata.commit_hash // "commit unknown")
      }'

Example: Rollback where the latest build is not live

If a newer build succeeded (33333333-...) but traffic was rolled back 100% to an earlier version (11111111-...), .result.deployments[0].versions[] lists only 11111111-..., and querying ?version_ids=11111111-1111-4111-8111-111111111111 returns that live version's build_uuid and commit_hash:

{
	"version_id": "11111111-1111-4111-8111-111111111111",
	"percentage": 100,
	"build_uuid": "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa",
	"commit_hash": "9f8e7d6c5b4a3210fedcba9876543210abcdef12"
}

Example: Split deployment with an unmatched version

During a 90/10 split deployment where the 10% version has no matching build record (for example, it was uploaded directly with Wrangler or built past the 90-day Builds retention limit), .result.builds omits that version ID. Keep both live versions with their traffic percentages and record the unmatched version as commit unknown:

{
	"version_id": "11111111-1111-4111-8111-111111111111",
	"percentage": 90,
	"build_uuid": "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa",
	"commit_hash": "9f8e7d6c5b4a3210fedcba9876543210abcdef12"
}
{
	"version_id": "22222222-2222-4222-8222-222222222222",
	"percentage": 10,
	"build_uuid": null,
	"commit_hash": "commit unknown"
}

A missing build record does not mean a different commit is serving traffic. Use Get build logs to inspect a known build_uuid, not to guess which build matches a live version by timestamp.

Get build logs

Use the GET /builds/builds/{build_uuid}/logs endpoint with a build_uuid from List builds:

curl -s "https://api.cloudflare.com/client/v4/accounts/{account_id}/builds/builds/{build_uuid}/logs" \
  --header "Authorization: Bearer <API_TOKEN>"

Build logs are retained for 90 days after the build finishes. To keep them longer, save the response.

Cancel a build

Use the PUT /builds/builds/{build_uuid}/cancel endpoint:

curl -s "https://api.cloudflare.com/client/v4/accounts/{account_id}/builds/builds/{build_uuid}/cancel" \
  --header "Authorization: Bearer <API_TOKEN>" \
  --request PUT

Was this helpful?