|
| 1 | +# Environment variables |
| 2 | + |
| 3 | +PayOnce reads configuration from `process.env`, validated in [`src/config/env.ts`](../../src/config/env.ts) with Zod. |
| 4 | + |
| 5 | +Bun loads dotenv files automatically when you run `bun run dev` or `bun run start`. |
| 6 | + |
| 7 | +## Files in this repo |
| 8 | + |
| 9 | +| File | Committed? | Purpose | |
| 10 | +|------|------------|---------| |
| 11 | +| [`.env.example`](../../.env.example) | Yes | Template for new developers — **local Docker** Postgres + Redis | |
| 12 | +| [`.env.test`](../../.env.test) | Yes | Reference values for tests (overridden by [`tests/setup.ts`](../../tests/setup.ts)) | |
| 13 | +| `.env` | No | **Active local config** — what `bun run dev` uses on your machine | |
| 14 | +| `.env.local` | No | Optional personal dev file (e.g. Neon + Upstash while app runs on localhost) | |
| 15 | +| `.env.production` | No | Private checklist of **Render production** values — paste into Render Dashboard, do not deploy as a file | |
| 16 | + |
| 17 | +Never commit `.env`, `.env.local`, or `.env.production` — they contain secrets. |
| 18 | + |
| 19 | +## Which file should I use? |
| 20 | + |
| 21 | +| Scenario | What to do | |
| 22 | +|----------|------------| |
| 23 | +| First clone | `cp .env.example .env` then `docker compose up -d postgres redis` and `bun run migrate` | |
| 24 | +| Local dev with Docker only | Keep `.env.example` values in `.env` | |
| 25 | +| Local dev with cloud DB (Neon/Upstash) | Put Neon/Upstash URLs in `.env` or `.env.local` | |
| 26 | +| Production on Render | Set variables in **Render → Environment** (use `.env.production` as your private checklist) | |
| 27 | +| Integration tests | Safe even if `.env` points at Neon — [`tests/setup.ts`](../../tests/setup.ts) redirects cloud URLs to local Docker | |
| 28 | + |
| 29 | +## Variable reference |
| 30 | + |
| 31 | +| Variable | Required | Default | Description | |
| 32 | +|----------|----------|---------|-------------| |
| 33 | +| `PORT` | No | `3000` | HTTP port. Render sets this automatically (e.g. `10000`). | |
| 34 | +| `NODE_ENV` | No | `development` | `development`, `production`, or `test` | |
| 35 | +| `DATABASE_URL` | **Yes** | — | PostgreSQL connection string | |
| 36 | +| `REDIS_URL` | No | `redis://localhost:6379` | Redis or Upstash URL (`redis://` or `rediss://`) | |
| 37 | +| `API_KEYS` | No | `dev-api-key` | Comma-separated server API keys (see below) | |
| 38 | +| `DEMO_ENABLED` | No | `true` | Set `false` to disable `/demo/api` | |
| 39 | +| `SIGNUP_ENABLED` | No | `true` | Set `false` to disable public `POST /api/keys` | |
| 40 | +| `SESSION_COOKIE_NAME` | No | `payonce_session` | HttpOnly session cookie for `/auth` and `/dashboard` | |
| 41 | +| `SESSION_TTL_HOURS` | No | `168` | Session lifetime in hours (7 days) | |
| 42 | +| `IDEMPOTENCY_TTL_SECONDS` | No | `86400` | How long idempotency records are kept (24 hours) | |
| 43 | +| `CORS_ORIGINS` | No | (empty) | Comma-separated browser origins. Empty = allow all in dev; restrict in production | |
| 44 | + |
| 45 | +Optional overrides used only during tests: |
| 46 | + |
| 47 | +| Variable | Purpose | |
| 48 | +|----------|---------| |
| 49 | +| `TEST_DATABASE_URL` | Force a specific Postgres URL for integration tests | |
| 50 | +| `TEST_REDIS_URL` | Force a specific Redis URL for integration tests | |
| 51 | + |
| 52 | +## Environment profiles |
| 53 | + |
| 54 | +### Profile A — Local Docker (`.env.example`) |
| 55 | + |
| 56 | +Best for offline dev and matching CI/local test defaults. |
| 57 | + |
| 58 | +```env |
| 59 | +PORT=3000 |
| 60 | +NODE_ENV=development |
| 61 | +DATABASE_URL=postgresql://postgres:postgres@localhost:5433/payonce |
| 62 | +REDIS_URL=redis://localhost:6379 |
| 63 | +API_KEYS=dev-api-key,another-dev-key |
| 64 | +DEMO_ENABLED=true |
| 65 | +SIGNUP_ENABLED=true |
| 66 | +SESSION_COOKIE_NAME=payonce_session |
| 67 | +SESSION_TTL_HOURS=168 |
| 68 | +IDEMPOTENCY_TTL_SECONDS=86400 |
| 69 | +CORS_ORIGINS=http://localhost:3000 |
| 70 | +``` |
| 71 | + |
| 72 | +Requires: |
| 73 | + |
| 74 | +```bash |
| 75 | +docker compose up -d postgres redis |
| 76 | +bun run migrate |
| 77 | +bun run dev |
| 78 | +``` |
| 79 | + |
| 80 | +Postgres is on host port **5433** (not 5432) to avoid clashing with a local Postgres install. |
| 81 | + |
| 82 | +### Profile B — Local app + cloud data (`.env.local` pattern) |
| 83 | + |
| 84 | +Run the app on `http://localhost:3000` but store data in Neon and Upstash: |
| 85 | + |
| 86 | +```env |
| 87 | +PORT=3000 |
| 88 | +NODE_ENV=development |
| 89 | +DATABASE_URL=postgresql://USER:[email protected]/neondb?sslmode=require |
| 90 | +REDIS_URL=rediss://default:[email protected]:6379 |
| 91 | +API_KEYS=dev-api-key,another-dev-key |
| 92 | +DEMO_ENABLED=true |
| 93 | +SIGNUP_ENABLED=true |
| 94 | +SESSION_COOKIE_NAME=payonce_session |
| 95 | +SESSION_TTL_HOURS=168 |
| 96 | +IDEMPOTENCY_TTL_SECONDS=86400 |
| 97 | +CORS_ORIGINS=http://localhost:3000 |
| 98 | +``` |
| 99 | + |
| 100 | +Use Neon’s **pooled** connection string. Wrap `REDIS_URL` in quotes if the value contains special characters. |
| 101 | + |
| 102 | +### Profile C — Production Render (`.env.production` checklist) |
| 103 | + |
| 104 | +Set these in **Render Dashboard → Environment**, not as a file on the server: |
| 105 | + |
| 106 | +```env |
| 107 | +NODE_ENV=production |
| 108 | +DATABASE_URL=postgresql://[email protected]/neondb?sslmode=require |
| 109 | +REDIS_URL=rediss://[email protected]:6379 |
| 110 | +API_KEYS=your-strong-production-key |
| 111 | +DEMO_ENABLED=true |
| 112 | +SIGNUP_ENABLED=true |
| 113 | +SESSION_COOKIE_NAME=payonce_session |
| 114 | +SESSION_TTL_HOURS=168 |
| 115 | +IDEMPOTENCY_TTL_SECONDS=86400 |
| 116 | +CORS_ORIGINS=https://your-app.onrender.com |
| 117 | +``` |
| 118 | + |
| 119 | +Replace `CORS_ORIGINS` with your real Render URL (and any other front-end origins you allow). |
| 120 | + |
| 121 | +`PORT` is injected by Render — do not hardcode it unless debugging. |
| 122 | + |
| 123 | +## Two kinds of API keys |
| 124 | + |
| 125 | +| Type | Stored in | Used for | |
| 126 | +|------|-----------|----------| |
| 127 | +| **Env keys** | `API_KEYS` variable | Server-configured keys; work without dashboard signup | |
| 128 | +| **Personal keys** | Neon `api_keys` table | Created at `/dashboard`; format `pk_live_…` | |
| 129 | + |
| 130 | +Both authenticate `/api/v1` via `Authorization: Bearer <key>`. |
| 131 | + |
| 132 | +Env keys survive database resets. Personal keys are lost if the `api_keys` table is truncated (e.g. accidental test run against cloud DB before the test safety guards). |
| 133 | + |
| 134 | +For Scalar `/docs` Try It, env keys like `pk_live_dev_payonce_scalar01` work without creating a dashboard key. |
| 135 | + |
| 136 | +## GitHub secret (not app env) |
| 137 | + |
| 138 | +| Secret | Where | Purpose | |
| 139 | +|--------|-------|---------| |
| 140 | +| `RENDER_DEPLOY_HOOK` | GitHub → Settings → Secrets → Actions | CD workflow POSTs here after pushing `:latest` to GHCR | |
| 141 | + |
| 142 | +See [../render/how-latest-code-is-deployed.md](../render/how-latest-code-is-deployed.md). |
| 143 | + |
| 144 | +## How Bun loads env files |
| 145 | + |
| 146 | +Typical load order (later overrides earlier): |
| 147 | + |
| 148 | +1. `.env` |
| 149 | +2. `.env.local` |
| 150 | +3. `.env.[NODE_ENV]` (e.g. `.env.development`) |
| 151 | +4. `.env.[NODE_ENV].local` |
| 152 | + |
| 153 | +Keep one active source of truth (usually `.env`) to avoid confusion. |
| 154 | + |
| 155 | +## Integration tests and your `.env` |
| 156 | + |
| 157 | +[`tests/setup.ts`](../../tests/setup.ts) runs before integration tests: |
| 158 | + |
| 159 | +- If `DATABASE_URL` or `REDIS_URL` points at a **cloud host** (Neon, Upstash, etc.), tests switch to **local Docker** URLs. |
| 160 | +- If URLs are already local (e.g. CI on `localhost:5432`), they are kept unchanged. |
| 161 | + |
| 162 | +[`tests/helpers/testUtils.ts`](../../tests/helpers/testUtils.ts) refuses to `TRUNCATE` or `flushdb` remote URLs as a second safety net. |
| 163 | + |
| 164 | +Never disable these guards when pointing at production data. |
| 165 | + |
| 166 | +## Common mistakes |
| 167 | + |
| 168 | +| Mistake | Result | |
| 169 | +|---------|--------| |
| 170 | +| Committing `.env.local` / `.env.production` | Leaked database and Redis credentials | |
| 171 | +| Wrong `CORS_ORIGINS` in production | Browser blocks API calls from your site | |
| 172 | +| Using only the key **prefix** from the dashboard table | 401 — full secret is shown once at creation | |
| 173 | +| Pinning Render GHCR image to an old SHA | Production never updates when CD publishes `:latest` | |
| 174 | + |
| 175 | +## Related docs |
| 176 | + |
| 177 | +- [`.env.example`](../../.env.example) — committed template |
| 178 | +- [`.env.test`](../../.env.test) — test reference |
| 179 | +- [docs/09-deployment-and-devops](../../docs/09-deployment-and-devops/README.md) — Render env setup |
| 180 | +- [docs/08-testing](../../docs/08-testing/README.md) — test env overrides |
| 181 | +- [../api-keys/README.md](../api-keys/README.md) — API key types |
0 commit comments