What the agent does
The agent reads the Worker and its bindings before adding routes. In a project shaped like the d1-template of cloudflare/templates, wrangler.json sets compatibility_date 2025-10-08, main as src/index.ts, one d1_databases binding named DB, and observability on. package.json has wrangler 4.136.1 and TypeScript 5.9.3 as its only dev dependencies, a check script of tsc && wrangler deploy --dry-run, seedLocalD1 running wrangler d1 migrations apply DB --local, and no test script. migrations/0001_create_comments_table.sql creates comments(id, author, content) and inserts three rows. src/index.ts is a single fetch that runs SELECT * FROM comments LIMIT 3 and returns an HTML page from src/renderHtml.ts. tsconfig.json types the Env from worker-configuration.d.ts, produced by wrangler types.
The agent keeps the HTML page at / and adds a small router in src/index.ts on new URL(request.url).pathname and request.method. GET /api/comments returns { results } from env.DB.prepare("SELECT id, author, content FROM comments ORDER BY id DESC LIMIT ?").bind(limit).all(), with limit parsed from the query string and clamped to 100. POST /api/comments parses JSON, requires non-empty author and content strings under a length cap, answers 400 with a { error } body otherwise, inserts with a bound statement and RETURNING id, author, content, and answers 201. Anything else under /api/ is 404 and a non-matching method is 405 with an Allow header. A cors helper sets Access-Control-Allow-Origin from a CORS_ORIGIN var in wrangler.json, defaulting to *, and answers OPTIONS preflights with 204.
Tests follow the sibling d1-starter-sessions-api-template: vitest, @cloudflare/vitest-pool-workers with cloudflareTest({ wrangler: { configPath: "./wrangler.json" } }) in vitest.config.mts, and test/index.spec.ts importing env, SELF and createExecutionContext from cloudflare:test. The migrations are applied to the isolated test D1 in a beforeAll by reading migrations/*.sql and calling env.DB.exec. The repository's AGENTS.md asks for at least five tests, so the file covers the seeded list, a successful create and read-back, a 400 on a missing field, a 404 on an unknown path, a 405 on DELETE, and the preflight headers.
The agent adds test to package.json and the three dev dependencies at the versions the sibling template pins, runs npm install, npm run cf-typegen, npm run check and npm test, and from the repository root pnpm run check:templates and pnpm run check:prettier, which pnpm run check runs over every template. It pastes the Vitest summary and the dry-run deploy output in the pull request.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "claude",
"input": "Add a JSON REST API over D1 to the d1-template Worker in this repository and open a pull request. Read d1-template/wrangler.json, d1-template/package.json, d1-template/src/index.ts, d1-template/src/renderHtml.ts, d1-template/migrations/0001_create_comments_table.sql, d1-template/tsconfig.json, d1-starter-sessions-api-template/vitest.config.mts, d1-starter-sessions-api-template/test/index.spec.ts and AGENTS.md first. Keep the HTML page at /. Add GET /api/comments with a clamped limit query and POST /api/comments that validates author and content, inserts with a bound statement using RETURNING, and answers 201; answer 400 with an error body on bad input, 404 under /api for unknown paths and 405 with Allow for other methods. Add a CORS helper driven by a CORS_ORIGIN var in wrangler.json with an OPTIONS handler. Add vitest with @cloudflare/vitest-pool-workers at the versions the sessions template pins, a vitest.config.mts, and test/index.spec.ts with at least five tests that apply the migrations to the test database in beforeAll. Add a test script. Run npm install, npm run cf-typegen, npm run check, npm test and, from the root, pnpm run check:templates and pnpm run check:prettier, and paste the output in the pull request. Change no other template.",
"resources": [
{
"url": "https://github.com/your-org/your-app"
}
]
}- Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
- Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
- Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.
What comes back
What comes back is a pull request confined to d1-template/: a reworked src/index.ts with the router and handlers, a src/cors.ts, a vitest.config.mts, a test/index.spec.ts with six cases, a CORS_ORIGIN var in wrangler.json, and package.json gaining a test script and three dev dependencies with a regenerated package-lock.json. The description shows the Vitest summary, the wrangler deploy --dry-run output with the upload size, and a curl of POST /api/comments with its 201 body.
On a repository with the templates' workflow, the root pnpm run check runs syncpack lint, the template linter, the lockfile check, the turbo run check cf-typegen pass and prettier over every template, so a version that disagrees with the other templates or a stray formatting change fails there before a person reads the diff.
A reviewer checks that the LIMIT and the insert both use bound parameters, that CORS_ORIGIN is set to their site rather than * before deploy, and that the content length cap matches what their page will accept. Then they run npm run deploy, which applies the migrations remotely through predeploy first.
Run it on every new Worker project
A team that starts Workers from the same template sends one POST /o/{org}/sessions per new repository with this prompt and that repository in resources. Each run reads its own wrangler.json and migrations, so a project whose table has different columns gets handlers and validation for those columns, and each run opens its own pull request.
Give the API key a webhook_url and Rush POSTs run.completed when each turn finishes, with the session_url to read the pull request and the test output from.
The use case this job follows, with its automation and trigger: Hand off a long task.
Why run it on Rush?
The handlers are short; the time goes into the test harness, because vitest-pool-workers needs its own config, an isolated D1 with the migrations applied, and the exact dependency versions the monorepo's syncpack lint allows. In a Rush sandbox the agent sets that up, runs the tests and the dry-run deploy, runs the root checks that the monorepo enforces, and keeps every command and its output in the run's record.
The same request works on any Worker with a D1 binding, and because the run opens a pull request through the Rush GitHub App, the API arrives with tests that already meet the repository's own five-test rule rather than as a snippet pasted from a tutorial.
Frequently asked questions
Does it add a framework like Hono?
No. Two routes and a preflight fit in a switch on path and method, which keeps the template's zero runtime dependencies. The pull request notes that a project with more than a handful of routes should move to Hono, as the react-router-hono-fullstack-template in the same repository does.
How do the tests get a database?
@cloudflare/vitest-pool-workers reads wrangler.json and provides an isolated in-memory D1 for the DB binding. The beforeAll reads the SQL files under migrations/ and runs them with env.DB.exec, so the seeded three comments exist in every test file.
Is the POST protected?
Only by validation and CORS. The pull request leaves authentication out and says so, since the template has no identity; adding Cloudflare Access or a Turnstile check in front of POST /api/comments is the next step for a public site.
Will the HTML page still work?
Yes. GET / renders the same page from renderHtml.ts with the same query, so the Deploy to Cloudflare preview that the README links is unchanged.
More Cloudflare Workers jobs
The same job on other stacks
- Add a REST API to a CodeIgniter 4 App (CodeIgniter)
- Add a REST API to a Spring Boot App from a Linear Issue (Spring Boot)
- Add a REST API to a Yii2 App (Yii)