Skip to content

Migrating from v1

Alchemy v1 uses async/await with top-level await for orchestration. Alchemy v2 replaces this with Effect generators for type-safe error handling, composable retries, and declarative resource wiring.

Your existing async fetch handlers do not need to change — you can keep them as-is and still get all the benefits of the new engine.

In v1, you create an app with await alchemy(...) and finalize it at the end:

// v1 — alchemy.run.ts
import alchemy from "alchemy";
import { Worker, R2Bucket } from "alchemy/cloudflare";
const app = await alchemy("my-app", {});
const bucket = await R2Bucket("bucket", {});
const worker = await Worker("worker", {
entrypoint: "./src/worker.ts",
bindings: { BUCKET: bucket },
});
console.log(worker.url);
await app.finalize();

In v2, you export a default Alchemy.Stack and use yield* instead of await:

// v2 — alchemy.run.ts
import * as Alchemy from "alchemy";
import * as Cloudflare from "alchemy/Cloudflare";
import * as Effect from "effect/Effect";
export const Bucket = Cloudflare.R2Bucket("Bucket");
export const Worker = Cloudflare.Worker("Worker", {
main: "./src/worker.ts",
bindings: { Bucket },
});
export default Alchemy.Stack(
"MyApp",
{ providers: Cloudflare.providers() },
Effect.gen(function* () {
const worker = yield* Worker;
return { url: worker.url };
}),
);

Key differences:

  • await alchemy("name") + await app.finalize()Alchemy.Stack("name", { providers }, effect)
  • await R2Bucket("name", {})Cloudflare.R2Bucket("name")
  • await Worker("name", { entrypoint })Cloudflare.Worker("name", { main })
  • entrypoint is now called main
  • Resources are declared at the top level, then yield*-ed inside the Stack
  • No more finalize() — the Stack handles lifecycle automatically

Your existing Worker runtime code does not need to change. The async pattern passes bindings as props on the Worker resource and uses Cloudflare.InferEnv to type the env object:

alchemy.run.ts
export type
type WorkerEnv = Cloudflare.InferEnv<any>
WorkerEnv
=
Cloudflare
.
type Cloudflare.InferEnv = /*unresolved*/ any
InferEnv
<typeof
const Worker: any
Worker
>;
export const
const Worker: any
Worker
=
any
Cloudflare
.
any
Worker
("Worker", {
main: string
main
: "./src/worker.ts",
bindings: {
Bucket: any;
}
bindings
: {
type Bucket: any
Bucket
},
});

Your handler stays the same — just update the type import:

