# Add CORS to a Serverless Framework API from a Linear Issue

Yes. An agent picks up the Linear issue, adds an httpApi.cors block with your origins to serverless.yml, checks the packaged template and opens a pull request.

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

## What the agent does

The agent starts from the Linear issue and the repository in its own sandbox and reads how the API is declared before it adds a header. In a service shaped like `aws-node-http-api` in serverless/examples, `serverless.yml` names the service `serverless-http-api`, pins `frameworkVersion: "4"`, runs on `nodejs24.x` with `architecture: arm64`, and has one function, `hello`, on an `httpApi` event for `GET /`. `handler.js` returns a `statusCode` of 200 and a JSON body and sets no headers at all, so a browser on another origin gets no `Access-Control-Allow-Origin` and refuses the response. The TypeScript twin, `aws-node-http-api-typescript`, has the same shape with `handler.ts` typed as `APIGatewayProxyHandlerV2`, and `aws-node-express-api` routes `httpApi: "*"` into Express through `serverless-http`.

Because these are HTTP API events (API Gateway v2), CORS belongs on the API, not in every handler. The agent adds `httpApi.cors` under `provider` with the origins the issue names: `allowedOrigins` with the exact site, `allowedHeaders` with `Content-Type` and `Authorization`, `allowedMethods` with the verbs the functions actually serve, `allowCredentials` only when the issue asks for cookies, and a `maxAge` so browsers cache the preflight. It does not write `cors: true`, which answers `*` for every origin, unless the issue asks for a public API. The `aws-node-http-api-dynamodb` example in the same repository already uses the short form, so the agent cites it in the pull request as the shape it extended.

Handlers that build responses by hand need nothing more, since API Gateway adds the headers on the way out and answers `OPTIONS` itself. For the Express variant, where every route is proxied, the agent leaves Express without a `cors` package, because a second set of headers from the app would duplicate the gateway's. A small `README.md` section explains which origins are allowed and how to add one.

Then it checks the result the way a deploy would read it. `serverless package` writes `.serverless/cloudformation-template-update-stack.json`, and the agent reads the `AWS::ApiGatewayV2::Api` resource there to confirm `CorsConfiguration` carries exactly the origins, headers, methods and max age it wrote. It runs the handler once with `node` and a sample event to confirm the response body is unchanged, and opens a pull request that links the issue.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "A Linear issue labeled agent says: \"Add CORS to the HTTP API so the web app at https://app.example.com can call it from the browser. Allow GET, POST and OPTIONS with Content-Type and Authorization headers; no credentials.\" Do the work in this repository: add an httpApi.cors block under provider in serverless.yml with exactly those origins, headers and methods and a maxAge, leave the handlers' bodies as they are, run serverless package and confirm the generated CloudFormation template's AWS::ApiGatewayV2::Api resource has the matching CorsConfiguration, and open a pull request that links back to the issue.",
  "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 on its own branch that links the Linear issue. The diff adds a `httpApi.cors` block under `provider` in `serverless.yml` with the origins, headers, methods and `maxAge` from the issue, and a README section that says which origins may call the API. No handler changes, unless a handler already set a conflicting `Access-Control-Allow-Origin` header, in which case the pull request removes it and says so.

The run's record keeps the `serverless package` output and the lines of the generated template under `AWS::ApiGatewayV2::Api` that show `CorsConfiguration` with `AllowOrigins`, `AllowHeaders`, `AllowMethods` and `MaxAge`. That is the same resource `serverless deploy` would create, read before anything reaches AWS.

Before merging, a reviewer checks the origin list against the real site URLs, including the scheme and any port, confirms `allowCredentials` is off unless cookies are needed, and confirms the method list matches the functions' events rather than every verb.

## Run it on every Linear issue labelled agent

In the console, an automation with a `linear` trigger on `linear.issue.labeled` and the label `agent` starts one run per labelled issue, with the issue as its context, in the Project that has the repository. Updates made by an app integration never fire it, so the agent's own comments do not start new runs.

From your own server, the same run is one `POST /o/{org}/sessions` per issue: the repository in `resources`, plus the issue's `https://linear.app/<workspace>/issue/<ID>` URL there too, which Rush reads through your Linear connection into the prompt. A webhook on the API key posts `run.completed` or `run.failed` when the turn finishes, with a `session_url` to read the pull request link.

## Why run it on Rush?

CORS mistakes only show up in a browser on another origin, and the fix lives in a YAML block that compiles to a CloudFormation resource. The sandbox has Node and the Serverless CLI, so the agent packages the service and reads the resource it would deploy, which is a stronger check than eyeballing the YAML and a cheaper one than a deploy.

A labelled issue becomes a reviewable pull request without anyone opening a terminal. Several issues against the same repository run in their own sandboxes at once, each on its own branch, and the run record keeps the packaged template lines that prove the headers are what the issue asked for.

## Frequently asked questions

### Does the agent need AWS credentials?

No. `serverless package` builds the CloudFormation template locally without touching AWS, and that template is what the agent checks. Deploying stays with you. Serverless Framework v4 does ask for a sign-in or a `SERVERLESS_ACCESS_KEY` before it runs; a secret with that name in the Project reaches the sandbox as an environment variable.

### Why not add the headers in the handler?

An `httpApi` event is API Gateway v2, which answers the `OPTIONS` preflight itself and sets the headers on every response when `cors` is configured. Headers written in the handler would only cover the responses the handler returns, not the preflight, and would duplicate the gateway's.

### My service uses the older http events, not httpApi. Does this still work?

REST API (`http`) events take `cors` per event rather than on the provider. The agent reads which kind each function uses and writes the matching form, with the same origin list, so the pull request fits the API you have.

### Can I allow more than one origin?

Yes. `allowedOrigins` is a list, so staging and production sites can both be named. The agent lists each origin exactly as the issue gives it and refuses a wildcard unless the issue says the API is public.

## More Serverless Framework jobs

- [Add Cursor Pagination to a Serverless DynamoDB API](https://getrush.ai/run/add-cursor-pagination/serverless/)
- [Add a Custom Domain to a Serverless Framework API](https://getrush.ai/run/add-custom-domain/serverless/)
- [Add a Dead-Letter Queue to a Serverless SQS Worker](https://getrush.ai/run/add-dlq/serverless/)

## The same job on other stacks

- [Add CORS to a Slim 4 API](https://getrush.ai/run/add-cors/slim/)
- [Add CORS to a Socket.IO Server](https://getrush.ai/run/add-cors/socket-io/)
- [Add CORS to a tRPC API](https://getrush.ai/run/add-cors/trpc/)

## 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/)
