# Deploy

> Run your own copy of the Towers API on a server.

This section is for whoever hosts the Towers API. If you only call it, you do not need it.

The API is a single Node.js service with a SQLite file beside it. It needs Node.js 24 or Docker, and very little else.

## With Docker

1. **Get the code onto the server** and go to the `backend` folder.

2. **Start it**

   ```sh
   docker compose up -d --build
   ```

   The API listens on `127.0.0.1:7552`, reachable only from the server itself. Its data lives in a Docker volume named `towers-data`, so it survives restarts and rebuilds.

3. **Put HTTPS in front of it**

   Use a reverse proxy to add TLS and to publish only the keyed Engine endpoints `/towers`, `/placements`, `/recoveries` and `/comparisons`. With [Caddy](https://caddyserver.com), which fetches certificates by itself. Replace `towers-api.example.invalid` with your API hostname in the examples below.

   ```txt title="Caddyfile"
   towers-api.example.invalid {
     @engine path /api/v1/towers /api/v1/placements /api/v1/recoveries /api/v1/comparisons
     handle @engine {
       reverse_proxy 127.0.0.1:7552
     }
     handle {
       respond 404
     }
   }
   ```

4. **Issue a key and try it**

   ```sh
   docker compose exec api node dist/cli/keys create "Smoke test"
   ```

   ```sh
   curl https://towers-api.example.invalid/api/v1/towers \
     -H "Authorization: Bearer twr_..." \
     -H "Content-Type: application/json" \
     -d '{"program":{"contracts":[{"contractRef":"C-1","currency":"USD","layers":[{"layerName":"First excess","layerType":"XOL","lowerLimit":100,"upperLimit":200,"isUnlimited":false,"order":100}]}],"risks":[{"riskId":"R-1","contractRefs":["C-1"]}]}}'
   ```

   A `200` with a `risks` array means it is working. Revoke the test key afterwards.

:::caution[Publish only the keyed Engine endpoints]
The `/api/v1/programs` endpoints, used by the bundled programs app, and the Swagger page at `/docs` have no API-key protection. The proxy hides them, including the POST mirrors at `/api/v1/programs/:id/placements`, `/api/v1/programs/:id/recoveries` and `/api/v1/programs/:id/comparisons`. Keep port 7552 private.
:::

## Without Docker

```sh
cd backend
npm ci
npm run build
SEED_DEMO=false node dist/main
```

Keep it running with your process manager of choice, such as systemd.

## Settings

All are environment variables, and all are optional.

| Variable | Default | |
| --- | --- | --- |
| `PORT` | `7552` | The port to listen on. |
| `DATABASE_PATH` | `data/programs.db` (`/data/programs.db` in Docker) | The SQLite file holding API keys and stored programs. |
| `SEED_DEMO` | on | Set to `false` to disable demo seeding, including after legacy rows are cleared. The Docker setup does this. |
| `FRONTEND_URL` | `http://localhost:5173` | The one browser origin allowed to call the API, for the bundled programs app. |

## Back up

Everything worth keeping is the one SQLite file: the fingerprints of the keys you have issued, and any stored programs. Copy it on a schedule. If it is lost, every client needs a new key.

## Update

```sh
git pull
docker compose up -d --build
```

API keys are preserved. Stored requests in the old format without `program.contracts` are cleared on startup; current-format imports are kept. Missing demos are re-seeded after a legacy clear unless `SEED_DEMO=false`.
