# Cloudflare

Cloudflare lets you deploy apps with Workers and connect services like D1, R2, and DNS. This guide covers how to set it up with SST.

---

## Install

Add the Cloudflare provider to your SST app. Learn more about providers.

```
sst add cloudflare
```

This adds the provider to your `sst.config.ts`.

```
{
  providers: {
    cloudflare: "5.37.1",
  },
}
```

If Cloudflare should store your app state, set `home` to "cloudflare". This is useful in setups where you plan to use Cloudflare as your main cloud provider.

---

## Credentials

You can create an account token in the Cloudflare dashboard under Manage Account > API Tokens.

Start with the **Edit Cloudflare Workers** template and add these permissions:

- _Account - D1 - Edit_
- _Zone - DNS - Edit_

Give the token access to the account your application will be deploying to. If you are using Cloudflare DNS with SST, include the zones that SST should update.

Set `CLOUDFLARE_DEFAULT_ACCOUNT_ID` to the Cloudflare account ID that SST should use. If you leave it unset, SST falls back to the first account that Cloudflare returns for that token.

Then set these variables in your shell, `.env`, or CI environment before you deploy:

```
export CLOUDFLARE_API_TOKEN=aaaaaaaa_aaaaaaaaaaaa_aaaaaaaa

export CLOUDFLARE_DEFAULT_ACCOUNT_ID=aaaaaaaa_aaaaaaaaaaaa_aaaaaaaa
```

---

### Deploying into Multiple Cloudflare Accounts

You can deploy Cloudflare components into multiple Cloudflare accounts by specifying the accountId argument and providing a different Cloudflare Provider.

```javascript
// define a provider with another api token for a different account
const provider = new cloudflare.Provider("AnotherProvider", { apiToken: "bbbbbbbb_bbbbbbbbbbbb_bbbbbbbb" });

// specify resource in another account
const sstWorker = new sst.cloudflare.Worker(
  "MyWorker",
  { handler: "index.ts", accountId: "bbbbbbbb-bbbbbbbbbbbb-bbbbbbbb" },
  { provider },
);
```

---

## Components

SST includes Cloudflare components for Workers, storage, queues, cron jobs, AI bindings, and more.

### Worker

Create a Cloudflare Worker and enable a URL so it can handle HTTP requests.

```
const worker = new sst.cloudflare.Worker("MyWorker", {
  handler: "index.ts",
  url: true,
});

return {
  url: worker.url,
};
```

Use the Worker component to build APIs and edge handlers on Cloudflare.

### Storage

Create Cloudflare storage resources and link them to your Worker. For example, here’s a D1 database.

```
const db = new sst.cloudflare.D1("MyDatabase");

new sst.cloudflare.Worker("MyWorker", {
  handler: "index.ts",
  link: [db],
  url: true,
});
```

Then access it in your handler through `Resource`.

```javascript
import { Resource } from "sst";

export default {
  async fetch() {
    const row = await Resource.MyDatabase.prepare(
      "SELECT id FROM todo ORDER BY id DESC LIMIT 1",
    ).first();
    return Response.json(row);
  },
};
```

The same pattern works with `Bucket` for R2 and `Kv` for KV namespaces.

### Queue

Use `Queue` for async work.

```
const queue = new sst.cloudflare.Queue("MyQueue");
queue.subscribe("consumer.ts");

const producer = new sst.cloudflare.Worker("Producer", {
  handler: "producer.ts",
  link: [queue],
  url: true,
});
```

For scheduled work, use `Cron` to run a worker on a cron expression.

### More components

Browse the component docs for `Worker`, `Astro`, `Bucket`, `D1`, `Kv`, `Queue`, `Cron`, `Ai`, `Workflow`, and `RateLimit`.

If you are using Cloudflare DNS with SST, use `sst.cloudflare.dns` with custom domains.

---

## Cloudflare Vite plugin

Cloudflare SSR components like `Astro` or `TanStack Start` need the Cloudflare Vite plugin to work correctly.

You need to configure the Vite plugin to use the SST-managed Wrangler config.

```javascript
import { defineConfig } from "vite";
import { cloudflare } from "@cloudflare/vite-plugin";

export default defineConfig({
  plugins: [
    cloudflare({
      configPath: process.env.SST_WRANGLER_CONFIG,
    }),
  ],
});
```

This environment variable is set by SST and ensures the plugin uses the generated Wrangler configuration.

---

## Examples

Check out the full examples:

- Cloudflare Workers with SST
- Hono on Cloudflare with SST
- tRPC on Cloudflare with SST
- Astro on Cloudflare
- Cloudflare D1
- Cloudflare KV
- Cloudflare Queue
- Cloudflare Cron
