A worked OpenSouk referral endpoint, in seven small TypeScript modules.
Start with Set up referrals — it is the integration end to end. This repo is the executable version of it.
Two different floors, for two different reasons:
- Runtime — Node 20+. The example itself (hono,
@hono/node-server, viem) has no dependency on anything newer. Serve it on Node 20 withnpx tsx src/server.ts, or compilesrc/withtscfirst — either way you load the environment yourself, sincenpm startreads.envthrough Node's own--env-file-if-exists, which arrived in 22.9. - Repo tooling — Node 24+.
npm testandnpm run typecheckneed it: vitest 5 supports^22.12 || ^24 || >=26, and Node's unflagged TypeScript stripping (which is what letsnpm startrunsrc/server.tsdirectly, with no build step) only starts at 23.6. 24 is the lowest version where both work. This is a constraint of this repo's tooling, not of the OpenSouk protocol.
- Answer both
POSTandGET. Buyer tooling POSTs; the readiness probe GETs. An endpoint registered on POST alone reports as not ready even though purchases against it succeed. →src/referral-endpoint.ts - Resolve
payTofrom the address registry; do not configure it. Registry entries change behind a timelock, and a402naming last week's split router does not settle. →src/split-router.ts - A rejected payment is HTTP
200withsuccess: false. Branch on the body. Branching on the status reads a refusal as a completed sale. →src/settle.ts - Read
PAYMENT-SIGNATURE, notX-PAYMENT. The latter is x402 v1, which the onboarding manifest's prose recipe still names. →src/settle.ts - Sign the settlement request. Only the seller may create a purchase record. Unsigned, the
call is just an HTTP POST anyone can make: a buyer holding your public ref link can settle
their own cent-sized payment and mint an on-chain purchase — the protocol's highest review
trust tier — having bought nothing from you. →
src/settlement-auth.ts
Two routes. /buy is the merchant's ordinary paid route, and this repo never modifies it. Its
payTo is the merchant's own wallet and it carries no attribution fields.
/ref/buy is the copy, named after /buy with ref after the base path, as
Set up referrals recommends. Same product,
same price, same auth, differing only in payTo, the facilitator, the echoed extensions, extra.endpoint, and the two headers that sign the
settlement call. Those five things are the whole integration. The rest of what src/referral-endpoint.ts adds over src/direct-endpoint.ts is
the ordinary x402 retry handshake — decode PAYMENT-SIGNATURE, settle, then serve — which
direct-endpoint.ts omits and a merchant already selling over x402 already has.
extra.endpoint is the path of the request you are answering (/ref/buy here), in the 402
and again at settlement. A buyer may pay this endpoint with a ref link minted for any of your
products; the facilitator records the product whose registered endpoint_url has this path, at
that product's commission rate. It must be the path you registered, or the sale is refused. Send
the path, not the full URL: the query string is the buyer's input. Leave the field out and every
sale here is recorded as the ref link's product.
You register /ref/buy as the product's endpoint_url, so it is where ref links send buyers.
The facilitator accepts /verify and /settle from the merchant owner or from any address
holding SETTLEMENT_SIGNER_ROLE for that merchant. This server signs with
MERCHANT_SIGNER_PRIVATE_KEY, over keccak256(body || uint64_be(unix_seconds)), EIP-191, in
X-Agent-Signature and X-Agent-Timestamp — the same scheme the protocol API uses for agents.
Use a dedicated hot key and grant it the role. The owner key controls your commission rates, your delisting, your payout wallet and the merchant identity itself; none of that is needed to sell something, and an always-on web server is the wrong place to keep it.
Order matters. Grant the role first, then deploy the key:
# Role id: keccak256("SETTLEMENT_SIGNER_ROLE")
cast send $MERCHANT_REGISTRY \
"grantRole(uint256,bytes32,address)" \
$MERCHANT_ID \
0x9afba5c05b1dfbaa7188068a6610fc1a9f4c5b291aea74d5d9d8bc81f8d004f1 \
$SIGNER_ADDRESS \
--private-key $MERCHANT_OWNER_KEYA signature the facilitator cannot place is refused whatever its rollout setting says — the flag governs whether unsigned calls are still tolerated, never whether a wrong signature is. So deploying a key you have not granted takes your sales down immediately. The server prints the signer address at startup for exactly this check.
Rotating is the same order in reverse: grant the new key, deploy it, then revoke the old one.
cp .env.example .env # then fill it in
npm install
npm start # Node 24+; on Node 20 use: npx tsx src/server.tsNothing is deployed on a public chain yet, so the values in .env.example are placeholders —
zero addresses and a eip155:0 network — not any particular chain's real values. The zero
MERCHANT_SIGNER_PRIVATE_KEY is not a usable key either; generate one and grant it the role.
# the probe path: a 402 naming the split router
curl -s -X POST localhost:8083/ref/buy | jq '.accepts[0].payTo, .extensions'"0x70997970C51812dc3A010C7d01b50e0d17dc79C8"
{
"attributionToken": "",
"setup_url": "http://127.0.0.1:8080/.well-known/opensouk"
}
(payTo above is anvil account #1. It came from a local registry read during testing —
resolved, not configured, per point 2 above — and like every address in this repo it is a
placeholder, not somewhere to send money.)
setup_url is built from MERCHANT_API_URL, not from MERCHANT_FACILITATOR_URL: the two are
different hosts, and only the API serves /.well-known/opensouk. Pointing it at the
facilitator answers 404 to the one reader the field exists for — a buyer that has just failed to
pay and is looking for how to onboard.
No wallet, no signer, no private key — on the x402 path a merchant signs nothing, so no key
handling appears anywhere in this repo. Minting the merchant identity and listing the product are
one-time setup steps the quickstart covers with cast and curl; they are deliberately not here.
hono (the server), @hono/node-server (its Node adapter), and viem (one eth_call and one
keccak256). Nothing else.