feat(web-search-paid-api-ts): pay-as-you-go API key tutorial (Exa pattern)#51
Draft
r-marques wants to merge 1 commit into
Draft
feat(web-search-paid-api-ts): pay-as-you-go API key tutorial (Exa pattern)#51r-marques wants to merge 1 commit into
r-marques wants to merge 1 commit into
Conversation
Demonstrates the Exa-style monetization pattern: one x402-protected endpoint returns a quota-bearing API key, and the provider keeps using its own metering on the issued key. Sibling to http-simple-agent-ts, which shows the opposite pattern (burn credits on every request). Handler ordering is verify -> settle -> provision so a settle failure leaves the store untouched. In-memory provider quota keyed on payer wallet means returning payers top up their existing key, matching the behaviour Exa ships in production. Includes an llms.txt + nevermined.md template under src/llms-txt-template/ so other providers can publish the same kind of agent-discoverable integration page. First of three PRs landing nevermined-io/nvm-monorepo#1684 (tutorial -> docs -> multi-agent validation). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
web-search-paid-api-ts/demonstrating the pay-as-you-go API key monetization pattern: one x402-protected endpoint returns a quota-bearing API key, and the provider does its own metering on the issued key.http-simple-agent-ts/— that one settles credits on every protected request (burn-credits middleware). This one settles only at purchase time and gets out of the hot path.llms.txt→nevermined.md).First of three PRs landing nevermined-io/nvm-monorepo#1684:
docs/use-cases/...) with the agent-discoverability page + Exa case study; links here.What's in the tutorial
src/server.tsPOST /purchase-key(x402-protected) +POST /search(api-key-gated). Uses the facilitator API directly (verifyPermissions/settlePermissions) — not thepaymentMiddlewarewrapper — so the verify → settle → provision flow is visible.src/store.tspayer → { apiKey, remainingQuota }map. The provider's own bookkeeping, independent of any Nevermined credit balance.src/search.tssrc/client.tssearch()so call-site narrowing is clean.src/llms-txt-template/llms.txtllms.txt(root of the docs site).src/llms-txt-template/nevermined.md{{LIKE_THIS}}.README.mdDesign decisions worth flagging for review
verify → settle → provision(notverify → provision → settle). Settle moves the money; provisioning last means a settle failure leaves the store untouched, so the customer is neither charged nor served. Code-review caught this and an earlier ordering would have leaked quota on settle failure.docs/solutions/api-providers.mdxindocs_mintlifycurrently shows the unsafe order — PR-B will reconcile.paymentMiddleware. The middleware hides the "do business logic between verify and settle" step inside the handler — but that step (provisioning the key) is exactly the educational point of this pattern, so we use the lower-level API explicitly./purchase-keycall triggers exactly one card charge. The README and template are careful never to say "buy credits and burn them" — that's a credit-plan mental model, not a PAYG one.Test plan
yarn installresolves cleanly.yarn build(tsc) clean.pm_id. Will run end-to-end:yarn agent+yarn client, exercise both first purchase and top-up paths, verify one card charge per/purchase-key.nevermined.mdtemplate renders correctly when placeholders are filled in.Notes for reviewers
/purchase-keyis acknowledged in a code comment but not guarded against in the tutorial. Production code should de-dup by token hash or settlement transaction.🤖 Generated with Claude Code