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 cloudflareThis 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 you 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_aaaaaaaaexport CLOUDFLARE_DEFAULT_ACCOUNT_ID=aaaaaaaa_aaaaaaaaaaaa_aaaaaaaaComponents
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.
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, and Ai.
If you are using Cloudflare DNS with SST, use sst.cloudflare.dns with custom domains.
Examples
Check out the full examples: