# Add Request ID Logging to a React Router App

Yes. An agent gives every request to your React Router app a request id, logs it with the Express access line, passes it into loaders and actions, and opens a PR.

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

## What the agent does

The agent clones the app into its own cloud sandbox and reads how requests reach the framework before it changes a line. In a React Router app built from the node-custom-server template in remix-run/react-router-templates, `server.js` creates an Express 5 app, enables `compression`, disables `x-powered-by`, and in production serves `build/client/assets` with a one-year cache, logs with `morgan("tiny")`, and hands everything else to the built server bundle; in development it mounts Vite's middleware and loads `server/app.ts` through `ssrLoadModule` on every request. `server/app.ts` calls `createRequestHandler` from `@react-router/express` with a `getLoadContext` that builds a `RouterContextProvider` and sets `valueFromExpressContext`, a typed context created in `app/context.ts`, which the loader in `app/routes/home.tsx` reads. `package.json` has `dev`, `build`, `start` and `typecheck` scripts and no test script, and `tsconfig.node.json` type-checks `server.js` itself with `checkJs`.

The agent adds one Express middleware at the top of `server.js`, before `compression`, that reads an incoming `x-request-id` header when the deployment's load balancer sets one and otherwise generates an id with `crypto.randomUUID()`, stores it on `res.locals`, and sets it on the response as `x-request-id` so a user can quote it from the browser's network panel. It registers a `morgan` token for the id and replaces the `tiny` format with one that includes it, and it moves the access log so development requests are logged too, since the template only logs in production.

To make the id available to route code, the agent creates a second typed context in `app/context.ts`, `requestIdContext`, and sets it in `getLoadContext` in `server/app.ts` from the `res.locals` value, the same way the template sets its example string. A small `app/lib/logger.server.ts` exports a `log(context, message, fields)` helper that writes one JSON line with the id, so a loader or action can log with the request it belongs to, and `app/routes/home.tsx` is updated to call it once so the pattern has a worked example. The root `ErrorBoundary` in `app/root.tsx` gets the id through a root loader and prints it under the error text, so a support ticket can carry the exact id to search the logs for.

Then the agent proves it. It runs `npm run typecheck`, which runs `react-router typegen` and `tsc -b` across both tsconfig projects, so the typed context and the `checkJs` pass on `server.js` are verified. It runs `npm run build` and `npm start`, sends requests with `curl`, once with its own `x-request-id` header and once without, and confirms the same id appears in the response header, in the access line and in the loader's JSON line. It runs `npm run dev` and repeats the check so the Vite path is covered too, and it builds the `Dockerfile`'s `node:24-alpine` image to make sure `server.js` still starts there.

## Start it with one API call

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

```json
{
  "agent": "codex",
  "input": "Add request id logging to this React Router app, which runs on the Express server in server.js with server/app.ts as the framework entry. Read server.js, server/app.ts, app/context.ts, app/root.tsx, app/routes/home.tsx, package.json and the tsconfig files first. Add middleware at the top of server.js that reuses an incoming x-request-id header or generates one with crypto.randomUUID(), stores it on res.locals and sets it on the response. Register a morgan token for it, replace the tiny format with one that includes the id, and log in development as well as production. Create a typed requestIdContext in app/context.ts, set it in getLoadContext in server/app.ts, add an app/lib/logger.server.ts helper that writes one JSON line per call with the id, use it once in the home loader, and show the id in the root ErrorBoundary through a root loader. Run npm run typecheck, npm run build and npm start, send requests with and without an x-request-id header and confirm the id appears in the response header, the access line and the loader log; repeat under npm run dev; build the Dockerfile. Open a pull request listing the files changed and what each check printed.",
  "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 changing `server.js`, `server/app.ts`, `app/context.ts`, `app/root.tsx` and `app/routes/home.tsx`, and adding `app/lib/logger.server.ts`. No dependency is added: `morgan` and `crypto` are already there, and `@types/morgan` already covers the token API. The description shows one access line and one loader line from the sandbox with the same id, and names the header the middleware reads so an operator can set it at the edge.

The transcript at `GET /o/{org}/sessions/{id}/messages` keeps the `npm run typecheck`, `npm run build`, `npm start` and `curl` output, including the response headers, so a reviewer can see the id round trip rather than read that it did. The session read names the repository and the pull request.

Before merging, a reviewer checks that the middleware runs before `compression` and before the static handlers, that the incoming header is only trusted where the load balancer is known to set it, and that the error boundary shows the id without the stack in production. A follow-up on the same session can forward the id to an outgoing fetch header so a downstream API logs the same value.

## Run it on every new service built from the template

A team that creates a React Router service from the same template each month runs the prompt once per repository. From your own server, send the body above to `POST /o/{org}/sessions` with the new repository in `resources` and a `request_id` that is a UUID v5 of its name, so a retry returns the run that already started. Give the key a `webhook_url` and every run the key starts posts `run.completed` with `data.session_url` when its turn ends; a handler reads the pull request link from the session and posts it to the team channel.

For a service that already exists, start the run from the console and watch it live, or add an automation with a `github` trigger on `pull_request.opened` and a prompt that checks each new pull request's loaders and actions still log through the helper, with the pull request event placed after the prompt.

## Why run it on Rush?

Request ids are only useful when the same value shows up in every place a request touches: the response header, the access line, the loader's log and the error page. Checking that takes a running server and real requests, under both the Vite development path and the built production path. The agent does both in its own sandbox and quotes the lines in the pull request, so the review is of output rather than of intent.

The commands and their output stay in the run's transcript, including the `curl` headers, and the laptop that handed off the task is free while the build and type check run. Several services can get the same change at once, each in its own sandbox and its own pull request.

## Frequently asked questions

### Does it add a logging library?

No. The template already depends on `morgan` for the access line, and the loader helper writes one JSON line with `console.log`, which is what `morgan` does under the hood. Swapping the helper for pino or another structured logger is a one-file change in `app/lib/logger.server.ts` that the pull request leaves to you.

### Will it trust a request id sent by a client?

Only if you tell it to. The middleware reuses an incoming `x-request-id` so an id set by your load balancer survives, and the pull request says so plainly. If your edge does not set one, say so in the prompt and the agent generates an id for every request and ignores the header.

### How does a loader get the id?

Through the typed context the template already uses. `server/app.ts` sets `requestIdContext` in `getLoadContext`, and a loader or action calls `context.get(requestIdContext)`, exactly as `app/routes/home.tsx` reads `valueFromExpressContext` today.

### Does this work with the default template that uses react-router-serve?

Not without a custom server, because `react-router-serve` gives you no place to add Express middleware. The agent would first switch the app to the node-custom-server layout, with `server.js` and `server/app.ts`, and say so in the pull request.

## More React Router jobs

- [Add Security Headers and a CSP to a React Router App](https://getrush.ai/run/add-security-headers/react-router/)
- [Add a Sitemap to a React Router App](https://getrush.ai/run/add-sitemap/react-router/)
- [Add Structured Logging to a React Router App With a Custom Express Server](https://getrush.ai/run/add-structured-logging/react-router/)

## The same job on other stacks

- [Add Request ID Logging to a Rocket App](https://getrush.ai/run/add-request-id-logging/rocket/)
- [Add Request ID Logging to a Serverless Express API](https://getrush.ai/run/add-request-id-logging/serverless/)
- [Add Request ID Logging to a Slim App](https://getrush.ai/run/add-request-id-logging/slim/)

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