Skip to content
betterstack-alchemyUnofficial

Part 1: Your First Server

Over four parts you build one small app on Better Stack: a server, an HTTP Service on it, a volume for its files, and the monitoring that watches it. In this part you create a Stack with one server behind a firewall, deploy it, and SSH in.

  • Node.js 24.11.1+ (or 22.22.3+) or Bun
  • A Better Stack account and an API token that reaches Better Stack Cloud — see Setup. Part 4 also creates a monitor, a status page, a heartbeat, a telemetry source and an error-tracking application, so a global API token is the simplest choice; a Cloud-only team token is refused by those APIs.

Start with an empty directory and a package.json:

Terminal window
mkdir my-app && cd my-app && bun init -y

Install Alchemy, the Better Stack provider, Effect, and Effect’s Node platform (a Service’s bootstrap runs on Node). betterstack-alchemy is not on npm yet: until it is, install it from a tarball built from the repository, as described in Setup → Install. After the first release, the install is this line:

Terminal window
bun add betterstack-alchemy alchemy@2.0.0-beta.87 effect @effect/platform-node

Every Alchemy program is a Stack: the resources it manages, the providers that create them, and a state store that remembers what was deployed. Create alchemy.run.ts:

alchemy.run.ts
import * as Alchemy from "alchemy";
import * as BetterStack from "betterstack-alchemy";
import * as Effect from "effect/Effect";
export default Alchemy.Stack(
"MyApp",
{ providers: BetterStack.providers(), state: Alchemy.localState() },
Effect.gen(function* () {
// resources go here
}),
);

BetterStack.providers() is the layer that talks to Better Stack’s APIs. It reads your token from your Alchemy profile, or from BETTERSTACK_API_TOKEN in CI. Alchemy.localState() keeps the Stack’s state on disk in .alchemy/ next to your code. See State stores for state your team and CI can share.

Declare a server inside the Stack’s generator:

alchemy.run.ts
"MyApp",
{ providers: BetterStack.providers(), state: Alchemy.localState() },
Effect.gen(function* () {
const server = yield* BetterStack.Cloud.Server("Box", {
serverType: "AM1",
image: "ubuntu-26-04",
});
// resources go here
}),
);

serverType is the size: AM1 is the smallest available in us-west-2a as of 2026-12-01 (Cloud.findServerType picks one by vCPUs and memory). image is the operating system, here Ubuntu 26.04. Changing image later replaces the server, while changing serverType rescales it in place. See Servers.

Register your public key as an SSH key and give it to the server:

alchemy.run.ts
"MyApp",
{ providers: BetterStack.providers(), state: Alchemy.localState() },
Effect.gen(function* () {
const key = yield* BetterStack.Cloud.SshKey("Laptop", {
publicKey:
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEF0uIiLF54lpavoiG7O95cChLJ80vbi0Nh08Kf3+G/a you@laptop",
});
const server = yield* BetterStack.Cloud.Server("Box", {
serverType: "AM1",
image: "ubuntu-26-04",
sshKeys: [key.slug],
});
}),
);

Paste your own key from ~/.ssh/id_ed25519.pub. Passing key.slug to sshKeys is a resource reference: Alchemy creates the key before the server that uses it.

You don’t need a key for Alchemy itself. For every server it creates, Alchemy generates its own deploy key, which it uses to deploy code in Part 2. Your key is for your own ssh.

A Better Stack Cloud firewall only allows traffic: whatever no rule matches is dropped. Declare one that lets SSH in and everything out, and attach it to the server:

alchemy.run.ts
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEF0uIiLF54lpavoiG7O95cChLJ80vbi0Nh08Kf3+G/a you@laptop",
});
const wall = yield* BetterStack.Cloud.Firewall("Wall", {
rules: [
{ direction: "inbound", protocol: "tcp", portFrom: 22, ipAddresses: ["0.0.0.0/0", "::/0"] },
{ direction: "outbound", protocol: "any", portFrom: 0, ipAddresses: ["0.0.0.0/0", "::/0"] },
],
});
const server = yield* BetterStack.Cloud.Server("Box", {
serverType: "AM1",
image: "ubuntu-26-04",
sshKeys: [key.slug],
firewallIds: [wall.id],
});
}),
);

The rules list is the firewall’s whole rule set, so the outbound rule is listed too. Keep port 22 open: Alchemy deploys over SSH. See Networking.

Return the server’s public addresses from the generator, so the deploy prints them:

alchemy.run.ts
sshKeys: [key.slug],
firewallIds: [wall.id],
});
return {
ipv4: server.ipv4,
ipv6: server.ipv6,
};
}),
);
Terminal window
bun alchemy deploy

Alchemy shows a plan with three resources to create, asks you to confirm, then creates the key, the firewall and the server. The deploy returns once the server is running and an SSH login with Alchemy’s deploy key has succeeded, then prints ipv4 and ipv6.

If this is the first server of your Better Stack account, the deploy can fail with PaymentRequired instead, and the error’s message carries a payment link. Paying through it creates the server outside your state; read The first server of an account before you deploy again.

The deploy key’s private half is in your state, in clear text with Alchemy.localState(): keep .alchemy/ out of version control (State storage).

Log in with your own key:

Terminal window
ssh root@<ipv4>

Run alchemy deploy again. Nothing changed, so the plan is empty and no API call writes anything. That’s the loop: declare resources in code, deploy, and Alchemy works out what changed. An unchanged deploy doesn’t read the remote objects either, so a change made in the Better Stack console stays until alchemy drift finds it; see How deploys behave.

You now have:

  • An alchemy.run.ts with a Stack, an SSH key, a firewall and a server
  • An AM1 server running Ubuntu, reachable over SSH and closed to everything else
  • Stack outputs with its public addresses

Next, Part 2 deploys code to this server: a Cloud.Service that serves HTTP, without a unit file or a Dockerfile.