Skip to content
betterstack-alchemyUnofficial

Setup

Everything you need before deploying to Better Stack: the packages, an API token, and a profile that holds it.

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

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:

Terminal window
git clone https://github.com/dominikvit-com/betterstack-alchemy
cd betterstack-alchemy && bun install && bun run build && npm pack # writes betterstack-alchemy-0.0.0.tgz
cd ../my-app
Terminal window
bun add ../betterstack-alchemy/betterstack-alchemy-0.0.0.tgz alchemy@2.0.0-beta.87 effect @effect/platform-node

Once it is published, that becomes one line:

Terminal window
bun add 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

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.

Store the token in an Alchemy profile:

Terminal window
bun alchemy profile edit --add BetterStack

Alchemy 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:

Terminal window
bun alchemy profile edit --add BetterStack --method stored \
--set apiToken=env:BETTERSTACK_API_TOKEN --set teamId=t123 --set availabilityZone=us-west-2a

Inspect what’s stored (secrets are redacted):

Terminal window
bun alchemy profile show

Give your Stack BetterStack.providers(). It registers every Better Stack resource and reads the credentials from the profile on the first API call:

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* () {
const site = yield* BetterStack.Incidents.Monitor("Site", {
url: "https://example.com",
monitorType: "status",
});
return { monitorId: site.id };
}),
);

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.

  • An unchanged deploy doesn’t look at Better Stack. alchemy deploy plans 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 drift finds out-of-band changes. It reads each resource and compares it with the state; when it finds drift it offers to repair it, and alchemy 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 code 2, so a scheduled job can tell drift from a clean stage. alchemy deploy --detect-drift runs 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 --force does 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.

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() or AWS.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.