Skip to content

Deploying to Cloudflare

The platform deploys as five Workers. Run deploys from a machine that is logged in to Cloudflare (wrangler login or CLOUDFLARE_API_TOKEN).

Terminal window
bun run deploy # preview: *.workers.dev, "-preview" data resources
bun run deploy:production # production: the cemco.app custom domains

Both build everything first (bun run build), then deploy in dependency order, applying D1 migrations before the Worker that owns the database:

  1. cemco-auth: migrations, deploy
  2. cemco-api: migrations, deploy (binds AUTH)
  3. cemco-agents (binds API, AUTH)
  4. cemco-mcp (binds API, AUTH)
  5. cemco-web (binds API, AUTH, AGENTS)

Add --dry-run to print the steps without running them.

Preview (default) Production (--env production)
Worker names cemco-web, cemco-api, … cemco-web-production, cemco-api-production, …
Hosts *.workers.dev (one host for all three sites, see preview routing) custom domains below
Data cemco-crm-preview, cemco-auth-preview, cemco-documents-preview, cemco-events-preview, cemco-agent-events-preview, workflow cemco-import-preview cemco-crm, cemco-auth, cemco-documents, cemco-events, cemco-agent-events, workflow cemco-import
ENVIRONMENT preview production

A preview deploy therefore cannot touch production data.

Resource Binding Owner Create
D1 cemco-crm DB api wrangler d1 create cemco-crm before the first deploy (migrations run first)
D1 cemco-auth DB auth wrangler d1 create cemco-auth before the first deploy
R2 cemco-documents DOCS api wrangler r2 bucket create cemco-documents
Queue cemco-events EVENTS (producer and consumer) api wrangler queues create cemco-events
Queue cemco-agent-events AGENT_EVENTS (api produces, agents consume) api, agents wrangler queues create cemco-agent-events
KV cemco-cache CACHE api auto-provisioned
Workers AI AI api, mcp, agents nothing to create
Worker Loader LOADER mcp, agents nothing to create
Durable Objects AssistantAgent, EmailAgent, AccountWatcherAgent agents created by the migration in wrangler.jsonc
Workflow cemco-import IMPORT_WORKFLOW agents created on deploy
Send email EMAIL api, auth, agents needs Email Routing on the domain (below)

Database and KV ids are left out of the configs. Wrangler can provision the KV namespace on deploy, but create both databases yourself (see the caution above) and add their database_id to the wrangler.jsonc of the owning Worker if your wrangler version does not find them by name. Create the queues and the R2 bucket (with -preview names for preview) before the first deploy as well.

Set each with wrangler secret put <NAME> inside the Worker’s folder (add --env production for production).

Worker Secret Required What
api DOCUMENT_SIGNING_SECRET yes Signs the 15-minute document links. A long random string
api ANTHROPIC_API_KEY no Anthropic models for nameplate reading and other AI features
auth BETTER_AUTH_SECRET yes Signs sessions and encrypts stored Google tokens. Without it sign-in reports not connected
auth GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET for Google sign-in OAuth client from Google Cloud; redirect https://auth.cemco.app/auth/callback/google. Enable the Gmail and Calendar APIs for email filing and calendar sync
auth BOOTSTRAP_TOKEN first run only Lets one person create the first admin login at /auth/bootstrap; stops working once a staff login exists. Delete it afterwards
agents ANTHROPIC_API_KEY no Anthropic models for the assistant and email agent (otherwise Workers AI)
agents AGENTS_WEBHOOK_SECRET no Shared secret for POST /agents/v1/watcher/events

Plain settings are in each wrangler.jsonc under vars (every one is listed with an explanation in the Worker’s .dev.vars.example): APP_URL, API_URL, AUTH_URL, MCP_URL, COOKIE_DOMAIN (.cemco.app in production), ADMIN_EMAILS, STAFF_EMAIL_DOMAIN, EMAIL_FROM, EMAIL_ENABLED, and optionally AI_GATEWAY_ID. On cemco-web, API_ORIGIN is the API’s public origin, which the app’s CSP allows for document previews. Connecting Claude over OAuth needs both AUTH_URL and MCP_URL on cemco-auth; until both are set, the OAuth endpoints answer not connected.

Generate a secret with openssl rand -base64 48.

The production configs declare custom domains, which Cloudflare creates (DNS and certificates) on deploy when the cemco.app zone is on the same account:

Host Worker
cemco.app (redirects to www), www.cemco.app, app.cemco.app, docs.cemco.app cemco-web-production
api.cemco.app cemco-api-production
auth.cemco.app cemco-auth-production
mcp.cemco.app cemco-mcp-production
agents.cemco.app cemco-agents-production
  1. Turn on Email Routing for cemco.app in the dashboard.
  2. Set up crm@cemco.app as a sender for the EMAIL bindings (customer status emails, invites, sign-in codes), then set EMAIL_ENABLED to "true" on cemco-api and cemco-auth. Until then sign-in emails report not connected and the API stores outgoing mail as skipped.
  3. Route crm@cemco.app to the cemco-agents-production Worker so mail that staff forward is filed. The Worker accepts mail only from active admins and editors whose message passes DMARC (INBOUND_AUTHSERV_ID names the authentication service whose results it trusts).

To add a second gate for staff, create a Zero Trust Access application for app.cemco.app and api.cemco.app that allows the Google Workspace domain, and bypass it for /portal, /auth/* and the public API routes customers use. No code change is needed.

  1. Sign in at https://app.cemco.app with Google using an address in ADMIN_EMAILS; it becomes admin. Without Google, use BOOTSTRAP_TOKEN at https://auth.cemco.app/auth/bootstrap once, then remove the secret.
  2. Load data with Import, starting with a dry run.
  3. Connect Claude to https://mcp.cemco.app/mcp (see Using the CRM inside Claude).