Repository navigation
Commit 27836bc
authored
feat(setup): per-org API key variables; stop one org's key reaching another (#83)
## Value
**V.A.L.U.E. tier:** small. It changes how every command picks its API key and adds a setup refusal. Not micro: it's a behavior change, and it touches key handling.
- **Problem:** an exported `VAPI_PRIVATE_API_KEY` applies to every org and beats each `.env.<org>` file. That is what the docs' coding-agent prompt asks people to do, and a key in a shell profile is the natural way to make setup a single command. It works for one org. Once a second org is connected, it goes wrong without any error:
- Every command for the second org uses the first org's key. The only output is `📁 Loaded env file: .env.<org>`, which suggests the file's key was used.
- `npm run setup -- <second-org>` writes the first org's key into `.env.<second-org>` and downloads the first org into the second org's folder.
- The interactive wizard saves the key you paste, but its download step inherits the exported key, so the same thing happens there.
- **Who it affects:** anyone running more than one org from one shell, such as development, staging and production. That's the setup where gitops starts to pay off, and the one a production deploy can reach with the wrong key.
- **What changes:**
- **Per-org variables.** Every command looks for `VAPI_PRIVATE_API_KEY_<ORG>` first (`acme-dev` → `VAPI_PRIVATE_API_KEY_ACME_DEV`), then `VAPI_PRIVATE_API_KEY`, then `.env.<org>`. Exporting one per org in a shell profile is safe, and setup stays a single command. Org slugs are lowercase letters, digits and hyphens, so two orgs can't map to the same variable name.
- **Warn, or refuse.** When the generic variable overrides a *different* key in `.env.<org>`, every command warns on stderr. The warning names both sources, never the values. `setup` refuses outright, because it would write the key and download the org. It also refuses a key that another org's `.env` file already holds.
- **Say where the key came from.** Commands print `🔑 Using the API key from the VAPI_PRIVATE_API_KEY_ACME_DEV environment variable` when the key wasn't read from `.env.<org>`.
- **Pin the chosen key in child processes.** The wizard's download, and the child runs that `promote` and `check` start, all receive the key that was actually chosen.
- **Placeholders count as no key everywhere.** A `.env.example` placeholder no longer gets as far as an HTTP 401.
- **Docs:** README, AGENTS.md, the configuration guide, `.env.example` and `setup --help` recommend the per-org name. AGENTS.md tells agents to stop and ask rather than unset variables to silence a warning.
- **Unchanged:** CI that sets `VAPI_PRIVATE_API_KEY` for a single org, `VAPI_CHECK_TOKENS` and `VAPI_PROMOTION_TOKENS` all behave as before. `.env.<org>` files still work on their own.
**Alignment needed:** none beyond review. [VapiAI/docs#1371](VapiAI/docs#1371) updates docs.vapi.ai to recommend the per-org name and should merge after this.
## Evidence of value
| Shell exports org A's key as `VAPI_PRIVATE_API_KEY`; `.env.org-b` holds org B's key | Before | After |
| --- | --- | --- |
| `npm run <command> -- org-b` | Uses org A's key; prints only "Loaded env file" | Uses org A's key and warns that it overrides `.env.org-b`, naming `VAPI_PRIVATE_API_KEY_ORG_B` as the fix |
| `npm run setup -- org-b` | Writes org A's key to `.env.org-b`, downloads org A | Refuses before validating or writing anything |
| `npm run setup -- org-b` with `.env.org-a` already holding that key | Same as above | Refuses: "already the key in .env.org-a" |
| `VAPI_PRIVATE_API_KEY_ORG_B` exported as well | n/a | Uses org B's key, no warning |
- **Tests:** new cases in `api-key.test.ts`, `setup-noninteractive.test.ts` and `org-connection.test.ts`, plus `config-api-key.test.ts`, which runs `validate` to check what a command reports about its key. Every case checks that the key values never appear in output. `tests/no-vapi-api.ts` now also clears per-org variables a developer may have exported.
- **Live run** against a production org: `setup -- <org> --resources none` with the real key in `VAPI_PRIVATE_API_KEY_<ORG>` and a fake value in `VAPI_PRIVATE_API_KEY`. Setup used the per-org key, connected, wrote `.env.<org>` and synced bindings. The pull listed all 13 assistants, then stopped on a platform-side 503 from `/eval/simulation/personality` that persisted through retries. That outage is unrelated to this change.
## Testing plan
- `npm run build` and `npm test` (562) pass.
- **Not covered by tests:** the wizard's download step needs a TTY. After merge, run `npm run setup` in a terminal that exports a different org's `VAPI_PRIVATE_API_KEY`, paste the intended org's key, and check that the downloaded folder holds the intended org's resources.
🤖 Generated with [Claude Code](https://claude.com/claude-code)18 files changed
Lines changed: 453 additions & 65 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
19 | 22 | | |
20 | 23 | | |
21 | 24 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
| 56 | + | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | | - | |
102 | | - | |
103 | | - | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
104 | 107 | | |
105 | 108 | | |
106 | 109 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
123 | | - | |
124 | | - | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
125 | 126 | | |
126 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
127 | 137 | | |
128 | 138 | | |
129 | 139 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
18 | 34 | | |
19 | 35 | | |
20 | 36 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
36 | 100 | | |
37 | 101 | | |
38 | 102 | | |
39 | 103 | | |
40 | 104 | | |
41 | 105 | | |
| 106 | + | |
42 | 107 | | |
43 | 108 | | |
44 | 109 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
149 | 149 | | |
150 | 150 | | |
151 | 151 | | |
152 | | - | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
153 | 155 | | |
154 | 156 | | |
155 | 157 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
215 | 215 | | |
216 | 216 | | |
217 | 217 | | |
218 | | - | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
219 | 222 | | |
220 | 223 | | |
221 | 224 | | |
| |||
243 | 246 | | |
244 | 247 | | |
245 | 248 | | |
| 249 | + | |
246 | 250 | | |
247 | 251 | | |
248 | 252 | | |
| |||
251 | 255 | | |
252 | 256 | | |
253 | 257 | | |
| 258 | + | |
254 | 259 | | |
255 | 260 | | |
256 | 261 | | |
| |||
273 | 278 | | |
274 | 279 | | |
275 | 280 | | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
280 | 286 | | |
281 | | - | |
| 287 | + | |
| 288 | + | |
282 | 289 | | |
283 | 290 | | |
284 | | - | |
| 291 | + | |
285 | 292 | | |
286 | 293 | | |
287 | 294 | | |
288 | 295 | | |
289 | 296 | | |
290 | 297 | | |
291 | | - | |
292 | | - | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
293 | 304 | | |
294 | 305 | | |
295 | 306 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
169 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
170 | 172 | | |
171 | 173 | | |
172 | 174 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
| 73 | + | |
73 | 74 | | |
74 | 75 | | |
75 | | - | |
| 76 | + | |
76 | 77 | | |
77 | 78 | | |
78 | 79 | | |
| |||
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
| 100 | + | |
| 101 | + | |
99 | 102 | | |
100 | 103 | | |
101 | 104 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
53 | 55 | | |
54 | 56 | | |
55 | 57 | | |
| |||
0 commit comments