Getting Started
Use From npm
Install the published packages:
npm install @payai-sh/core @payai-sh/x402
Run Locally
Clone the repository and install dependencies:
npm install
Run checks:
npm run check
Run the demo:
npm run demo
The demo simulates:
- An agent requests a paid API.
- The API returns
402 Payment Required. - PayAI parses the quote and checks the grant.
- A mock x402 payer adds payment headers.
- The API returns the paid resource.
- PayAI records a receipt.
Packages
@payai-sh/core
Core policy primitives:
SpendingGrantPaymentQuotePaymentDecisionPaymentReceiptMemorySpendingLedgerevaluatePaymentcreateReceipt
@payai-sh/x402
Fetch wrapper for x402-like paid resources:
- detects
402responses - parses payment requirements from JSON response body or payment headers
- runs PayAI policy before payment
- calls a provided payer adapter
- records receipts after successful retry
Minimal Example
import { createPayAIFetch } from "@payai-sh/x402";
const payaiFetch = createPayAIFetch({
grant,
payer: async (request) => {
request.paymentHeaders.set("X-Payment", "mock-payment");
return request.fetch(request.paymentHeaders);
}
});
const response = await payaiFetch("https://data.example.com/report", {
purpose: "research"
});