Protect an endpoint
Put billable work behind Hilt Pay API without building the HTTP 402, settlement, receipt, entitlement, and atomic-consumption loop yourself. The TypeScript SDK’sprotectEndpoint wrapper works with Fetch-compatible server handlers, including Next.js App Router routes, workers, and modern Node server frameworks.
Before you start
You need:- a server-side Hilt API key with
access:readandaccess:write - an active Hilt Pay API product using
solana_usdc usage_unitset to the thing you sell, such asrequestusage_units_per_paymentset to the number of calls each payment buys
Install the SDK
Wrap the route
handler is served as billable work. The Hilt API key remains on your server and Hilt does not proxy the request body or paid result.
Python services
The Python SDK exposes the same decision boundary without requiring a specific web framework:allowed is false. Run billable work only when allowed is true.
Send stable identities
Every caller must send:- one customer header:
X-Hilt-Customer-Id,X-Customer-Id, orX-Agent-Id - one request header:
Idempotency-Key,X-Request-Id, orX-Hilt-Request-Id
What the wrapper does
For every request, the wrapper:- resolves the customer and stable request ID
- atomically consumes the configured usage units
- returns HTTP
402with Hilt’sPAYMENT-REQUIREDheader when usage is missing - accepts the buyer’s
PAYMENT-SIGNATUREon the retry - settles the Hilt-bound Solana USDC payment
- atomically consumes usage and then runs your handler
- relays Hilt’s
PAYMENT-RESPONSEheader with the protected result
402 appears only when the balance is exhausted or access is otherwise unavailable.
Buyer behavior
The buyer or buyer agent calls your endpoint, validates the returned payment terms, signs the advertised Solana USDC transaction, and retries your endpoint withPAYMENT-SIGNATURE.
The buyer does not receive your Hilt API key and does not call Hilt’s protected settlement or entitlement routes directly.
Use the agent-to-agent micropayments guide for the buyer implementation, payment-term validation, and complete runtime explanation. The runnable TypeScript example contains both the protected resource and buyer agent.
Safety boundaries
- Keep
HILT_API_KEYin server-side secrets. - Never call billable work before atomic consumption succeeds.
- Never authorize from a raw transaction hash, wallet claim, or client-side flag.
- Preserve the same request ID across retries of one operation.
- Make side effects inside the paid handler idempotent by request ID because a client may retry after receiving the result.
- Use Hilt sandbox validation before live settlement.

