---
title: "Self-Hosting Shaf on Cloudflare Workers"
description: "Deploy a sovereign production instance of Shaf on Cloudflare Workers, D1 distributed SQLite, and KV cache."
---

> Documentation Index
> Fetch the complete documentation index at: https://shaf.app/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Self-Hosting Shaf on Cloudflare Workers

# Self-Hosting Shaf on Cloudflare Workers

Organizations requiring 100% data sovereignty, custom compliance controls, or private network deployments can run Shaf on their own Cloudflare infrastructure. This guide provisions Cloudflare D1 and KV resources, runs database migrations, and deploys the Nitro edge Worker.

---

## Prerequisites

- Node.js 22.x and pnpm 11.x installed on your deployment workstation.
- A Cloudflare account with the [Wrangler CLI](https://developers.cloudflare.com/workers/wrangler/) authenticated (`wrangler login`).
- A custom domain or free `workers.dev` subdomain attached to your Cloudflare account.

---

## Implementation Recipe

1. **Clone the Repository and Install Dependencies**

   Clone the codebase and run the bootstrap sequence:

   ```bash
   git clone https://github.com/shaf-is/shaf.git
   cd shaf
   pnpm install
   ```
2. **Provision Cloudflare D1 and KV Storage**

   Create your distributed SQLite database and Anycast KV cache using Wrangler:

   ```bash
   # 1. Create D1 database
   wrangler d1 create shaf-db

   # 2. Create KV read-cache namespace
   wrangler kv namespace create shaf-kv
   ```

   Note the generated `database_id` and KV `id` in the CLI output.
3. **Update Wrangler Configuration**

   In `wrangler.jsonc`, insert the provisioned IDs into your storage bindings:

   ```jsonc
   {
     "name": "shaf-core",
     "main": "./.output/server/index.mjs",
     "compatibility_date": "2025-02-14",
     "compatibility_flags": ["nodejs_compat"],
     "d1_databases": [
       {
         "binding": "DB",
         "database_name": "shaf-db",
         "database_id": "YOUR_D1_DATABASE_ID"
       }
     ],
     "kv_namespaces": [
       {
         "binding": "KV",
         "id": "YOUR_KV_NAMESPACE_ID"
       }
     ]
   }
   ```
4. **Apply D1 Database Migrations**

   Execute Drizzle ORM schema migrations against your remote D1 instance:

   ```bash
   pnpm db:migrate:remote
   ```

   **Expected output:**

   ```text
   Applying migration 0001_initial_schema.sql...
   Applying migration 0002_add_analytics_indices.sql...
   100% migrated (all migrations applied successfully).
   ```
5. **Set Production Secrets**

   Configure mandatory security secrets via Wrangler:

   ```bash
   # Generate cryptographic site secret
   wrangler secret put NUXT_SITE_TOKEN
   wrangler secret put SESSION_SECRET
   ```
6. **Build and Deploy to Cloudflare Workers**

   Compile the client UI and Nitro server bundle, then deploy to Cloudflare:

   ```bash
   pnpm build
   wrangler deploy
   ```

   **Expected output:**

   ```text
   Uploaded shaf-core (2.45 MiB)
   Deployed to https://shaf-core.your-subdomain.workers.dev
   ```

---

## Verification

Probe your self-hosted deployment to confirm Worker execution and D1 database connectivity:

1. Test the health status endpoint:
   ```bash
   curl -s "https://shaf-core.your-subdomain.workers.dev/api/admin/system/health"
   ```
   **Expected response (`200 OK`):**
   ```json
   {
     "status": "healthy",
     "database": "connected",
     "kv": "connected",
     "version": "1.0.0"
   }
   ```

2. Inspect edge redirect execution:
   ```bash
   curl -I "https://shaf-core.your-subdomain.workers.dev/docs"
   ```
   **Expected header:**
   ```http
   HTTP/2 301 Moved Permanently
   location: /docs/
   server: cloudflare
   ```

---

## Common Gotchas

| Issue / Error | Root Cause | Solution |
| :--- | :--- | :--- |
| `D1_ERROR: no such table: links` | Migrations were run locally but not deployed to the remote database. | Run `pnpm db:migrate:remote` to apply tables to the production D1 instance. |
| `KV_ERROR: namespace not found` | The `id` in `wrangler.jsonc` does not match the output of `wrangler kv namespace create`. | Verify your KV IDs by running `wrangler kv namespace list`. |
| Out of Memory during `pnpm build` | The 8GB Node heap constraint was exceeded during full prerendering. | Increase Node memory limit: `export NODE_OPTIONS="--max-old-space-size=8192"`. |
| Missing `NUXT_SITE_TOKEN` error | Mandatory authentication token secret was not set. | Run `wrangler secret put NUXT_SITE_TOKEN` and provide a random 32-character string. |

Source: https://shaf.app/docs/how-to/integrations-and-developer/self-hosting-cloudflare/index.mdx
