A starter Cloudflare Worker using Chanfana (OpenAPI), Hono, D1, and Zod v4.
Scaffold a new project with the create-cloudflare CLI:
npm create cloudflare@latest -- --template https://github.com/cloudflare/chanfana/tree/main/template# Install dependencies
npm install
# Create the D1 database
npx wrangler d1 create tasks-db
# Copy the returned database_id into wrangler.jsonc
# Run the migration locally
npm run db:migrate:localnpm run dev # Start local dev server (http://localhost:8787)Once running, open:
- API docs — http://localhost:8787/api/docs
- OpenAPI spec — http://localhost:8787/api/openapi.json
Tests use @cloudflare/vitest-pool-workers with a local D1 binding (no real database needed):
npm test# Apply migration to remote D1
npm run db:migrate:remote
# Deploy to Cloudflare
npm run deploysrc/
index.ts # Hono app + route registration
models/task.ts # Zod schema + shared meta
endpoints/
taskCreate.ts # POST /api/tasks
taskList.ts # GET /api/tasks
taskRead.ts # GET /api/tasks/:id
taskUpdate.ts # PUT /api/tasks/:id
taskDelete.ts # DELETE /api/tasks/:id
hello.ts # GET /api/hello (custom non-D1 example)
test/
tasks.test.ts # Integration tests
migrations/
0001_create_tasks/up.sql # D1 schema
- Create a Zod schema and meta object in
src/models/ - Create endpoint classes in
src/endpoints/extendingD1CreateEndpoint,D1ReadEndpoint, etc. - Register routes in
src/index.ts - Add a D1 migration in
migrations/
See the Chanfana docs for the full API reference.