# Deploy Mastra to Cloudflare

Use `@mastra/deployer-cloudflare` to deploy your Mastra server to Cloudflare Workers. The deployer bundles your code and generates a `wrangler.jsonc` file conforming to Cloudflare's [wrangler configuration](https://developers.cloudflare.com/workers/wrangler/configuration/), ready to deploy with no additional configuration.

> **Info:** If you're using a [server adapter](https://mastra.ai/docs/server/server-adapters) or [web framework](https://mastra.ai/docs/deployment/web-framework), deploy the way you normally would for that framework.

## Before you begin

You'll need a [Mastra application](https://mastra.ai/guides/getting-started/quickstart) and a [Cloudflare](https://cloudflare.com/) account.

> **Warning:** Cloudflare Workers use an ephemeral filesystem, so any storage you configure (including observability storage) must be hosted externally. If you're using [LibSQLStore](https://mastra.ai/reference/storage/libsql) with a file URL, switch to a remotely hosted database.

## Installation

Add the `@mastra/deployer-cloudflare` package to your project:

**npm**:

```bash
npm install @mastra/deployer-cloudflare@latest
```

**pnpm**:

```bash
pnpm add @mastra/deployer-cloudflare@latest
```

**Yarn**:

```bash
yarn add @mastra/deployer-cloudflare@latest
```

**Bun**:

```bash
bun add @mastra/deployer-cloudflare@latest
```

Import [`CloudflareDeployer`](https://mastra.ai/reference/deployer/cloudflare) and set it as the deployer in your Mastra configuration:

```typescript
import { Mastra } from '@mastra/core'
import { CloudflareDeployer } from '@mastra/deployer-cloudflare'

export const mastra = new Mastra({
  deployer: new CloudflareDeployer({
    name: 'your-project-name',
    vars: {
      NODE_ENV: 'production',
    },
  }),
})
```

In order to test your Cloudflare Worker locally, also install the [`wrangler` CLI](https://developers.cloudflare.com/workers/wrangler/install-and-update/):

**npm**:

```bash
npm install -D wrangler
```

**pnpm**:

```bash
pnpm add -D wrangler
```

**Yarn**:

```bash
yarn add --dev wrangler
```

**Bun**:

```bash
bun add --dev wrangler
```

## Usage

After setting up your project, push it to your remote Git provider of choice (e.g. GitHub).

1. Connect your repository to Cloudflare. On the "Workers & Pages" dashboard, select **Create application** and choose your Git provider in the next step. Continue with the setup process and select the repository you want to deploy.

   > **Note:** Remember to set your environment variables needed to run your application (e.g. your [model provider](https://mastra.ai/models/providers) API key). You can upload secrets from your `.env` file using `npx wrangler secret bulk .env`. See [Secrets](https://mastra.ai/reference/deployer/cloudflare) for details.

2. Once you're ready, click the **Deploy** button and wait for the first deployment to complete.

3. Try out your newly deployed function by going to `https://<your-project-name>.<slug>.workers.dev/api/agents`. You should get a JSON response listing all available agents.

   Since the [Mastra server](https://mastra.ai/docs/server/mastra-server) prefixes every API endpoint with `/api`, you have to add it to your URLs when making requests.

4. You can now call your Mastra endpoints over HTTP.

   > **Note:** Set up [authentication](https://mastra.ai/docs/server/auth) before exposing your endpoints publicly.