# Add CORS to a Hono API

Yes. An agent adds Hono's cors middleware to your Worker with origins read from wrangler vars, tests the preflight with app.request in Vitest, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-cors/hono/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads how the app is wired before it adds a header. In a Hono service shaped like the `basic` workspace of honojs/examples, `package.json` pins `hono ^4.2.4`, `vitest ^1.5.0` and `wrangler ^3.53.0`, with `npm run dev` running `wrangler dev src/index.ts`, `npm test` running `vitest --run`, and `tsconfig.json` loading `@cloudflare/workers-types`. `wrangler.toml` names the Worker `hono-example-basic` with a `compatibility_date` and no `[vars]` block yet. `src/index.ts` builds one `Hono` app, mounts `poweredBy()` on every path, `basicAuth` on `/auth/*`, `etag()` on `/etag/*`, two custom middlewares that set `X-message` and `X-Response-Time`, and the routes: `/`, `/entry/:id`, a nested `book` app under `/book`, `/api/posts` for `GET` and `POST`, and a catch-all `GET /api/*` that answers `404`.

Hono ships `cors` in `hono/cors`, so no dependency is added. The agent registers it with `app.use('/api/*', cors({ ... }))` above the `/api` routes, after `poweredBy()` so the preflight still carries the existing headers, and ahead of `basicAuth`, which stays on `/auth/*` only. `origin` is a function that checks the request's origin against `c.env.ALLOWED_ORIGINS`, a comma-separated `[vars]` entry the agent adds to `wrangler.toml`, so the list is a deployment setting and an empty value allows nothing. `allowMethods` is `GET`, `POST`, `OPTIONS`; `allowHeaders` is `Content-Type` and `Authorization`; `maxAge` is set so browsers cache the preflight. To read `c.env` the app type becomes `new Hono<{ Bindings: { ALLOWED_ORIGINS: string } }>()`, the pattern the `blog` workspace already uses in `src/bindings.ts`.

The blog workspace also shows the shortcut the agent avoids: `blog/src/api.ts` calls `cors()` with no options on `/posts/*`, which answers `Access-Control-Allow-Origin: *`. The pull request explains why the basic app gets an explicit list instead, since `/api/posts` accepts `POST`.

Tests follow `src/index.test.ts`, which calls `app.request('http://localhost/')` and checks status and headers with Vitest globals from `vitest.config.ts`. The new cases pass an env object as the third argument of `app.request`: an `OPTIONS /api/posts` with `Origin` and `Access-Control-Request-Method: POST` from an allowed origin answers `204` with the matching `Access-Control-Allow-Origin` and the allowed headers; the same from another origin gets no allow-origin header; a `GET /api/posts` with an allowed `Origin` carries the header on its `200`; and `GET /` with an `Origin` carries none, because the middleware is scoped to `/api/*`. The agent runs `npm test` and `npx tsc --noEmit` in the sandbox and pastes both.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "codex",
  "input": "Add CORS to this Hono Worker and open a pull request. Read package.json, wrangler.toml, tsconfig.json, vitest.config.ts, src/index.ts and src/index.test.ts first. Use the cors middleware from hono/cors, no new dependency. Register it on /api/* after poweredBy and before the /api routes, with origin as a function that matches the request origin against a comma-separated ALLOWED_ORIGINS var, allowMethods GET, POST and OPTIONS, allowHeaders Content-Type and Authorization, and a maxAge. Add ALLOWED_ORIGINS under [vars] in wrangler.toml, and type the app with Bindings so c.env.ALLOWED_ORIGINS is typed. Add tests to src/index.test.ts using app.request with an env object: a preflight OPTIONS /api/posts from an allowed origin answers 204 with the matching allow-origin and allow-headers, the same from another origin gets no allow-origin header, GET /api/posts from an allowed origin carries the header on its 200, and GET / with an Origin carries none. Run npm test and npx tsc --noEmit and paste the output in the pull request. Do not change the existing routes, the basicAuth credentials or the compatibility_date.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **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.
2. **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.
3. **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 with a few lines in `src/index.ts`: the `cors` import, the typed `Hono` constructor, and one `app.use('/api/*', cors({ ... }))` block; a `[vars]` section in `wrangler.toml`; and four new cases in `src/index.test.ts`. The description shows the preflight response headers from a test run and the output of `vitest --run` and `tsc --noEmit` from the sandbox.

The example repository has no workflow of its own, only `.github/dependabot.yml`, so the test output in the pull request is the check. On a repository with a Vitest workflow the same cases run on the pull request.

A reviewer checks that the middleware is scoped to `/api/*` and not `*`, that an empty `ALLOWED_ORIGINS` allows no origin, that `Authorization` is in the allowed headers only if the frontend will send one, and that the `basicAuth` routes are untouched. Then they set `ALLOWED_ORIGINS` per environment in `wrangler.toml` or the dashboard.

## Run it on every new frontend origin

A team shipping several Workers behind separate frontends sends one `POST /o/{org}/sessions` per repository with this prompt and that repository in `resources`. Each run reads its own `wrangler.toml` and routes, so a Worker that already calls `cors()` with a wildcard gets the explicit list instead, and each run opens its own pull request.

Give the API key a `webhook_url` and Rush POSTs `run.completed` when each turn ends, with the `session_url` to read the pull request from, so the origin list for each Worker can be checked against the frontend that calls it.

## Why run it on Rush?

CORS problems only show in a browser, and the usual fix is a wildcard copied from an example. In a Rush sandbox the agent sends real preflights through `app.request` with `Origin` and `Access-Control-Request-Method` set, from an allowed and a disallowed origin, and the response headers are kept in the run's record beside the test output, so the pull request shows what a browser will see.

The change is a few lines, but the proof is what takes time on a laptop: typing the bindings, wiring the var, and writing the four cases. The agent does that in one pass, runs `vitest` and `tsc` before pushing, and the same request works unchanged on the next Hono service.

## Frequently asked questions

### Why not cors() with no options?

Because that answers `Access-Control-Allow-Origin: *`, which lets any site call `POST /api/posts` from a browser. Reading `ALLOWED_ORIGINS` from `wrangler.toml` keeps the list explicit per deployment and empty until someone sets it.

### Does it add a dependency?

No. `hono/cors` is part of the `hono` package already pinned in `package.json`, so `package-lock.json` does not change.

### How do the tests read the var?

`app.request` takes an env object as its third argument, so each test passes `{ ALLOWED_ORIGINS: 'https://app.example' }` and the middleware reads it from `c.env` the same way it will in the Worker. No Miniflare or `wrangler dev` is needed for these cases.

### What about the basicAuth routes?

They stay on `/auth/*` and the CORS middleware is scoped to `/api/*`, so the two never meet. If a browser frontend needs the protected routes too, the scope widens to `*` and `credentials` turns on, which the pull request notes but does not do.

## More Hono jobs

- [Add a Cron Trigger to a Hono Worker](https://getrush.ai/run/add-cron-trigger/hono/)
- [Add CSRF Protection to a Hono App with Cookie Sessions](https://getrush.ai/run/add-csrf-protection/hono/)
- [Add Cursor Pagination to a Hono API on Workers KV](https://getrush.ai/run/add-cursor-pagination/hono/)

## The same job on other stacks

- [Add CORS to a Koa App](https://getrush.ai/run/add-cors/koa/)
- [Add CORS and Rate Limiting to a Ktor Server from a Linear Issue](https://getrush.ai/run/add-cors/ktor/)
- [Add CORS to a Litestar API](https://getrush.ai/run/add-cors/litestar/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
