The best way to control Claude Code, Codex and more from any computer or phone
| 1 | # @alchemy.run/stripe |
| 2 | |
| 3 | Stripe for [Alchemy](https://alchemy.run). Declare products, prices, webhooks, and Connect accounts next to the code that sells them, and call Stripe from a Worker with a token scoped to exactly the calls it makes. |
| 4 | |
| 5 | ```sh |
| 6 | npm install alchemy @alchemy.run/stripe @alchemy.run/cloudflare effect |
| 7 | ``` |
| 8 | |
| 9 | A Worker that sells a monthly plan through Checkout and reacts to new subscriptions: |
| 10 | |
| 11 | ```typescript |
| 12 | // src/api.ts |
| 13 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 14 | import * as Stripe from "@alchemy.run/stripe"; |
| 15 | import * as Effect from "effect/Effect"; |
| 16 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 17 | |
| 18 | export default class Api extends Cloudflare.Worker<Api>()( |
| 19 | "Api", |
| 20 | { main: import.meta.url }, |
| 21 | Effect.gen(function* () { |
| 22 | // The catalog deploys with the Worker. |
| 23 | const product = yield* Stripe.Product("Pro", { name: "Pro" }); |
| 24 | const price = yield* Stripe.Price("ProMonthly", { |
| 25 | product, |
| 26 | currency: "usd", |
| 27 | unitAmount: 2000, |
| 28 | recurring: { interval: "month" }, |
| 29 | }); |
| 30 | const priceId = yield* price.id; |
| 31 | |
| 32 | // Each call adds its permission to the Worker's restricted key. |
| 33 | const createCheckout = yield* Stripe.CreateCheckoutSession(); |
| 34 | |
| 35 | // Stripe → app: the webhook endpoint is created for you. |
| 36 | yield* Stripe.consumeEvents( |
| 37 | "Events", |
| 38 | { events: [Stripe.CustomerSubscriptionCreated] }, |
| 39 | Effect.fn(function* (event) { |
| 40 | yield* Effect.log(`${event.object.customer} subscribed`); |
| 41 | }), |
| 42 | ); |
| 43 | |
| 44 | return { |
| 45 | fetch: Effect.gen(function* () { |
| 46 | const session = yield* createCheckout({ |
| 47 | mode: "subscription", |
| 48 | line_items: [{ price: yield* priceId, quantity: 1 }], |
| 49 | success_url: "https://example.com/welcome", |
| 50 | cancel_url: "https://example.com/pricing", |
| 51 | }); |
| 52 | return HttpServerResponse.redirect(session.url!); |
| 53 | }).pipe(Effect.orDie), |
| 54 | }; |
| 55 | }).pipe(Effect.provide([Stripe.CreateCheckoutSessionHttp, Stripe.ConsumeEventsLive])), |
| 56 | ) {} |
| 57 | ``` |
| 58 | |
| 59 | ```typescript |
| 60 | // alchemy.run.ts |
| 61 | import * as Alchemy from "alchemy"; |
| 62 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 63 | import * as Stripe from "@alchemy.run/stripe"; |
| 64 | import * as Effect from "effect/Effect"; |
| 65 | import * as Layer from "effect/Layer"; |
| 66 | import Api from "./src/api.ts"; |
| 67 | |
| 68 | export default Alchemy.Stack( |
| 69 | "MyApp", |
| 70 | { |
| 71 | providers: Layer.mergeAll(Cloudflare.providers(), Stripe.providers()), |
| 72 | state: Cloudflare.state(), |
| 73 | }, |
| 74 | Effect.gen(function* () { |
| 75 | const api = yield* Api; |
| 76 | return { url: api.url }; |
| 77 | }), |
| 78 | ); |
| 79 | ``` |
| 80 | |
| 81 | ## Learn more |
| 82 | |
| 83 | - [Stripe on alchemy.run](https://alchemy.run/stripe) |
| 84 | - [Setup](https://alchemy.run/stripe/setup) |
| 85 | - [API reference](https://alchemy.run/providers) |