This is the full file; follow it from Step 0.
OpenReceive agent directions (SvelteKit)
# READ THIS FIRST: this file is 35 KB and a summary drops required steps. Download it whole:
curl -fsSL https://openreceive.org/agent-directions/sveltekit/full.md
# Skip the download only if you already have all of it: pasted, read from disk or fetched raw.
Step 0 in brief (Step 0 below has the details):
- Before installing or editing anything, ask the user for a receive-only NWC code. One question per message.
- Next ask for the LSC code. If the user wants stablecoins, USDT, USDC, ETH, SOL or crypto, do not ask yes/no first.
- The user only pastes codes into this chat. Never ask them to edit .env or run a command to save one.
- Write each code into .env yourself, as Step 0 says. Never echo it or put it in a shell command.
- Do not suggest rotating or revoking a code because it was pasted here.
- Start the quickstart only once the NWC code is saved, and the LSC code is saved or the user said “Bitcoin only”.
These directions describe OpenReceive 0.4.26.
Add OpenReceive to a SvelteKit application — the app you are already working
in, on @sveltejs/adapter-node. OpenReceive ships no SvelteKit package: the
integration is one endpoint that hands each request to @openreceive/http.
You do not need a copy of the OpenReceive source: the packages are on npm, and
the SvelteKit recipe is appended to this file in full as the quickstart, so
you can do the whole integration without fetching anything. Prefer the
published packages and the handler they export — do not reimplement wallet
RPC, settlement, or pricing.
Do not clone the OpenReceive repository into this app, and do not copy a demo’s
models (ShopOrder, a signed-cookie visitor, an in-memory catalog) over tables
that already exist. Find this application’s order, product, and user models —
whatever they are actually named — and map the three hooks onto those.
Keep this application’s frontend framework, authentication and database. Pick
the UI package that matches the frontend that is already here
(@openreceive/react, /vue, /svelte, /angular, or /elements for
plain HTML) — do not add React to a Vue app. Reuse the app’s existing
session/auth in authorize and its existing database handle in storage.db.
What OpenReceive is
A payment library that runs inside YOUR server. It mounts as ONE catch-all
endpoint (src/routes/openreceive/[...path]/+server.ts, on Node with
adapter-node) in the application you are editing, issues Lightning
invoices against a wallet the merchant already controls, and calls back into
your code when one settles. There is no OpenReceive account and no API key, and
OpenReceive never holds the funds — the sats land in the wallet the merchant
connected.
The one required credential is a receive-only NWC code (Nostr Wallet Connect):
a string from the merchant’s wallet that can create invoices and read their
status, and cannot spend. A swap provider (an “LSC” code) optionally lets the
payer send USDT, USDC, ETH or SOL instead, converted into that same
Lightning payment. Step 0 collects those credentials from the user; you write three hooks — authorize, amountFor,
onPaid;
OpenReceive supplies invoices, polling, settlement and the checkout UI. It never
owns orders, users, prices, or fulfillment.
Step 0 — ask for the two codes, one question at a time
Before anything else, check one file: the project’s .env, if it exists.
grep -E '^(NWC_URI|LSC_URI_PRIMARY)=.' .env | cut -d= -f1 names the codes
it sets without printing them. Check with that, here and after each write,
not by reading the file, and never print the values. A code that is already
set is not asked for again; if both are set, skip to step 5 (making the
server load the file), then the quickstart.
On a hosted builder (v0, Vercel, Replit, Lovable), the user may say instead
that both codes are already set as the project’s environment variables or
secrets. Believe them: do not ask for the codes, do not write .env, and
do not read or print the variables. Skip step 5’s file loading: the platform already puts them in process.env. Do not add them to a compose file or any other deploy config
either: they already reach the app, and a line such as NWC_URI: ${NWC_URI}
replaces them with an empty value.
Otherwise your next action is a question to the user. Do not install packages,
edit the app, write .env.example, or search anywhere else before asking
it. Do not read deploy config (compose files, Vercel or platform secrets), Docker containers, or process environments, and never run
printenv. A new shop has neither code yet: hunting for one burns the turn and
can leak other secrets into the transcript.
Two server-only URIs are needed before the quickstart:
NWC_URI— a receive-only Nostr Wallet Connect code,nostr+walletconnect://…. Required for Bitcoin.LSC_URI_PRIMARY— a Lightning Swap Connect URI,lightning+swapconnect://…. Required for USDT, USDC, ETH and SOL. Skip it only when the user says they want Bitcoin alone.
The user never edits an environment file. They paste each code into the chat; you store it. Ask one question per message.
-
First message — the NWC code, and nothing else. Ask for it and walk them through getting it:
To receive payments I need a receive-only wallet code. In Rizful: open the menu, tap NWC, choose Receive-only NWC code, and tap Copy (https://openreceive.org/get_a_nwc_code_to_receive_payments). If you would rather run your own wallet, Alby Hub works too: Connections → Add Connection → Read Only. Paste the code here and I will store it.
Do not mention
.env, exports, or “tell me when it’s set”. - When they paste it. If it does not start with
nostr+walletconnect://, ask them to copy the receive-only code again. Otherwise writeNWC_URI=<paste>into the project’s.envwith your file-editing tool, creating the file if needed. Never write it with a shell command (echo,printf, a heredoc,python -c): the command line shows the code. Make sure.gitignorecovers.env(and.dockerignore, if the app has one). Never echo the value, commit it, or put it in client code. Reply only that it is saved, then ask the next question. -
Second message — swaps. If the user asked for stablecoins, USDT, USDC, ETH, SOL, altcoins or “crypto” (as in “Bitcoin and stablecoin payments”), this message IS the walkthrough below: send it as it is, and do not ask yes or no first. Otherwise ask whether payers should also be able to pay with USDT, USDC, ETH or SOL, then give the walkthrough. The walkthrough:
Go to https://lightning-swap.com, sign in for API keys, create a key, and copy the whole URI (https://openreceive.org/set_up_swap_provider). Paste it here and I will store it — or say “Bitcoin only” and I will continue without it.
Mention FixedFloat only if they already use it.
- When they paste it. If it does not start with
lightning+swapconnect://, ask them to copy it again. Otherwise addLSC_URI_PRIMARY=<paste>to the same.envwith the file-editing tool, never a shell command. Swaps are now on, so build the refund route back (the swap non-negotiable below) as part of this integration. If they chose Bitcoin only, leaveLSC_URI_PRIMARYunset and skip that route. - Make the server load the file — yourself. OpenReceive reads
process.env, and a.envfile on disk is not in it.vite devreads.envonly for the variables the app declares, so start it withnode --env-file=.env node_modules/.bin/vite dev, and start the built server withnode --env-file=.env build. When the app is started with Docker Compose, give the serviceenv_file: .env. Never make a code public (public: trueinsrc/env.ts, or aPUBLIC_prefix): that sends it to the browser. Restart the server after writing the file. Under Compose, recreate the container withdocker compose up -d:docker compose restartkeeps its old environment.
Do not invent placeholder URIs. Start the quickstart only once NWC_URI is
saved and LSC_URI_PRIMARY is saved or explicitly declined. The first boot
runs the receive-only preflight: if it reports spend methods such as
pay_invoice, remove NWC_URI from .env and ask for a receive-only code
again. Never set the spend-capable override to get past it.
Upgrading an existing install
This is not the opening move of a new integration. When OpenReceive is ALREADY
installed here, npx openreceive doctor reports every credential as set/unset
(never the value) and probes the wallet. Check the installed
@openreceive/node and @openreceive/browser against the release named at the
top of this file: the headless display models below do not exist in older
versions, and the first tile click throws with nothing saying why. Upgrade
first.
Only then start the quickstart.
After the quickstart — hand over, then stop
The quickstart is done when
npx openreceive doctor --db <file-or-url> --url <app url> is clean and this
app serves the checkout page for one of its orders. Doctor names any failed
check and exits nonzero; fix it before going on.
Doctor reads .env itself. Never source it into a shell: the & in a
code splits the value and prints the pieces.
Give the user that checkout link. The browser check of that page (payment-method icons, wallet logos, a pay tutorial) is theirs: name it in one line of your closing message, and do not run it yourself.
You cannot pay the invoice: the code is receive-only. Do not pay, settle or
mark an order paid, and do not look for a way to (a wallet control port, a
test endpoint, another wallet). If the user wants a real settlement test, they
pay on that link from their own wallet, and onPaid marks the order paid.
Setup ends here. Your last message starts “Setup is finished” and has at most five short lines (about 80 characters each): the link, the methods it offers, and one line of what to look at. Send nothing after it. Do not list what changed, copy out the quickstart’s browser checklist, offer more work, or end the message on a question.
- The link is to a real unpaid order. Keep that order; do not delete it.
- When this app’s orders belong to a session or cookie, the user’s browser
cannot open the order you made. Then the link is the shop’s own page:
“Open
and click Buy: it opens the checkout." Do not explain order ownership or mention your test order. - Name the methods it offers: Bitcoin, plus USDT, USDC, ETH and SOL when
LSC_URI_PRIMARYis saved. Do not mention minimums, and never say a coin will not work or will not be offered.
Non-negotiables
The quickstart below has the code. These are the rules it cannot state for itself, and they hold for every integration.
- OpenReceive never owns orders, users, prices, or fulfillment. The section
below is how those tables sit next to the library — not a second order model,
and not a Prisma/Drizzle relation to
openreceive_payments. - Keep
NWC_URI/LSC_URI_*server-only. Never put them in browser code, logs, or assets. - Do not suggest rotating, revoking or replacing a code because it was pasted into this chat; that is the supported path.
- Work only in this application. Never read or run anything from another
project on this machine (its
node_modules, tools or source), for any reason. A browser and Playwright are not part of setup. - Restart only this app’s server, on the port it already uses: stop the
process you started, or the one listening on that port, by its pid (or
restart its Compose service). Never
pkillorkillallby name: that stops other people’s servers too. Start it the way this project already does (its README, Procfile, compose file or package script), on its own port, not a port you pick. - Run commands where the app runs. When a compose file builds it, run its
package manager, generators, migrations and doctor inside that service
(
docker compose execorrun), and rebuild the image after adding a package. This machine’s language version and database path are not the app’s. - To check that the running app sees the codes, run doctor: it reports each
one as present or missing and never prints a value. Never print the
environment (
printenv,env,docker compose config), even filtered to names. - The host owns the price.
amountForreads it from your own data; reject payer-supplied amounts. authorizeruns on every request, and theresourceit receives is a CLAIM the payer made, not proof. Read a framework session; never trust a body field.onPaidmust be idempotent. Its database fulfillment commits once perreference— your order id, one per thing you fulfill, created before checkout, kept across retries, never reused. A fresh id per page load lets one order be paid twice.- Receive-only NWC is required; a spend-capable code fails closed at boot unless explicitly overridden.
- There is NO merchant-initiated refund of a settled Lightning payment, because
the wallet cannot spend. Swap refunds — a payer reclaiming a deposit that
never converted — are the only refund OpenReceive performs, and only from the
refund_requiredprovider state. Do not build, promise, or imply a Lightning refund path. - IF YOU TURN SWAPS ON, BUILD THE ROUTE BACK. A deposit that arrives short or
late becomes
refund_required, and the payer claims it on a SECOND VISIT, after leaving your page to fetch an address from another wallet. Three things must exist or that money is unreachable through your UI: a per-order URL your server serves (/checkout/:reference—syncUrlon<Checkout>,sync-urlorresumableon<openreceive-checkout>), your own order-summary route to restore the order from, and the ATTEMPT./checkouts/preparereturns no attempts, so a checkout rebuilt from the reference alone opens on the method grid. Re-picking the same coin (POST /swaps) re-serves the committed attempt — but only while it is live, and the shadow invoice behind a swap lasts about half an hour, after which the same click mints a NEW deposit address and the refund is off-screen. Keep thepayment_hashand reopen the attempt withPOST /swaps/status, which has no such window. On the drop-ins:resumePaymentHash, fed fromonState, on<Checkout>; theresume-payment-hashattribute, fed from theopenreceive-stateevent (event.detail.state.payment_hash), on<openreceive-checkout>. That visit can come hours later, andauthorizemust still know the payer: a session that expired in between answers 403 and strands the deposit. On a guest shop, tie the order to a long-lived signed cookie, or put a secret per-order token in its URL that your page trades for that cookie. https://openreceive.org/guides/swap-refunds.md - Show the payer WHAT THEY ARE BUYING. Return an optional
descriptionbeside the price fromamountForand both drop-ins render it above the amount. Without it the checkout is a QR and “$1.00” with no sign of what the dollar is for. - Show the payer the transaction record:
createTransactionDetails(...)rows, collapsed behind a caret, on the live checkout AND on the receipt. A payment hash and a deposit txid are the only evidence a payer has that they paid you.<Checkout>/<openreceive-checkout>already render this panel and thedescription— these two rules cost you code only on a custom UI or your own receipt page, never a reason to replace the drop-in. (It returns no rows while the rail ischeckout_lock— before the payer has chosen anything there is no transaction — so render the caret only when the rows are non-empty.) - The handler lives in a server-only module under
src/lib/server/, and the stack is built on the first request, never at import:vite buildimports server modules to analyse them, and a stack connects to the wallet as it is built. The endpoint exportsGETandPOST, runs on Node through adapter-node — never an edge runtime — and is never prerendered. If you declareNWC_URIinsrc/env.ts, give it the recipe’s schema, which returns""whilebuilding: the build checks every declared variable without the codes, and a missing one fails it. - Install the OpenReceive packages and
pgasdependencies, neverdevDependencies: adapter-node bundles devDependencies into the server build and leaves dependencies tonode_modules. - Pass the
RequestEventasnative, soauthorizereads the session the app already has, and giverateLimitingip: ({ native }) => native.getClientAddress(). Behind your own reverse proxy, set adapter-node’sADDRESS_HEADERandXFF_DEPTH, or every payer shares the proxy’s address. <Checkout>comes from@openreceive/svelte/checkout.svelte, with@openreceive/svelte/styles.css, on the order’s own page: its resumable URL. SvelteKit’s CSRF check refuses cross-site form posts; the checkout posts JSON, which it lets through, so leave the check on.- HTTP JSON is snake_case; TypeScript APIs are camelCase.
- Money is integers or decimal strings — never binary floats.
Your tables, not ours
npx openreceive scaffold payments emits openreceive_payments and
openreceive_meta for THIS application’s database. That is the whole
persistence OpenReceive needs. It does not replace your orders, users, or
products, and you do not join them.
- Find this app’s models first. They may be named
Order,Invoice,Booking,Product,Variant,User,Account— anything. Wire the hooks to those. Do not generate a parallelShopOrder/ShopProduct/ShopUserstack. - The payable row’s id is the
reference. Create it before checkout, keep it across retries, never reuse it. Pass that id to<Checkout>/<openreceive-checkout>. A fresh id per page load lets one order be paid twice. - Products (or the catalog) are the price authority. Order creation reads
live prices into the order (snapshot line items if this app has them).
amountForreads only that order — never a payer-supplied amount, never a live catalog lookup that could re-price a cart already placed. Return{ currency, value }as a decimal STRING, plus adescriptionof what they are buying. - Users own the order; OpenReceive never sees them.
authorizeuses the same ownership check this app already uses on the order show / pay page —event.locals.user,event.cookies, whatever it is.nativeis the request’sRequestEvent.resource.referenceis a claim the payer sent, not proof. - The order is unpaid or paid. Do not copy
pending/expired/failed/attentiononto it. Those are attempt statuses onopenreceive_payments. An expired invoice does not cancel the order; a later checkout may mint another attempt. The library refuses a new checkout under a reference that already settled (409). - A paid order’s page says it is paid and shows no checkout. Nothing is left
to pay, and a checkout cannot start a new payment for a paid order. Mount the
checkout only while the order is unpaid. When it settles on the page, update
the page’s status line from
onSettledon<Checkout>:onPaidhas already marked the order paid. Keep that checkout mounted, because it is the payer’s receipt. - Pass this app’s
dbhandle. Do not add a Prisma/Drizzle relation from Order toopenreceive_payments, and do not implementPaymentRepositoryunless no supported handle can reach this database.referenceis not unique (many attempts per order). Fulfillment is a guarded transition on YOUR order row insideonPaid—UPDATE … WHERE state = 'awaiting_payment'(or this app’s equivalent) through thequerythe library hands you, on that same settlement transaction, not a second connection from your ORM. Database writes only in the hook; emails, jobs, and pushes after commit. Placeholder style is the dialect you declared:?on sqlite,$1on postgres.
If you build your own checkout UI
The drop-ins (<Checkout>, <openreceive-checkout>) already obey all of this.
This list is the short form of https://openreceive.org/guides/checkout-ux.md, for a
UI built on @openreceive/browser/headless. Read that before writing
components.
- Check Bitcoin → Switch payment method → Bitcoin: the grid must hide the Lightning invoice, then restore the same bolt11 without another mint.
createCheckoutControlleris the engine. Do not hand-roll a poll loop.createCheckoutStatusModelfor the status line. Do not draw a Cart → Pay → Done stepper. Read the model’sphase, not the snapshot’s.resolveWizardSelectiondecides whether to ask “which network?”. A one-network asset starts the swap from the tile. KeyselectedAssetByGroupby group (USDT), valued bypay_in_asset(USDT_TRON).createMethodGridDisplayfor tiles, includinglimitMessageso an unavailable method says the minimum in the payer’s currency.createSwapDisplayModel→display.copyRowsfor deposits: address, memo, and the bare amount each get a copy row. Renderswap.networkWarning*as the model gives it.swap.deposit_amountis the only amount a payer is told to send. Never put a fiat valuation of it (swap.fee.pay_in_fiat) next to a stablecoin amount: “$50.03” under “50.05 USDC” reads as a typo, and the payer asks which one to send. The one fiat figure on a USDT/USDC deposit panel is the cart total (payout_fiat); express “you send” and the fee in the token. UsecreateSwapFeeBreakdown(fee, swap)— with the swap, not the fee alone — and it applies this for you; SOL and ETH keep a fiat breakdown.createCheckoutSessionowns mint and swap start. To start swaps, pass itsswapoption (selection,prefix,fetch) together. Without itstartSwapreports throughonError.createQrSvgis async. UsecreateQrSvgControllerso you do not render[object Promise].checkoutLabelsfor every payer-facing string. Only write copy it lacks.stageSwapRefundthenconfirmSwapRefund— only the second submits. Validate withgetSwapRefundFormError. Treat409as a normal outcome.- Pass
{ resumable: true }tocreateSwapDisplayModelwhen the payer has a URL they can come back to, and renderdisplay.refundReturnLabel. Resume helpers (createGuestCheckoutResume,createGuestOrderFetcher) are on@openreceive/browser, not/headless. - A refund replaces the deposit panel. On
refund_requiredalso drop “switch payment method”. - No “Open wallet” button on desktop.
-
Wallet suggestions:
getPaymentWizardRoutes()+createWizardRouteDisplays. Lightning only. Logos are data URIs; tutorial images load from a JavaScript chunk. For a custom headless UI, load it when a tutorial opens and look up the returned table by the tutorial’spath:import { loadPayTutorialImages } from "@openreceive/browser/headless"; const images = await loadPayTutorialImages(); const src = images[tutorial.path]; // data URI for the selected tutorialRender
srcas the image source and update your UI after loading. Existing display objects do not update: theirtutorial.imagestaysundefinedif created before loading. Alternatively, await the loader, recreate the displays withcreateWizardRouteDisplays, and render the newtutorial.image. Show the caption while loading or if loading fails; never use an empty image source. Deploy all JavaScript chunks and allowdata:in CSPimg-src. For missing images, check CSP errors, failed chunks, and stale displays. Registry paths are lookup keys; there is no asset option or image route.
More documentation
Fetch one when the moment comes. Each is raw markdown, so a plain GET is
enough; drop the .md for the same page a person would read.
- https://openreceive.org/guides/authorization.md — before you write
authorize - https://openreceive.org/guides/environment-variables.md — every variable, and what is deliberately not one
- https://openreceive.org/guides/storage.md — the payment tables and the attempt state machine
- https://openreceive.org/guides/supabase.md — when the database is Supabase: its pooler, its certificate, and locking the tables away from its Data API
- https://openreceive.org/guides/serverless.md — when the app runs serverless (Netlify, AWS Lambda, Cloud Run, Google AI Studio): pooled database URLs, timeouts, and why it needs no cron or worker
- https://openreceive.org/guides/node-orms.md — recipes for Prisma, Drizzle, Knex, TypeORM, Sequelize
- https://openreceive.org/guides/frontend-checkout.md — the drop-in’s props, attributes and slots
- https://openreceive.org/guides/mobile-apps.md — when a mobile app takes the payment: the checkout page it opens, and what the App Store and Google Play allow
- https://openreceive.org/guides/checkout-ux.md — read before building any custom UI
- https://openreceive.org/guides/headless-checkout.md — the controller, the display models, refunds
- https://openreceive.org/guides/provider-registry.md — where the wallet logos and pay tutorials come from: inside the JavaScript, nothing to serve. This is the page that owns the image rule, not the summary in checkout-ux.md
- https://openreceive.org/guides/automated-swaps.md — only if
LSC_URI_PRIMARYis set - https://openreceive.org/guides/swap-refunds.md — the refund flow, and the route back to it. Read it before you turn swaps on
- https://openreceive.org/guides/lightning-swap-connect.md — what an
LSC_URI_*code actually is - https://openreceive.org/guides/price-feeds.md — where the fiat→sats rate comes from, and how to replace it
- https://openreceive.org/guides/host-testing.md — testing your three hooks without a live wallet or provider
- https://openreceive.org/guides/rate-limiting.md — before a public shop goes live
- https://openreceive.org/guides/security.md and https://openreceive.org/guides/deploying.md — before this goes anywhere real
- https://openreceive.org/guides/api-reference.md — every route, option and error code
- https://openreceive.org/guides/custom-checkout-route.md — advanced: replacing the shipped adapter’s routes with your own
- https://openreceive.org/guides/react-material-ui-recipe.md — a worked custom UI on a component library
- https://openreceive.org/guides/quickstart-next.md — each hook explained step by step, on Next.js; they are the same here
- https://openreceive.org/guides.md — the index, if what you need is not above
Questions, or a problem with the library itself: https://openreceive.org/contact
- https://openreceive.org/guides/payment-safety-upgrade.md — coordinated upgrades and reviewed repair of existing attempts
The quickstart, in full
Inlined verbatim so this file needs no network access — follow it once Step 0 passes. The page it comes from is https://openreceive.org/guides/sveltekit-recipe.
SvelteKit recipe
OpenReceive ships no SvelteKit package. A SvelteKit endpoint (+server.ts)
takes a Web-standard Request and returns a Response, and so does
@openreceive/http, so the integration is the route below. Your server code
creates each Lightning invoice, the payment goes straight to your wallet, and
payment attempts are stored in your app’s database. There is no OpenReceive
account and no background worker.
Optional swaps let customers pay with USDT, USDC, SOL, and ETH. A swap provider you configure converts the payment to BTC over Lightning, and it settles into the same wallet. Available assets and networks depend on the provider.
This recipe was checked with @sveltejs/kit 3.0, @sveltejs/adapter-node
6.0 and @openreceive/* 0.4.25. The built app ran on Node in Docker, with
Postgres and a test wallet on a public NWC relay. In Chromium, the buyer’s
checkout page showed the Lightning invoice. A stranger got 403 from the
payment routes and 404 from the page, and a request that named its own amount
got 400. When the wallet marked the invoice paid, the page showed it paid and
onPaid marked the order paid.
npm install @openreceive/http @openreceive/svelte pg
Where it runs
On Node, with @sveltejs/adapter-node. The wallet connection and your
database driver need Node, so the route cannot run on an edge runtime. The
Vercel and Netlify adapters deploy Node functions: read
Bitcoin checkout on serverless hosts first. For
Cloudflare Workers, the TanStack Start recipe
explains what a Worker needs. This recipe was not run on those hosts.
Secrets
SvelteKit 3 reads the environment variables you declare in src/env.ts. The
build runs your app to analyse it, without your secrets, so require them only
when the app starts:
// src/env.ts
import { building } from "$app/env";
import { defineEnvVars } from "@sveltejs/kit/env";
const secret = (value: string | undefined): string => {
if (value) return value;
if (building) return "";
throw new Error("is not set");
};
export const variables = defineEnvVars({
NWC_URI: { schema: secret, description: "Receive-only NWC code from your wallet" },
DATABASE_URL: { schema: secret, description: "Postgres connection string" },
});
Never mark them public: true, which would send them to the browser.
The payment route
Keep the handler under src/lib/server/. SvelteKit refuses to bundle a
module from there into browser code:
// src/lib/server/openreceive.ts
import { createStack, type Stack } from "@openreceive/http";
import type { RequestEvent } from "@sveltejs/kit";
import { NWC_URI } from "$app/env/private";
import { db } from "./db"; // your existing pg Pool
import { findOrder } from "./orders"; // your own orders
let stack: Stack | undefined;
// Built on the first request, not at import: the build imports server
// modules to analyse them, and a stack connects to the wallet at once.
function openReceive(): Stack {
stack ??= createStack({
wallet: { nwc: NWC_URI },
storage: {
db,
// Runs once, inside the transaction that records the payment.
onPaid: async ({ reference, paidAt, query }) => {
await query(
"UPDATE orders SET state = 'paid', paid_at = $1 WHERE id = $2 AND state = 'awaiting_payment'",
[paidAt, reference],
);
},
},
// The price comes from the order row, never from the request.
amountFor: async (reference) => {
const order = await findOrder(reference);
return order?.state === "awaiting_payment"
? { currency: order.currency, value: order.amount, description: order.title }
: null;
},
// Only the buyer may pay for their order.
authorize: async ({ native, resource }) => {
const event = native as RequestEvent;
const order = resource.reference ? await findOrder(resource.reference) : undefined;
return Boolean(order && order.visitor === event.cookies.get("visitor"));
},
// adapter-node's client address: the socket, or ADDRESS_HEADER behind a proxy.
rateLimiting: {
ip: ({ native }) => (native as RequestEvent).getClientAddress(),
},
});
return stack;
}
export function handleOpenReceive(event: RequestEvent): Promise<Response> {
return openReceive().handler(event.request, { native: event });
}
// src/routes/openreceive/[...path]/+server.ts — every route under /openreceive/
import { handleOpenReceive } from "#lib/server/openreceive.ts";
import type { RequestHandler } from "./$types";
export const GET: RequestHandler = (event) => handleOpenReceive(event);
export const POST: RequestHandler = (event) => handleOpenReceive(event);
native is the request’s RequestEvent, so authorize can check the
session your app already has: its cookies, or the event.locals.user your
hooks.server.ts sets. The Next.js quickstart
explains each hook. They are the same here.
One stack serves the whole server process. Behind a reverse proxy, set
adapter-node’s ADDRESS_HEADER (and XFF_DEPTH), so that
getClientAddress() returns the payer’s address and not the proxy’s
(Rate limiting).
On SvelteKit 2, import the secret as env.NWC_URI from
$env/dynamic/private, and import server modules from $lib/server/, with
no src/env.ts. The rest is the same. This recipe was checked on SvelteKit 3
only.
Your database
db is a pg Pool, node:sqlite or better-sqlite3, or one of the
adapters in Node ORM recipes. With Drizzle, pass
the driver underneath it. sv add drizzle sets up Postgres through
postgres.js or Neon, which OpenReceive does not take directly. Then give
OpenReceive its own small pg Pool on the same DATABASE_URL. It is the same
database, not a second one.
Create OpenReceive’s two tables with your migrations
(npx openreceive scaffold payments --orm drizzle writes the Drizzle
schema), or run paymentsSchemaSql("postgres") from @openreceive/http
once. It is idempotent. See Payment storage.
The checkout page
// src/routes/checkout/[reference]/+page.server.ts
import { error } from "@sveltejs/kit";
import { findOrder } from "#lib/server/orders.ts";
import type { PageServerLoad } from "./$types";
export const load: PageServerLoad = async ({ params, cookies }) => {
const order = await findOrder(params.reference);
if (!order || order.visitor !== cookies.get("visitor")) error(404);
return { reference: order.id };
};
<!-- src/routes/checkout/[reference]/+page.svelte -->
<script lang="ts">
import Checkout from "@openreceive/svelte/checkout.svelte";
import "@openreceive/svelte/styles.css";
import type { PageProps } from "./$types";
let { data }: PageProps = $props();
</script>
<Checkout reference={data.reference} prefix="/openreceive" />
The page renders on the server and starts the checkout in the browser. The payer picks Bitcoin and gets the invoice. Keep it at the order’s own URL: a payer with a swap deposit in flight comes back to it (Swap refunds).
Settings
NWC_URI: a receive-only code from your wallet (get one).DATABASE_URL: your Postgres. On serverless hosts use the pooled URL.- Optional:
LSC_URI_PRIMARYfor swaps (set one up), andLSC_URI_BACKUP. OpenReceive reads these, andLOG_LEVEL, straight fromprocess.env, not throughsrc/env.ts.
In production, set them all in the server’s environment. In development,
vite dev reads .env for the variables in src/env.ts but keeps the file
out of process.env. To try swaps locally, start it with
node --env-file=.env node_modules/.bin/vite dev.
No worker or cron job is needed. Each request to the payment routes also checks the wallet for settled invoices, through a lock in your database. A payer who closes the tab is settled on the next request.