src/worker.ts
import type {
import Env
Env
} from "../alchemy.run.ts";
import type {
import WorkerEnv
WorkerEnv
} from "../alchemy.run.ts";
export default {
async
function fetch(request: Request, env: Env): Promise<Response>
fetch
(
request: Request
request
:
interface Request

The Request interface of the Fetch API represents a resource request.

MDN Reference

Request
,
env: Env
env
:
import Env
Env
) {
any
async
function fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> (+2 overloads)
fetch
(
request: Request
request
:
var Request: {
new (input: RequestInfo | URL, init?: RequestInit): Request;
prototype: Request;
}

The Request interface of the Fetch API represents a resource request.

MDN Reference

Request
,
env: Env
env
:
import WorkerEnv
WorkerEnv
) {
const
const object: any
object
= await
env: Env
env
.
any
BUCKET
.
any
get
("key");
const
const object: any
object
= await
env: Env
env
.
any
Bucket
.
any
get
("key");
return new
var Response: new (body?: BodyInit | null, init?: ResponseInit) => Response

The Response interface of the Fetch API represents the response to a request.

MDN Reference

Response
(
const object: any
object
?.
any
body
?? null);
},
};

Cloudflare.InferEnv derives a fully typed env object from the bindings you declared on the Worker. You get type safety on the binding names and their APIs without using Effect in your runtime code.

The CLI commands are the same:

Terminal window
alchemy deploy
alchemy destroy

Your v1 state is not compatible with v2. On your first deploy, Alchemy creates new resources. You should destroy your v1 stack first, then deploy with v2.

When you’re ready, you can switch to Effect-native Workers. This gives you typed errors, composable retries, and Effect’s HttpServer integration.

Instead of passing bindings as props, you bind resources in the Worker’s Init phase using yield*:

src/worker.ts
import * as
import Cloudflare
Cloudflare
from "alchemy/Cloudflare";
import * as
import Effect
Effect
from "effect/Effect";
import {
const HttpServerRequest: Service<HttpServerRequest, HttpServerRequest>

@since4.0.0

@categorymodels

@since4.0.0

@categorycontext

HttpServerRequest
} from "effect/unstable/http/HttpServerRequest";
import * as
import HttpServerResponse
HttpServerResponse
from "effect/unstable/http/HttpServerResponse";
import {
import Bucket
Bucket
} from "./bucket.ts";
export default {
async
function fetch(request: Request, env: WorkerEnv): Promise<Response>
fetch
(
request: Request<unknown, CfProperties<unknown>>
request
:
interface Request<CfHostMetadata = unknown, Cf = CfProperties<CfHostMetadata>>

The Request interface of the Fetch API represents a resource request.

MDN Reference

Request
,
env: WorkerEnv
env
:
type WorkerEnv = /*unresolved*/ any
WorkerEnv
) {
const
const object: any
object
= await
env: WorkerEnv
env
.
any
Bucket
.
any
get
("key");
return new
var Response: new (body?: BodyInit | null, init?: ResponseInit) => Response

The Response interface of the Fetch API represents the response to a request.

MDN Reference

Response
(
const object: any
object
?.
any
body
?? null);
},
};
export default
import Cloudflare
Cloudflare
.
const Worker: <Cloudflare.WorkerShape, never, Cloudflare.WorkerServices | PlatformServices>(id: string, props: InputProps<Cloudflare.WorkerProps<any, Cloudflare.WorkerAssetsConfig | undefined>, never> | Effect.Effect<InputProps<Cloudflare.WorkerProps<any, Cloudflare.WorkerAssetsConfig | undefined>, never>, never, never>, impl: Effect.Effect<Cloudflare.WorkerShape, never, Cloudflare.WorkerServices | PlatformServices>) => Effect.Effect<...> (+3 overloads)
Worker
("Worker",
{
main: Input<string>
main
: import.

The type of import.meta.

If you need to declare that a given property exists on import.meta, this type may be augmented via interface merging.

meta
.
ImportMeta.path: string

Absolute path to the source file

path
},
import Effect
Effect
.
const gen: <Effect.Effect<Cloudflare.R2BucketClient, never, Cloudflare.R2BucketBinding>, {
fetch: Effect.Effect<HttpServerResponse.HttpServerResponse, Cloudflare.R2Error, HttpServerRequest | Cloudflare.WorkerEnvironment>;
}>(f: () => Generator<Effect.Effect<Cloudflare.R2BucketClient, never, Cloudflare.R2BucketBinding>, {
fetch: Effect.Effect<HttpServerResponse.HttpServerResponse, Cloudflare.R2Error, HttpServerRequest | Cloudflare.WorkerEnvironment>;
}, never>) => Effect.Effect<...> (+1 overload)

Provides a way to write effectful code using generator functions, simplifying control flow and error handling.

When to Use

gen allows you to write code that looks and behaves like synchronous code, but it can handle asynchronous tasks, errors, and complex control flow (like loops and conditions). It helps make asynchronous code more readable and easier to manage.

The generator functions work similarly to async/await but with more explicit control over the execution of effects. You can yield* values from effects and return the final result at the end.

@example

import { Data, Effect } from "effect"
class DiscountRateError extends Data.TaggedError("DiscountRateError")<{}> {}
const addServiceCharge = (amount: number) => amount + 1
const applyDiscount = (
total: number,
discountRate: number
): Effect.Effect<number, DiscountRateError> =>
discountRate === 0
? Effect.fail(new DiscountRateError())
: Effect.succeed(total - (total * discountRate) / 100)
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
const fetchDiscountRate = Effect.promise(() => Promise.resolve(5))
export const program = Effect.gen(function*() {
const transactionAmount = yield* fetchTransactionAmount
const discountRate = yield* fetchDiscountRate
const discountedAmount = yield* applyDiscount(
transactionAmount,
discountRate
)
const finalAmount = addServiceCharge(discountedAmount)
return `Final amount to charge: ${finalAmount}`
})

@since2.0.0

@categoryCreating Effects

gen
(function* () {
const
const bucket: Cloudflare.R2BucketClient
bucket
= yield*
import Cloudflare
Cloudflare
.
const R2Bucket: ResourceClassWithMethods<Cloudflare.R2Bucket, {
readonly bind: (<Req = never>(args_0: Cloudflare.R2Bucket | Effect.Effect<Cloudflare.R2Bucket, never, Req>) => Effect.Effect<Cloudflare.R2BucketClient, never, Cloudflare.R2BucketBinding | Req>) & ((bucket: Cloudflare.R2Bucket) => Effect.Effect<any, any, any>);
}>

A Cloudflare R2 object storage bucket with S3-compatible API.

R2 provides zero-egress-fee object storage. Create a bucket as a resource, then bind it to a Worker to read and write objects at runtime.

@sectionCreating a Bucket

@example

Basic R2 bucket

const bucket = yield* Cloudflare.R2Bucket("MyBucket");

@example

Bucket with location hint

const bucket = yield* Cloudflare.R2Bucket("MyBucket", {
locationHint: "wnam",
});

@sectionBinding to a Worker

@example

Reading and writing objects

const bucket = yield* Cloudflare.R2Bucket.bind(MyBucket);
// Write an object
yield* bucket.put("hello.txt", "Hello, World!");
// Read an object
const object = yield* bucket.get("hello.txt");
if (object) {
const text = yield* object.text();
}

@example

Streaming upload with content length

const bucket = yield* Cloudflare.R2Bucket.bind(MyBucket);
yield* bucket.put("upload.bin", request.stream, {
contentLength: Number(request.headers["content-length"] ?? 0),
});

R2Bucket
.
bind: <never>(args_0: Cloudflare.R2Bucket | Effect.Effect<Cloudflare.R2Bucket, never, never>) => Effect.Effect<Cloudflare.R2BucketClient, never, Cloudflare.R2BucketBinding> (+1 overload)
bind
(
import Bucket
Bucket
);
return {
fetch: Effect.Effect<HttpServerResponse.HttpServerResponse, Cloudflare.R2Error, HttpServerRequest | Cloudflare.WorkerEnvironment>
fetch
:
import Effect
Effect
.
const gen: <Service<HttpServerRequest, HttpServerRequest> | Effect.Effect<Cloudflare.R2ObjectBody | null, Cloudflare.R2Error, Cloudflare.WorkerEnvironment> | Effect.Effect<string, Cloudflare.R2Error, never>, HttpServerResponse.HttpServerResponse>(f: () => Generator<Service<HttpServerRequest, HttpServerRequest> | Effect.Effect<Cloudflare.R2ObjectBody | null, Cloudflare.R2Error, Cloudflare.WorkerEnvironment> | Effect.Effect<...>, HttpServerResponse.HttpServerResponse, never>) => Effect.Effect<...> (+1 overload)

Provides a way to write effectful code using generator functions, simplifying control flow and error handling.

When to Use

gen allows you to write code that looks and behaves like synchronous code, but it can handle asynchronous tasks, errors, and complex control flow (like loops and conditions). It helps make asynchronous code more readable and easier to manage.

The generator functions work similarly to async/await but with more explicit control over the execution of effects. You can yield* values from effects and return the final result at the end.

@example

import { Data, Effect } from "effect"
class DiscountRateError extends Data.TaggedError("DiscountRateError")<{}> {}
const addServiceCharge = (amount: number) => amount + 1
const applyDiscount = (
total: number,
discountRate: number
): Effect.Effect<number, DiscountRateError> =>
discountRate === 0
? Effect.fail(new DiscountRateError())
: Effect.succeed(total - (total * discountRate) / 100)
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
const fetchDiscountRate = Effect.promise(() => Promise.resolve(5))
export const program = Effect.gen(function*() {
const transactionAmount = yield* fetchTransactionAmount
const discountRate = yield* fetchDiscountRate
const discountedAmount = yield* applyDiscount(
transactionAmount,
discountRate
)
const finalAmount = addServiceCharge(discountedAmount)
return `Final amount to charge: ${finalAmount}`
})

@since2.0.0

@categoryCreating Effects

gen
(function* () {
const
const request: HttpServerRequest
request
= yield*
const HttpServerRequest: Service<HttpServerRequest, HttpServerRequest>

@since4.0.0

@categorymodels

@since4.0.0

@categorycontext

HttpServerRequest
;
const
const key: string
key
=
const request: HttpServerRequest
request
.
HttpServerRequest.url: string
url
.
String.split(separator: string | RegExp, limit?: number): string[] (+1 overload)

Split a string into substrings using the specified separator and return them as an array.

@paramseparator A string that identifies character or characters to use in separating the string. If omitted, a single-element array containing the entire string is returned.

@paramlimit A value used to limit the number of elements returned in the array.

split
("/").
Array<string>.pop(): string | undefined

Removes the last element from an array and returns it. If the array is empty, undefined is returned and the array is not modified.

pop
()!;
const
const object: Cloudflare.R2ObjectBody | null
object
= yield*
const bucket: Cloudflare.R2BucketClient
bucket
.
R2BucketClient.get(key: string, options?: Cloudflare.R2GetOptions): Effect.Effect<Cloudflare.R2ObjectBody | null, Cloudflare.R2Error, Cloudflare.WorkerEnvironment> (+1 overload)
get
(
const key: string
key
);
return
const object: Cloudflare.R2ObjectBody | null
object
?
import HttpServerResponse
HttpServerResponse
.
const text: (body: string, options?: HttpServerResponse.Options.WithContentType) => HttpServerResponse.HttpServerResponse

@since4.0.0

@categoryconstructors

text
(yield*
const object: Cloudflare.R2ObjectBody
object
.
R2ObjectBody.text(): Effect.Effect<string, Cloudflare.R2Error>
text
())
:
import HttpServerResponse
HttpServerResponse
.
const text: (body: string, options?: HttpServerResponse.Options.WithContentType) => HttpServerResponse.HttpServerResponse

@since4.0.0

@categoryconstructors

text
("Not found", {
status?: number | undefined
status
: 404 });
}),
};
}),
);

The Worker resource declaration moves from alchemy.run.ts into the Worker file itself (using import.meta.path as the main), and the Stack just yield*-s the imported Worker:

alchemy.run.ts
import * as
import Alchemy
Alchemy
from "alchemy";
import * as
import Cloudflare
Cloudflare
from "alchemy/Cloudflare";
import * as
import Effect
Effect
from "effect/Effect";
import
import Worker
var Worker: Effect.Effect<Cloudflare.Worker<{
readonly Bucket: any;
}>, never, Cloudflare.Providers>
Worker
from "./src/worker.ts";
import {
import Bucket
Bucket
} from "./src/bucket.ts";
export type
type WorkerEnv = {
readonly Bucket: any;
}
WorkerEnv
=
import Cloudflare
Cloudflare
.
type InferEnv<W> = W extends Cloudflare.Worker<infer Bindings extends Cloudflare.WorkerBindings> | Effect.Effect<Cloudflare.Worker<infer Bindings extends Cloudflare.WorkerBindings>, any, any> ? { [K in keyof Bindings]: GetBindingType<UnwrapEffect<Bindings[K]>>; } : never
InferEnv
<typeof
import Worker
var Worker: Effect.Effect<Cloudflare.Worker<{
readonly Bucket: any;
}>, never, Cloudflare.Providers>
Worker
>;
export const
const Worker: Effect.Effect<Cloudflare.Worker<{
readonly Bucket: any;
}>, never, Cloudflare.Providers>
Worker
=
import Cloudflare
Cloudflare
.
const Worker: <{
readonly Bucket: any;
}, undefined, never>(id: string, props: Alchemy.InputProps<Cloudflare.WorkerProps<{
readonly Bucket: any;
}, undefined>, never> | Effect.Effect<Alchemy.InputProps<Cloudflare.WorkerProps<{
readonly Bucket: any;
}, undefined>, never>, never, never>) => Effect.Effect<Cloudflare.Worker<{
readonly Bucket: any;
}>, never, Cloudflare.Providers> (+3 overloads)
Worker
("Worker", {
main: Alchemy.Input<string>
main
: "./src/worker.ts",
bindings?: Alchemy.Input<{
readonly Bucket: any;
} | undefined>
bindings
: {
type Bucket: any
Bucket
},
});
export default
import Alchemy
Alchemy
.
Stack<{
url: Alchemy.Output<string | undefined, never>;
}, unknown>(stackName: string, options: Alchemy.StackProps<unknown>, eff: Effect.Effect<{
url: Alchemy.Output<string | undefined, never>;
}, never, unknown>): Effect.Effect<Alchemy.CompiledStack<{
url: Alchemy.Output<string | undefined, never>;
}, any>, never, never> (+2 overloads)
export Stack
Stack
(
"MyApp",
{
StackProps<unknown>.providers: Layer<unknown, never, Alchemy.StackServices>
providers
:
import Cloudflare
Cloudflare
.
const providers: () => Layer<Cloudflare.Providers | Alchemy.Provider<Command> | Alchemy.Provider<Alchemy.Random> | Cloudflare.Credentials | Cloudflare.CloudflareEnvironment | Access | Retry, never, any>

Cloudflare providers, bindings, and credentials for Worker-based stacks.

providers
() },
import Effect
Effect
.
const gen: <any, {
url: Alchemy.Output<string | undefined, never>;
}>(f: () => Generator<any, {
url: Alchemy.Output<string | undefined, never>;
}, never>) => Effect.Effect<{
url: Alchemy.Output<string | undefined, never>;
}, unknown, unknown> (+1 overload)

Provides a way to write effectful code using generator functions, simplifying control flow and error handling.

When to Use

gen allows you to write code that looks and behaves like synchronous code, but it can handle asynchronous tasks, errors, and complex control flow (like loops and conditions). It helps make asynchronous code more readable and easier to manage.

The generator functions work similarly to async/await but with more explicit control over the execution of effects. You can yield* values from effects and return the final result at the end.

@example

import { Data, Effect } from "effect"
class DiscountRateError extends Data.TaggedError("DiscountRateError")<{}> {}
const addServiceCharge = (amount: number) => amount + 1
const applyDiscount = (
total: number,
discountRate: number
): Effect.Effect<number, DiscountRateError> =>
discountRate === 0
? Effect.fail(new DiscountRateError())
: Effect.succeed(total - (total * discountRate) / 100)
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
const fetchDiscountRate = Effect.promise(() => Promise.resolve(5))
export const program = Effect.gen(function*() {
const transactionAmount = yield* fetchTransactionAmount
const discountRate = yield* fetchDiscountRate
const discountedAmount = yield* applyDiscount(
transactionAmount,
discountRate
)
const finalAmount = addServiceCharge(discountedAmount)
return `Final amount to charge: ${finalAmount}`
})

@since2.0.0

@categoryCreating Effects

gen
(function* () {
const
const bucket: any
bucket
= yield*
import Bucket
Bucket
;
const
const worker: Cloudflare.Worker<{
readonly Bucket: any;
}>
worker
= yield*
import Worker
var Worker: Effect.Effect<Cloudflare.Worker<{
readonly Bucket: any;
}>, never, Cloudflare.Providers>
Worker
;
return {
url: Alchemy.Output<string | undefined, never>
url
:
const worker: Cloudflare.Worker<{
readonly Bucket: any;
}>
worker
.
url: Alchemy.Output<string | undefined, never>
url
};
}),
);
v1 (async)v2 (async style)v2 (Effect style)
Stackawait alchemy("name")Alchemy.Stack("name", ...)Alchemy.Stack("name", ...)
Resourcesawait R2Bucket(...)Cloudflare.R2Bucket(...)Cloudflare.R2Bucket(...)
Worker entryentrypointmainmain: import.meta.path
Bindingsbindings: { KEY: resource }bindings: { Key: resource }yield* Resource.bind(ref)
Runtime codeasync fetch(req, env)async fetch(req, env)Effect.gen(function* () { ... })
Lifecycleawait app.finalize()automaticautomatic
Type safetyruntime errorstyped env via InferEnvfull Effect type system