| 1 | --- |
| 2 | title: "Part 1: Your First Server" |
| 3 | description: Install Alchemy, create a Stack with a Hetzner Server, and deploy it. |
| 4 | sidebar: |
| 5 | order: 1 |
| 6 | --- |
| 7 | |
| 8 | import Terminal from "../../../../components/Terminal.astro"; |
| 9 | import { Code, Tabs, TabItem } from "@astrojs/starlight/components"; |
| 10 | import { effectVersion } from "../../../../versions"; |
| 11 | |
| 12 | export const pkgs = `"alchemy@latest" "effect@${effectVersion}" "@effect/platform-bun@${effectVersion}" "@effect/platform-node@${effectVersion}"`; |
| 13 | |
| 14 | In this first part you'll install Alchemy and Effect, create a Stack |
| 15 | with a Hetzner Cloud Server, deploy it, and SSH in — all in under |
| 16 | five minutes. |
| 17 | |
| 18 | ## Prerequisites |
| 19 | |
| 20 | - [Bun](https://bun.sh) or Node.js 22+ |
| 21 | - A Hetzner project and an API token — see [Setup](/hetzner/setup) |
| 22 | if you haven't created those yet |
| 23 | |
| 24 | :::note[Costs] |
| 25 | Hetzner bills servers by the hour. The `cx22` used in this tutorial |
| 26 | costs about €4/month — a few cents for the time it takes to finish |
| 27 | all four parts. `alchemy destroy` tears everything down when you're |
| 28 | done. |
| 29 | ::: |
| 30 | |
| 31 | ## Create a project |
| 32 | |
| 33 | Start with an empty directory and initialize a `package.json`: |
| 34 | |
| 35 | <Tabs syncKey="pkgManager"> |
| 36 | <TabItem label="bun" icon="bun"> |
| 37 | <Code code="mkdir my-app && cd my-app && bun init -y" lang="sh" /> |
| 38 | </TabItem> |
| 39 | <TabItem label="npm" icon="npm"> |
| 40 | <Code code="mkdir my-app && cd my-app && npm init -y" lang="sh" /> |
| 41 | </TabItem> |
| 42 | <TabItem label="pnpm" icon="pnpm"> |
| 43 | <Code code="mkdir my-app && cd my-app && pnpm init" lang="sh" /> |
| 44 | </TabItem> |
| 45 | <TabItem label="yarn" icon="seti:yarn"> |
| 46 | <Code code="mkdir my-app && cd my-app && yarn init -y" lang="sh" /> |
| 47 | </TabItem> |
| 48 | </Tabs> |
| 49 | |
| 50 | ## Install dependencies |
| 51 | |
| 52 | Install <code>alchemy@latest</code> and <code>effect@{effectVersion}</code>: |
| 53 | |
| 54 | <Tabs syncKey="pkgManager"> |
| 55 | <TabItem label="bun" icon="bun"> |
| 56 | <Code code={`bun add ${pkgs}`} lang="sh" /> |
| 57 | </TabItem> |
| 58 | <TabItem label="npm" icon="npm"> |
| 59 | <Code code={`npm install ${pkgs}`} lang="sh" /> |
| 60 | </TabItem> |
| 61 | <TabItem label="pnpm" icon="pnpm"> |
| 62 | <Code code={`pnpm add ${pkgs}`} lang="sh" /> |
| 63 | </TabItem> |
| 64 | <TabItem label="yarn" icon="seti:yarn"> |
| 65 | <Code code={`yarn add ${pkgs}`} lang="sh" /> |
| 66 | </TabItem> |
| 67 | </Tabs> |
| 68 | |
| 69 | ## Create the Stack |
| 70 | |
| 71 | Every Alchemy program starts with a `Stack` — a collection of |
| 72 | Resources managed by Providers with state tracked between deploys. |
| 73 | |
| 74 | Create an `alchemy.run.ts` file: |
| 75 | |
| 76 | ```typescript |
| 77 | // alchemy.run.ts |
| 78 | import * as Alchemy from "alchemy"; |
| 79 | import * as Effect from "effect/Effect"; |
| 80 | import * as Layer from "effect/Layer"; |
| 81 | |
| 82 | export default Alchemy.Stack( |
| 83 | "MyApp", |
| 84 | { |
| 85 | // @error: ts(2345) Argument of type '{ providers: Layer.Layer<never, never, never>; }' is not assignable to parameter of type 'StackProps<never>'. |
| 86 | // @error: Property 'state' is missing in type '{ providers: Layer.Layer<never, never, never>; }' but required in type 'StackProps<never>'. |
| 87 | providers: Layer.empty, |
| 88 | }, |
| 89 | Effect.gen(function* () { |
| 90 | // we'll add resources here next |
| 91 | }), |
| 92 | ); |
| 93 | ``` |
| 94 | |
| 95 | TypeScript is unhappy: the `state` property is required. Every Stack |
| 96 | needs a **state store** so Alchemy can persist resource state between |
| 97 | deploys and compute diffs against your infrastructure. |
| 98 | |
| 99 | ## Configure state |
| 100 | |
| 101 | Hetzner has no state backend of its own, so we'll keep state on disk |
| 102 | with `Alchemy.localState()` — it writes to `.alchemy/` next to your |
| 103 | code, no setup required: |
| 104 | |
| 105 | ```diff lang="typescript" |
| 106 | // alchemy.run.ts |
| 107 | import * as Alchemy from "alchemy"; |
| 108 | import * as Effect from "effect/Effect"; |
| 109 | import * as Layer from "effect/Layer"; |
| 110 | |
| 111 | export default Alchemy.Stack( |
| 112 | "MyApp", |
| 113 | { |
| 114 | providers: Layer.empty, |
| 115 | + state: Alchemy.localState(), |
| 116 | }, |
| 117 | Effect.gen(function* () { |
| 118 | // we'll add resources here next |
| 119 | }), |
| 120 | ); |
| 121 | ``` |
| 122 | |
| 123 | :::note[Other state stores] |
| 124 | Local state is perfect for a solo project. When you need state your |
| 125 | team and CI can share, swap in `Cloudflare.state()` or `AWS.state()` |
| 126 | — see [State Store](/state-store). |
| 127 | ::: |
| 128 | |
| 129 | ## Add a Server |
| 130 | |
| 131 | Resources represent cloud infrastructure. Each resource is |
| 132 | `yield*`-ed inside the Stack's Effect generator. |
| 133 | |
| 134 | Let's declare a Server — Hetzner's virtual machine — and observe the |
| 135 | type error: |
| 136 | |
| 137 | ```diff lang="typescript" |
| 138 | // alchemy.run.ts |
| 139 | import * as Alchemy from "alchemy"; |
| 140 | +import * as Hetzner from "alchemy/Hetzner"; |
| 141 | import * as Effect from "effect/Effect"; |
| 142 | import * as Layer from "effect/Layer"; |
| 143 | |
| 144 | export default Alchemy.Stack( |
| 145 | "MyApp", |
| 146 | { |
| 147 | providers: Layer.empty, |
| 148 | // @error: ts(2322) Type 'Layer<never, never, never>' is not assignable to type 'Layer<NoInfer<Providers>, never, StackServices>'. |
| 149 | // @error: Type 'Providers' is not assignable to type 'never'. |
| 150 | state: Alchemy.localState(), |
| 151 | }, |
| 152 | Effect.gen(function* () { |
| 153 | + const server = yield* Hetzner.Server("box", { |
| 154 | + serverType: "cx22", |
| 155 | + image: "ubuntu-24.04", |
| 156 | + location: "nbg1", |
| 157 | + }); |
| 158 | }), |
| 159 | ); |
| 160 | ``` |
| 161 | |
| 162 | The props map directly onto Hetzner's console: `serverType` is the |
| 163 | size (`cx22` = 2 vCPU / 4 GB), `image` is the OS, and `location` is |
| 164 | the datacenter (`nbg1` is Nuremberg; `fsn1`, `hel1`, `ash`, `hil`, |
| 165 | and `sin` also exist). |
| 166 | |
| 167 | TypeScript is telling us that `Layer.empty` doesn't provide |
| 168 | `Hetzner.Providers` — the layer required by `Server`. |
| 169 | |
| 170 | ## Fix the Providers |
| 171 | |
| 172 | Replace `Layer.empty` with `Hetzner.providers()` to resolve the type |
| 173 | error: |
| 174 | |
| 175 | ```diff lang="typescript" |
| 176 | // alchemy.run.ts |
| 177 | import * as Alchemy from "alchemy"; |
| 178 | import * as Hetzner from "alchemy/Hetzner"; |
| 179 | import * as Effect from "effect/Effect"; |
| 180 | -import * as Layer from "effect/Layer"; |
| 181 | |
| 182 | export default Alchemy.Stack( |
| 183 | "MyApp", |
| 184 | { |
| 185 | + providers: Hetzner.providers(), |
| 186 | state: Alchemy.localState(), |
| 187 | }, |
| 188 | Effect.gen(function* () { |
| 189 | const server = yield* Hetzner.Server("box", { |
| 190 | serverType: "cx22", |
| 191 | image: "ubuntu-24.04", |
| 192 | location: "nbg1", |
| 193 | }); |
| 194 | }), |
| 195 | ); |
| 196 | ``` |
| 197 | |
| 198 | Now the program type-checks. The providers layer tells Alchemy how to |
| 199 | talk to the Hetzner Cloud API, and the type system ensures you never |
| 200 | forget to wire it up. |
| 201 | |
| 202 | ## Add your SSH key |
| 203 | |
| 204 | The server will boot without one, but registering your public key |
| 205 | lets you SSH straight in (and stops Hetzner emailing you a root |
| 206 | password). Declare an `SshKey` and pass it to the server: |
| 207 | |
| 208 | ```diff lang="typescript" |
| 209 | Effect.gen(function* () { |
| 210 | + const key = yield* Hetzner.SshKey("laptop", { |
| 211 | + publicKey: "ssh-ed25519 AAAA... you@laptop", |
| 212 | + }); |
| 213 | |
| 214 | const server = yield* Hetzner.Server("box", { |
| 215 | serverType: "cx22", |
| 216 | image: "ubuntu-24.04", |
| 217 | location: "nbg1", |
| 218 | + sshKeys: [key], |
| 219 | }); |
| 220 | }), |
| 221 | ``` |
| 222 | |
| 223 | Paste your own key from `~/.ssh/id_ed25519.pub` (create one with |
| 224 | `ssh-keygen -t ed25519` if you don't have it). Passing the resolved |
| 225 | `key` to `sshKeys` is your first **resource reference** — Alchemy |
| 226 | sees the dependency and orders the deploy so the key exists before |
| 227 | the server boots. |
| 228 | |
| 229 | ## Return Stack outputs |
| 230 | |
| 231 | Stack outputs let you see important values after a deploy. Return an |
| 232 | object from the generator to expose the server's public addresses: |
| 233 | |
| 234 | ```diff lang="typescript" |
| 235 | const server = yield* Hetzner.Server("box", { |
| 236 | serverType: "cx22", |
| 237 | image: "ubuntu-24.04", |
| 238 | location: "nbg1", |
| 239 | sshKeys: [key], |
| 240 | }); |
| 241 | |
| 242 | + return { |
| 243 | + ipv4: server.ipv4, |
| 244 | + ipv6: server.ipv6, |
| 245 | + }; |
| 246 | }), |
| 247 | ``` |
| 248 | |
| 249 | ## Deploy |
| 250 | |
| 251 | Run `alchemy deploy` to create the key and the server: |
| 252 | |
| 253 | <Tabs syncKey="pkgManager"> |
| 254 | <TabItem label="bun" icon="bun"> |
| 255 | <Code code="bun alchemy deploy" lang="sh" /> |
| 256 | </TabItem> |
| 257 | <TabItem label="npm" icon="npm"> |
| 258 | <Code code="npm run alchemy deploy" lang="sh" /> |
| 259 | </TabItem> |
| 260 | <TabItem label="pnpm" icon="pnpm"> |
| 261 | <Code code="pnpm alchemy deploy" lang="sh" /> |
| 262 | </TabItem> |
| 263 | <TabItem label="yarn" icon="seti:yarn"> |
| 264 | <Code code="yarn alchemy deploy" lang="sh" /> |
| 265 | </TabItem> |
| 266 | </Tabs> |
| 267 | |
| 268 | The first time you deploy, Alchemy prompts for Hetzner credentials — |
| 269 | paste the API token you generated in [Setup](/hetzner/setup). The |
| 270 | token is verified and saved to your **`default`** |
| 271 | [profile](/environments/profiles), so you won't be asked again. |
| 272 | |
| 273 | <Terminal content={`[u]Plan[/u]: [g]2 to create[/g] |
| 274 | |
| 275 | [g]+[/g] [b]laptop[/b] [d](Hetzner.SshKey)[/d] |
| 276 | [g]+[/g] [b]box[/b] [d](Hetzner.Server)[/d] |
| 277 | |
| 278 | Proceed? |
| 279 | ◉ Yes ○ No |
| 280 | [g]✓[/g] [b]laptop[/b] [d](Hetzner.SshKey)[/d] [g]created[/g] |
| 281 | [g]✓[/g] [b]box[/b] [d](Hetzner.Server)[/d] [g]created[/g] |
| 282 | { |
| 283 | [s2]ipv4: "203.0.113.10", |
| 284 | [s2]ipv6: "2001:db8::1", |
| 285 | }`} /> |
| 286 | |
| 287 | Alchemy shows a plan, asks for confirmation, creates both resources, |
| 288 | and prints the stack outputs. A Hetzner server boots in about ten |
| 289 | seconds. |
| 290 | |
| 291 | ## Verify it worked |
| 292 | |
| 293 | The server is listed in your project in the |
| 294 | [Hetzner Cloud Console](https://console.hetzner.com), and you can SSH |
| 295 | straight in: |
| 296 | |
| 297 | ```sh |
| 298 | ssh root@203.0.113.10 |
| 299 | ``` |
| 300 | |
| 301 | Run `alchemy deploy` again. Because nothing changed, both resources |
| 302 | show as no-ops: |
| 303 | |
| 304 | <Terminal content={`[u]Plan[/u]: [d]no changes[/d] |
| 305 | |
| 306 | { |
| 307 | [s2]ipv4: "203.0.113.10", |
| 308 | [s2]ipv6: "2001:db8::1", |
| 309 | }`} /> |
| 310 | |
| 311 | This is the core loop — declare resources in code, deploy, and |
| 312 | Alchemy figures out what changed. |
| 313 | |
| 314 | ## Recap |
| 315 | |
| 316 | You now have: |
| 317 | |
| 318 | - An `alchemy.run.ts` with a Stack, an SSH key, and a Server |
| 319 | - A live VM running Ubuntu in Nuremberg, booted in seconds |
| 320 | - Stack outputs showing its public IPv4 and IPv6 addresses |
| 321 | |
| 322 | In [Part 2](/hetzner/tutorial/part-2), you'll deploy code to this |
| 323 | server — a `Hetzner.Service` that serves HTTP without you ever |
| 324 | writing a systemd unit or a Dockerfile. |