Skip to Content

CloudflareDeployer

The CloudflareDeployer class handles deployment of standalone Mastra applications to Cloudflare Workers. It manages configuration, deployment, and extends the base Deployer class with Cloudflare specific functionality.

Installation

npm install @mastra/deployer-cloudflare@latest

Usage example

src/mastra/index.ts
import { Mastra } from "@mastra/core/mastra"; import { CloudflareDeployer } from "@mastra/deployer-cloudflare"; export const mastra = new Mastra({ // ... deployer: new CloudflareDeployer({ projectName: "hello-mastra", scope: "", auth: { apiToken: process.env.CLOUDFLARE_API_TOKEN!, apiEmail: "name@email.com" } }) });

See the CloudflareDeployer API reference for all available configuration options.

Cloudflare API token

In your Cloudflare dashboard, navigate to Profile > API Tokens, then select Edit Cloudflare Workers and click Use template to create a token for the CloudflareDeployer.

To learn how to create a Cloudflare API token, see Create API token  in the Cloudflare docs.

Continuous integration

In the Cloudflare dashboard, navigate to Workers > Import a repository. After importing your Mastra project’s Git repository, create an application and configure the build settings with:

  • Build command: npm run build

  • Deploy command: cd .mastra/output && npx wrangler deploy

  • Build command: npm run build (optional)

Manual deployment

Manual deployments are also possible using the Cloudflare Wrangler CLI . With the Wrangler CLI installed run the following from your project root to deploy your application.

npm run build && wrangler deploy --config .mastra/output/wrangler.json

You can also run wrangler dev --config .mastra/output/wrangler.json from your project root to test your Mastra application locally.

Cloudflare secrets

To add secrets to your Cloudflare Workers application, use the Wrangler CLI. Secrets are environment variables that are encrypted and stored securely.

For example, to add an API key:

npx wrangler secret put OPENAI_API_KEY

When prompted, enter the secret value. The secret will be encrypted and stored securely in your Cloudflare account.

For more details on managing secrets, see Environment Variables and Secrets  in the Cloudflare documentation.

Build output

The build output for Mastra applications using the CloudflareDeployer includes all agents, tools, and workflows in your project, along with Mastra specific files required to run your application on Cloudflare.

      • index.mjs
      • wrangler.json
  • package.json

The CloudflareDeployer automatically generates a wrangler.json configuration file in .mastra/output with the following settings:

{ "name": "hello-mastra", "main": "./index.mjs", "compatibility_date": "2025-04-01", "compatibility_flags": ["nodejs_compat", "nodejs_compat_populate_process_env"], "observability": { "logs": { "enabled": true } }, "vars": { "OPENAI_API_KEY": "..", "CLOUDFLARE_API_TOKEN": "..." } }

Next steps