The best way to control Claude Code, Codex and more from any computer or phone
| 1 | --- |
| 2 | title: "Part 4: Secrets and Cleanup" |
| 3 | description: Read a secret from .env in the Service with Config.Redacted, guard a route with it, and destroy the stack. |
| 4 | sidebar: |
| 5 | order: 4 |
| 6 | --- |
| 7 | |
| 8 | import Terminal from "../../../../components/Terminal.astro"; |
| 9 | import PackageManagers from "../../../../components/PackageManagers.astro"; |
| 10 | |
| 11 | In this part the Service reads an API token from your `.env` with |
| 12 | `Config.Redacted` and uses it to guard a route. Then you tear the |
| 13 | stack down so billing stops. |
| 14 | |
| 15 | ## Add the secret to `.env` |
| 16 | |
| 17 | Create a `.env` file next to `alchemy.run.ts`: |
| 18 | |
| 19 | ```sh |
| 20 | # .env |
| 21 | API_TOKEN=not-a-real-token |
| 22 | ``` |
| 23 | |
| 24 | Alchemy reads Config values from the env of whoever runs the deploy, |
| 25 | which includes `.env`. Keep `.env` out of version control. |
| 26 | |
| 27 | ## Read it in the constructor |
| 28 | |
| 29 | Yield `Config.Redacted` in the Service's constructor Effect: |
| 30 | |
| 31 | ```diff lang="typescript" |
| 32 | // src/api.ts |
| 33 | +import * as Config from "effect/Config"; |
| 34 | // ... |
| 35 | Effect.gen(function* () { |
| 36 | const mount = yield* Fly.MountVolume({ path: "/data", sizeGb: 1 }); |
| 37 | const fs = yield* FileSystem.FileSystem; |
| 38 | + const apiToken = yield* Config.Redacted("API_TOKEN"); |
| 39 | |
| 40 | return { |
| 41 | fetch: /* ... */, |
| 42 | }; |
| 43 | }).pipe(Effect.provide(Fly.MountVolumeLive)), |
| 44 | ``` |
| 45 | |
| 46 | At deploy time Alchemy reads `API_TOKEN` and writes it onto the |
| 47 | Machine as a secret. At runtime the same line resolves from that env |
| 48 | var. `apiToken` is `Redacted<string>`, so it is never logged. |
| 49 | |
| 50 | ## Guard a route with the token |
| 51 | |
| 52 | Add a `/secret` route that answers only when the request carries the |
| 53 | token: |
| 54 | |
| 55 | ```diff lang="typescript" |
| 56 | // src/api.ts |
| 57 | +import * as Redacted from "effect/Redacted"; |
| 58 | // ... |
| 59 | if (url.pathname === "/health") { |
| 60 | return HttpServerResponse.json({ ok: true }); |
| 61 | } |
| 62 | +if (url.pathname === "/secret") { |
| 63 | + const expected = `Bearer ${Redacted.value(apiToken)}`; |
| 64 | + return request.headers["authorization"] === expected |
| 65 | + ? HttpServerResponse.text("authorized") |
| 66 | + : HttpServerResponse.text("unauthorized", { status: 401 }); |
| 67 | +} |
| 68 | const file = `${mount.path}${url.pathname}`; |
| 69 | ``` |
| 70 | |
| 71 | `Redacted.value` unwraps the token only where the comparison needs |
| 72 | the raw string. |
| 73 | |
| 74 | ## Deploy |
| 75 | |
| 76 | <PackageManagers run="alchemy deploy" /> |
| 77 | |
| 78 | <Terminal content={`[u]Plan[/u]: [y]1 to update[/y] |
| 79 | |
| 80 | [y]~[/y] [b]Api[/b] [d](Fly.Service)[/d] |
| 81 | |
| 82 | Proceed? |
| 83 | ◉ Yes ○ No |
| 84 | [g]✓[/g] [b]Api[/b] [d](Fly.Service)[/d] [y]updated[/y]`} /> |
| 85 | |
| 86 | ## Try it out |
| 87 | |
| 88 | ```sh |
| 89 | curl https://myapp-api-dev-a1b2c3d4.fly.dev/secret |
| 90 | # → unauthorized |
| 91 | |
| 92 | curl -H "Authorization: Bearer not-a-real-token" \ |
| 93 | https://myapp-api-dev-a1b2c3d4.fly.dev/secret |
| 94 | # → authorized |
| 95 | ``` |
| 96 | |
| 97 | :::tip[Sharing a secret or adding your own domain?] |
| 98 | A value several Services share belongs in a |
| 99 | [`Fly.Secret`](/fly/data/secrets#when-fly-should-own-the-secret), and |
| 100 | a custom domain needs a |
| 101 | [`Fly.Certificate`](/fly/networking#use-your-own-hostname). Both |
| 102 | attach to a `Fly.App`, so the Services that use them join that App |
| 103 | with [`app`](/fly/compute/services#group-services-in-one-app). |
| 104 | ::: |
| 105 | |
| 106 | ## Clean up |
| 107 | |
| 108 | You're done — tear everything down so Machine and Volume billing |
| 109 | stops: |
| 110 | |
| 111 | <PackageManagers run="alchemy destroy" /> |
| 112 | |
| 113 | Deleting the Service deletes its Machine, its Volume, and the App it |
| 114 | created, along with the App's addresses. |
| 115 | |
| 116 | ## Recap |
| 117 | |
| 118 | Over four parts you built a complete Fly deployment: |
| 119 | |
| 120 | - An HTTP Service in its own App with a generated globally unique |
| 121 | name and a public `fly.dev` URL |
| 122 | - Machines updated only when the code hash changes |
| 123 | - A Volume mounted at `/data` whose contents outlive deploys |
| 124 | - A secret from `.env` readable from the Service, never logged |
| 125 | |
| 126 | ## Where next |
| 127 | |
| 128 | - [Services](/fly/compute/services) — private Services, background |
| 129 | workers, and grouping Services in one App. |
| 130 | - [IPs & certificates](/fly/networking) — public and private |
| 131 | Services, and your own hostname. |
| 132 | - [Machines](/fly/compute/machines) — raw images without a bundle. |
| 133 | - [Volumes](/fly/data/volumes) — snapshots, extend, region rules. |
| 134 | - [Secrets](/fly/data/secrets) — `Fly.Secret`, `GetSecret` / |
| 135 | `ListSecrets` / `WriteSecret`, plus KMS `SecretKey` with |
| 136 | `Encrypt` / `Decrypt` / `Sign` / `Verify`. |
| 137 | - [Testing](/testing) — deploy this stack from an integration test |
| 138 | and drive it over HTTP. |
| 139 | - [CI](/environments/ci) — run `alchemy deploy` from GitHub Actions |
| 140 | with `FLY_API_TOKEN`. |