The best way to control Claude Code, Codex and more from any computer or phone
| 1 | # @alchemy.run/cloudflare |
| 2 | |
| 3 | Cloudflare for [Alchemy](https://alchemy.run). Declare Workers, Durable Objects, R2, KV, D1, Queues, Workflows, and Containers in the same TypeScript program as the code that uses them. |
| 4 | |
| 5 | ```sh |
| 6 | npm install alchemy @alchemy.run/cloudflare effect |
| 7 | ``` |
| 8 | |
| 9 | A Worker binds a bucket and gets a typed client for it: |
| 10 | |
| 11 | ```typescript |
| 12 | // src/worker.ts |
| 13 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 14 | import * as Effect from "effect/Effect"; |
| 15 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 16 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 17 | |
| 18 | export const Bucket = Cloudflare.R2.Bucket("Bucket"); |
| 19 | |
| 20 | export default Cloudflare.Worker( |
| 21 | "Worker", |
| 22 | { main: import.meta.url }, |
| 23 | Effect.gen(function* () { |
| 24 | const bucket = yield* Cloudflare.R2.ReadWriteBucket(Bucket); |
| 25 | |
| 26 | return { |
| 27 | fetch: Effect.gen(function* () { |
| 28 | const request = yield* HttpServerRequest; |
| 29 | const key = request.url.split("/").pop()!; |
| 30 | if (request.method === "PUT") { |
| 31 | yield* bucket.put(key, yield* request.text); |
| 32 | return HttpServerResponse.empty({ status: 201 }); |
| 33 | } |
| 34 | const object = yield* bucket.get(key); |
| 35 | if (object === null) { |
| 36 | return HttpServerResponse.text("Not found", { status: 404 }); |
| 37 | } |
| 38 | return HttpServerResponse.text(yield* object.text()); |
| 39 | }).pipe(Effect.orDie), |
| 40 | }; |
| 41 | }).pipe(Effect.provide(Cloudflare.R2.ReadWriteBucketBinding)), |
| 42 | ); |
| 43 | ``` |
| 44 | |
| 45 | The Stack deploys it: |
| 46 | |
| 47 | ```typescript |
| 48 | // alchemy.run.ts |
| 49 | import * as Alchemy from "alchemy"; |
| 50 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 51 | import * as Effect from "effect/Effect"; |
| 52 | import Worker from "./src/worker.ts"; |
| 53 | |
| 54 | export default Alchemy.Stack( |
| 55 | "MyApp", |
| 56 | { |
| 57 | providers: Cloudflare.providers(), |
| 58 | state: Cloudflare.state(), |
| 59 | }, |
| 60 | Effect.gen(function* () { |
| 61 | const worker = yield* Worker; |
| 62 | return { url: worker.url }; |
| 63 | }), |
| 64 | ); |
| 65 | ``` |
| 66 | |
| 67 | `alchemy deploy` creates the bucket and the Worker and wires the binding between them. `alchemy dev` runs the same program locally. |
| 68 | |
| 69 | ## Learn more |
| 70 | |
| 71 | - [Cloudflare on alchemy.run](https://alchemy.run/cloudflare) |
| 72 | - [Getting started](https://alchemy.run/getting-started) |
| 73 | - [Tutorial](https://alchemy.run/cloudflare/tutorial/part-1) |
| 74 | - [API reference](https://alchemy.run/providers) |