feat(x402): usage-based billing & quota enforcement with metering middleware#96
Open
teethaking wants to merge 1 commit into
Open
feat(x402): usage-based billing & quota enforcement with metering middleware#96teethaking wants to merge 1 commit into
teethaking wants to merge 1 commit into
Conversation
|
@teethaking Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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
This PR adds metering, quota enforcement, and a usage/billing summary endpoint on top of the existing x402 payment middleware, enabling operators to run x402 as a paid service with per-key rate tiers and configurable over-quota policy. The existing x402 middleware is extended, not replaced.
Closes #82
What changed
src/x402/metering.ts(new)block— reject the request outright once quota is exhausted402— return HTTP 402 Payment Required, prompting the caller to pay per-call as normaloverage— allow the request through and bill the overage at a configured ratesrc/api/usage.ts(new)overagepolicy is active)Tests
block,402,overage)How to verify
block,402,overage) and assert the expected response/behaviorGET /api/usage(or configured route) and assert the summary matches actual metered usageChecklist
block,402, andoverage