Setup
Everything you need before deploying to Better Stack: the packages, an API token, and a profile that holds it.
Prerequisites
Section titled “Prerequisites”- Node.js 22.22.3+ or 24.11.1+, or Bun
- A Better Stack account
Create a project directory
Section titled “Create a project directory”mkdir my-app && cd my-app && bun init -ymkdir my-app && cd my-app && pnpm initmkdir my-app && cd my-app && npm init -yInstall
Section titled “Install”betterstack-alchemy is not on npm yet. Until the first release, install it from a tarball built from the repository, then add Alchemy and Effect next to it:
git clone https://github.com/dominikvit-com/betterstack-alchemycd betterstack-alchemy && bun install && bun run build && npm pack # writes betterstack-alchemy-0.0.0.tgzcd ../my-appbun add ../betterstack-alchemy/betterstack-alchemy-0.0.0.tgz alchemy@2.0.0-beta.87 effect @effect/platform-nodepnpm add ../betterstack-alchemy/betterstack-alchemy-0.0.0.tgz alchemy@2.0.0-beta.87 effect @effect/platform-nodenpm install ../betterstack-alchemy/betterstack-alchemy-0.0.0.tgz alchemy@2.0.0-beta.87 effect @effect/platform-nodeOnce it is published, that becomes one line:
bun add betterstack-alchemy alchemy@2.0.0-beta.87 effect @effect/platform-nodepnpm add betterstack-alchemy alchemy@2.0.0-beta.87 effect @effect/platform-nodenpm install betterstack-alchemy alchemy@2.0.0-beta.87 effect @effect/platform-node@effect/platform-node is what a Cloud.Service runs on, and what the alchemy CLI needs to load the provider. Alchemy v2 itself is in beta: these docs are written against alchemy@2.0.0-beta.87. The tutorial installs the same way.
Add these only when you use them:
| You use | Also install |
|---|---|
migrations on Cloud.Postgres |
@alchemy.run/sql@2.0.0-beta.87 pg |
migrations on Cloud.MySQL |
@alchemy.run/sql@2.0.0-beta.87 mysql2 |
Cloud.Website.<Framework> (including StaticSite) |
@alchemy.run/frontend-frameworks@2.0.0-beta.87 and your framework |
Create an API token
Section titled “Create an API token”In Better Stack, open API tokens:
- A global API token (Global tokens) works with every product. Use it when one Stack declares resources from several products, like a server and the monitor that checks it.
- A team API token (Team-based tokens) works only with the product it was created for: Incidents, Telemetry or Cloud. Use it for a Stack that only touches that product.
See Teams, tokens & zones for which token reaches which resources.
Connect Alchemy to Better Stack
Section titled “Connect Alchemy to Better Stack”Store the token in an Alchemy profile:
bun alchemy profile edit --add BetterStackpnpm alchemy profile edit --add BetterStacknpx alchemy profile edit --add BetterStackAlchemy asks for:
| Field | What it is |
|---|---|
apiToken |
the API token (stored as a secret) |
teamId |
the team to act in, such as t123. Needed with a global token that reaches several teams |
availabilityZone |
the default Better Stack Cloud zone, such as us-west-2a. Only Cloud resources use it |
incidentsUrl, telemetryUrl, errorsUrl, accountUrl, cloudUrl |
optional API base URL overrides, for a test server |
To set it up without prompts (scripts, agents), read the token from the environment:
bun alchemy profile edit --add BetterStack --method stored \--set apiToken=env:BETTERSTACK_API_TOKEN --set teamId=t123 --set availabilityZone=us-west-2apnpm alchemy profile edit --add BetterStack --method stored \--set apiToken=env:BETTERSTACK_API_TOKEN --set teamId=t123 --set availabilityZone=us-west-2anpx alchemy profile edit --add BetterStack --method stored \--set apiToken=env:BETTERSTACK_API_TOKEN --set teamId=t123 --set availabilityZone=us-west-2aInspect what’s stored (secrets are redacted):
bun alchemy profile showpnpm alchemy profile shownpx alchemy profile showProvide the providers
Section titled “Provide the providers”Give your Stack BetterStack.providers(). It registers every Better Stack resource and reads the credentials from the profile on the first API call:
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* () { const site = yield* BetterStack.Incidents.Monitor("Site", { url: "https://example.com", monitorType: "status", }); return { monitorId: site.id }; }),);Team and zone per resource
Section titled “Team and zone per resource”The profile’s teamId and availabilityZone are defaults. Every team-scoped resource also takes teamId, and every Cloud resource takes availabilityZone; changing either replaces the resource. A Cloud resource with no zone in its props or the profile fails the deploy with MissingAvailabilityZone.
How deploys behave
Section titled “How deploys behave”- An unchanged deploy doesn’t look at Better Stack.
alchemy deployplans from the stored state: a resource whose props didn’t change is a no-op, and its remote object isn’t read. A monitor paused or a firewall rule deleted in the dashboard stays that way. alchemy driftfinds out-of-band changes. It reads each resource and compares it with the state; when it finds drift it offers to repair it, andalchemy drift --repair(or--yes) repairs without asking: a drifted resource is reset to its last-deployed props, a deleted one is recreated. Without a repair it exits with code2, so a scheduled job can tell drift from a clean stage.alchemy deploy --detect-driftruns the same check before deploying and offers to repair first. Resources whose provider can’t read them are skipped.- Changing a prop converges the resource. Change any prop and the next deploy reads that resource’s live object, recreates it if it was deleted, and sends every prop you set that differs from it, so manual edits to those props are undone too.
alchemy deploy --forcedoes the same for resources whose props didn’t change.
With CI=true, Alchemy skips the profile and reads the environment:
| Variable | Required | What it is |
|---|---|---|
BETTERSTACK_API_TOKEN |
yes | a global API token, or a team-scoped Incidents / Telemetry / Cloud token |
BETTERSTACK_TEAM_ID |
no | the team to act in, such as t123 |
BETTERSTACK_AVAILABILITY_ZONE |
no | the default Cloud zone, such as us-west-2a |
See CI.
State storage
Section titled “State storage”Better Stack has no state store of its own, so pick one:
Alchemy.localState(): state on disk under.alchemy/next to your code. Right for trying things out.- A cloud state store: if you also use Cloudflare or AWS, pass
Cloudflare.state()orAWS.state()so state is shared with your team and CI. See State Store.
State holds secrets. Every Redacted value a Stack deploys is kept in its state inside Alchemy’s
__redacted__ marker, and with Alchemy.localState() that marker is plain text in the files
under .alchemy/state: each Cloud server’s deploy private key (it logs in as root), every
secret in a Service’s env, source tokens, database passwords. The plan output and attributes
show (redacted); the state file does not. Protect .alchemy/state like the keys it holds: add
.alchemy/ to your .gitignore, don’t copy it around or attach it to an issue, and for anything
you keep prefer a shared store. Cloudflare.state() (in @alchemy.run/cloudflare@2.0.0-beta.87)
keeps an encryption key for the state in your account’s Secrets Store; for the other stores’
options, see State Store.
Next steps
Section titled “Next steps”- Tutorial part 1: deploy your first server.
- Better Stack overview: the map of the docs.