# Add Health Checks to an Express and Sequelize API

Yes. An agent adds liveness and readiness endpoints to your Express and Sequelize API, with a bounded database query and tests, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-health-checks/sequelize/). 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 connects to its database before writing a check for it, because a readiness probe that opens a new connection per request is a different thing from one that uses the pool the app already has. In an API like sequelize/express-example, `sequelize/index.js` builds one `Sequelize` instance on the `sqlite` dialect with `storage` at `sqlite-example-database/example-db.sqlite`, `logQueryParameters` and `benchmark` on, defines the `user`, `instrument` and `orchestra` models and applies the `orchestra.hasMany(instrument)` association in `extra-setup.js`. `index.js` calls `sequelize.authenticate()` once at startup inside `assertDatabaseConnectionOk`, exits the process when it fails, and then listens on a hard-coded port of 8080. `express/app.js` registers `body-parser`, a root route with HTML, and the REST routes for each controller under `/api`, each wrapped by `makeHandlerAwareOfAsyncErrors`. `package.json` has `start` and `setup-example-db` scripts, `express` ^4.17.1, `sequelize` ^6.3.3 and `sqlite3` ^5.0.0, an `engines` floor of Node 10, no test script and no dev dependencies, and `.npmrc` turns off lockfiles.

The startup check answers whether the database was there once; an orchestrator asks whether it is there now. The agent adds `express/routes/health.js` with two handlers that `app.js` registers outside the `/api` loop, since they are not a resource. `GET /healthz` is liveness: it returns `{ status: 'ok' }` with no I/O, so a stalled database never makes the scheduler restart a process that is fine. `GET /readyz` is readiness: it runs `SELECT 1` through `sequelize.query` with `QueryTypes.SELECT` on the app's own instance, bounded by a timeout the handler races against with a promise, and answers `200` with `{ status: 'ok', database: 'ok' }` or `503` with `{ status: 'unavailable', database: 'error' }` and the error's message kept out of the body. Both set `Cache-Control: no-store`, so a proxy never serves a stale `200`. Because `sequelize.authenticate()` also issues a trivial query, the pull request explains why the handler uses the explicit statement: the SQLite driver answers it without touching a table, and on another dialect the same line still works.

The agent also fixes the thing the readiness probe makes visible. `index.js` exits on a failed first connection, which is the right behavior for a process manager that restarts it, so that stays; but the port is fixed at 8080, and the pull request moves it behind `process.env.PORT` with 8080 as the default so a container can be given a port without a code change. The HTML on `/` that tells a visitor to run `npm run setup-example-db` is extended with the two new paths. `README.md` gains a short section on what each endpoint means and what a Kubernetes or Compose probe should point at.

The repository has no tests, so the agent adds the smallest harness that proves the two handlers without adding a framework: a `test` script running `node --test` and `test/health.test.js` using Node's built-in runner and `fetch`, which start the app on an ephemeral port, assert `200` from `/healthz` and `/readyz` against the example database created with `npm run setup-example-db`, then point the `Sequelize` instance at a storage path that cannot be opened and assert `503` from `/readyz` while `/healthz` still answers `200`. Since `engines` says Node 10 and `node --test` and global `fetch` need a newer runtime, the agent raises the floor in `engines` and says so in the pull request. It runs `npm install`, `npm run setup-example-db`, `npm test` and `npm start`, requests both endpoints with `curl -i`, and opens the pull request.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add health checks to this Express and Sequelize API. Read package.json, .npmrc, index.js, express/app.js, express/helpers.js, express/routes/users.js, sequelize/index.js, sequelize/extra-setup.js and sqlite-example-database/setup.js first. Add express/routes/health.js with GET /healthz, which answers 200 with status ok and does no I/O, and GET /readyz, which runs SELECT 1 through the app's own sequelize instance with QueryTypes.SELECT, bounded by a timeout, and answers 200 with status ok and database ok, or 503 with status unavailable and database error, never including the error message in the body. Set Cache-Control no-store on both. Register the routes in express/app.js outside the /api loop. Keep the startup authenticate in index.js but read the port from process.env.PORT with 8080 as the default. Mention the endpoints on the root page and in a README section on probes. Add a test script using node --test and test/health.test.js that starts the app on an ephemeral port, asserts 200 from both endpoints against the example database, and asserts 503 from /readyz with /healthz still 200 when the storage path cannot be opened; raise engines.node to a version with node --test and fetch and say why. Run npm install, npm run setup-example-db, npm test and npm start, curl -i both endpoints, and open a pull request that pastes the responses.",
  "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 that adds `express/routes/health.js` and `test/health.test.js`, and changes `express/app.js`, `index.js`, `package.json` and `README.md`. The description pastes `curl -i` for `/healthz` and `/readyz` against the running app, showing the status, the `Cache-Control: no-store` header and the JSON bodies, and the `node --test` summary with the unavailable-database case passing.

