examples/saas, so it runs as written. If you would rather read code than follow steps, clone that folder and run npm start.
1
Get a test secret key
Open the dashboard, go to Developers โ API keys, and create a key. It is shown once. Keep it on the server; the SDK refuses to construct in a browser.Then set a payout address under Settings. Checkout links cannot be created until Elapse knows where to send your settlements, and live keys are issued only after it is set.An
export lasts only as long as the terminal. Put both in your projectโs .env (git-ignored) so your server keeps them across restarts, and in your hostโs environment settings when you deploy. Every snippet below reads them from the environment, and so does the CLI.2
Install the SDK and construct a client
- TypeScript
- cURL
server.ts
secretKey and apiUrl come from the environment. The hosted API is https://api.elapse.finance; pass it as baseUrl explicitly, see Authentication for why.3
Create a Product
A Product is something billed at a rate per second. The rate is a decimal string in USD, never a float.
- TypeScript
- cURL
server.ts
PRODUCT is { name: "GPU ยท 4090", rateUsdPerSecond: "0.004" }. The second line is the create call; the first looks for a Product with the same name, so restarting your server does not litter the dashboard.4
Create a Checkout session on your server
- TypeScript
- cURL
server.ts
session.id to your page โ that is all the browser needs, and the only Elapse id it should ever hold. When the meter stops, Elapse returns the subscriber to success_url with ?session_id=cs_โฆ appended.5
Handle subscription.canceled
Read the raw request body, verify it, answer 200, then do your work. Never parse the body before verifying; the signature covers the exact bytes.
- TypeScript
- cURL
webhooks.ts
webhooks.ts
constructEvent throws on a missing, malformed, expired or mismatched signature. Anything it returns is genuine. The full handler, with the entitlement map and the six actions, is 45 lines in webhooks.ts.6
Forward webhooks to your laptop
Your server is on
localhost, which Elapse cannot reach. The CLI opens a stream and forwards each delivery, byte for byte, with the platformโs real signature.npx fetches the CLI from npm on first run; there is nothing to install. The first line it prints is your signing secret. Put it in ELAPSE_WEBHOOK_SECRET and restart your server. Then you will see:The CLI registers a webhook endpoint for you; it appears on the dashboard under Developers โ Webhooks as
cli://. In production you add your own https:// URL there instead, copy the secret it shows once into your serverโs environment, and Elapse delivers straight to it with retries. Your server code is the same in both cases.7
Render the meter, then authorise on your phone
@elapse/react takes the session id and your publishable key. Face ID happens in a frame Elapse
opens over your page, so your code never touches the subscriberโs wallet.mount.tsx
subscription.canceled
with seconds_elapsed and what was paid.Next
Webhooks catalog
The other five events and what to do with each.
Clone the finished merchant
examples/saas: the code these snippets come from, with a fake product page and a demo check.