> ## Documentation Index
> Fetch the complete documentation index at: https://docs.levelrail.com/llms.txt
> Use this file to discover all available pages before exploring further.

# IaC in TypeScript

> Write apps and databases as typed TypeScript and synthesize the same version 1 resource documents that apply reads.

> [!WARNING]
>
> Experimental and unit-tested only. The package lives in `sdk/iac-typescript` (`@levelrail/iac`), is not published yet, and has no dependencies. Set `APP_EXPERIMENTAL=iac` before you run `diff` or `apply` on its output.

The package only writes files. Plan, diff, apply, prune, drift, approvals and secrets are unchanged: it produces the YAML documents described in [IaC resource kinds](https://docs.levelrail.com/iac/resources.md), and the control plane never runs your code.

## Example

```ts theme={"dark"}
import { project, app, db, secret, synth } from "@levelrail/iac";

const shop = project("shop");
const pg = db("shop-db", { engine: "postgres", version: "16" }).in(shop);
const web = app("web").in(shop).image("ghcr.io/acme/web:1.2.3").port(3000)
  .db(pg, { as: "DATABASE_URL" }).domain("shop.example.com")
  .env({ LOG_LEVEL: "info", STRIPE_KEY: secret("STRIPE_KEY") });

synth("out/", [shop, pg, web]);
```

Run it with Node 22.18 or later (`node infra.ts`), then:

```bash theme={"dark"}
export APP_EXPERIMENTAL=iac
levelrail-cli diff -f out/
levelrail-cli apply -f out/ --secret STRIPE_KEY=env:CI_STRIPE --yes
```

Commit `out/` so a review shows the YAML diff. Output is one file per resource (`out/app/web.yaml`), keys sorted, byte-identical when the code is unchanged.

## What it covers

| Builder | Document |
| - | - |
| `project`, `environment` | Project, Environment |
| `db(name, { engine, version })` | Database. The version is required because apply refuses engine or version changes later. |
| `app(name)` with `.image()` or `.compose()` | App with an image service or a compose reference |
| `secret(name)`, `input(name)` | `{ secretRef }` and `${{ env.NAME }}` |
| `raw(kind, name, spec)` | Any other kind; the server schema still validates it |

Git builds are not available: apply only accepts image builds today. `.extra({...})` sets any other service field the schema allows.

## Guards

* A literal on a secret-looking key (or a token-shaped value) throws at synth and names the key, never the value. The server guard remains the real gate.
* Names are checked against the schema pattern, so a name can never escape the output folder; symlinked output paths are refused.
* Undeclared project or database references print warnings, since the target may exist live.
* Limits: `LEVELRAIL_IAC_SYNTH_MAX_FILES` (default 500) and `LEVELRAIL_IAC_SYNTH_MAX_BYTES` (default 5 MiB).

Removing a domain or env key from code does not remove it live unless you apply with `--prune --source`, the same as hand-written YAML.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.