The run's record holds the install, the example database setup, the test run and the two requests. The models, the association setup and the `/api` controllers are untouched; the only behavior change to existing code is the port now reading `process.env.PORT`.

Before merging, a reviewer decides the timeout for the readiness query, whether `/readyz` should also confirm the `users` table exists given the root page's warning about an unset-up database, and whether the raised `engines` floor is acceptable or the test should use a dependency that runs on older Node.

## Run it on every new Express service

A team with several Express services on Sequelize starts one run per repository from its own server: one `POST /o/{org}/sessions` each, with that repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name, so a retried call returns the run already started rather than a second pull request. Each run reads that service's own dialect and connection setup, so a service on Postgres with a pool gets a readiness check that reports the pool as well.

Afterwards, an automation on the Project with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` and the prompt "if this pull request adds a dependency the app needs at startup, such as a cache or a queue client, comment with whether /readyz should check it and the line to add" keeps the readiness answer honest as the service grows. The trigger adds the pull request after the prompt; only work by an owner, member or collaborator fires it, never a fork's.

## Why run it on Rush?

Health endpoints are judged by what they do when the database is gone, and that is the case nobody tests by hand. The agent runs the app in its own sandbox, requests both endpoints against the example database, then breaks the storage path and shows liveness still answering while readiness reports the failure, so the reviewer sees both halves rather than the happy path.

The sandbox keeps the setup, the test run and the responses with the run, and the pull request cites them. For a team with many services on this layout, each gets its own run and its own pull request, with the same two paths so one probe configuration works across all of them.

## Frequently asked questions

### Why two endpoints rather than one /health?

Because an orchestrator asks two different questions. Liveness asks whether the process should be restarted, and a database outage is not a reason to restart a healthy Node process. Readiness asks whether traffic should be routed here, and a database outage is exactly that. `/healthz` does no I/O and `/readyz` runs the bounded query; pointing both probes at one endpoint turns every database blip into a restart loop.

### Why SELECT 1 instead of sequelize.authenticate()?

`authenticate()` runs a similar trivial query, but the explicit statement through `sequelize.query` makes the timeout and the error handling visible in the handler and behaves the same on SQLite and on the Postgres or MySQL dialect the README invites readers to switch to. The handler races the query against a timer so a hung connection yields a `503` rather than a hanging probe.

### Why change the port?

The port was a constant of 8080 in `index.js`, and a container or process manager cannot set a constant. Reading `process.env.PORT` with the same default keeps `npm start` as it was and lets a probe configuration and the listener agree. The test uses an ephemeral port through the same variable.

### Why raise the Node floor?

The repository has no test framework, and adding one for two handlers is heavier than using `node --test` and `fetch`, which need a newer runtime than the Node 10 floor in `engines`. The pull request raises the floor and says why; a reviewer who must keep Node 10 can swap the test for a dependency-based one, and the handlers themselves run on the old floor.

## More Sequelize jobs

- [Add Input Validation to a Sequelize and Express API](https://getrush.ai/run/add-input-validation/sequelize/)
- [Add Migrations to a Sequelize Express App](https://getrush.ai/run/add-migrations/sequelize/)
- [Add Optimistic Locking to a Sequelize App](https://getrush.ai/run/add-optimistic-locking/sequelize/)

## The same job on other stacks

- [Add Health Checks to a Socket Mode Slack Bolt App](https://getrush.ai/run/add-health-checks/slack-bolt/)
- [Add a Health Check Endpoint to a Slim 4 App](https://getrush.ai/run/add-health-checks/slim/)
- [Add a Health Check Endpoint to a Socket.IO Server](https://getrush.ai/run/add-health-checks/socket-io/)

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