How to Build a Pay-per-Use API with Bitcoin
A paid API needs metering, pricing, request authorization, and payment reconciliation in addition to a Bitcoin wallet. This is a developer design pattern, not a claim that a ready-made AI-agent integration or SDK ships with the engine.
Architecture
A safe arrangement is: AI agent → authenticated application tool → deterministic authorization and policy service → WAB Engine HTTP JSON API → Bitcoin Core. The separate Factory handles wallet birth; the engine restores and operates wallets. The application owns business rules, user permissions, order records, and agent limits.
Implementation plan
Issue a quote and payment reference, accept an on-chain payment through the engine, verify it server-side, then grant a finite usage allowance in the application database.
- Define a typed input schema with explicit integer satoshis, wallet/network identifier, business reference, and requested action.
- Authenticate the caller and verify the operation is allowed for this particular agent and task.
- Validate amounts, address/network compatibility, and destination against trusted records; do not treat model-generated text as authority.
- Persist the business operation and its state before any irreversible payment action.
- For SEND, use the engine's documented idempotency mechanism and reconcile ambiguous results before retrying.
- Observe the actual transaction state, apply a written confirmation policy, and update the application ledger once.
- Test failure and recovery paths before mainnet use.
Pseudocode: policy before payment
request = parse_typed_agent_request()
assert authenticated(request.actor)
assert permitted(request.actor, request.operation)
assert request.amount_sat is a positive integer
assert network_and_destination_valid(request)
reservation = atomic_budget_reserve(request)
if reservation denied: reject
if requires_human_approval(request): await approval
result = trusted_backend_call_to_existing_engine_contract(request)
reconcile_result_and_reservation(result)
This is pseudocode, not a copy-paste WAB endpoint or a statement that every policy function exists today.
Wallet fee implications
For wallets governed by the documented signed 10% inclusive SEND law, the recipient receives 90% of the sender total; miner fee comes first from the remaining 10% and software fee receives the rest. An unaffordable miner fee or dust output causes failure rather than an additional charge. An agent spending budget should account for the sender total, not only the recipient amount.
Failure modes and verification
For tiny calls, batching or prepaid credits may be more economical than a separate on-chain payment per request. Test duplicate callbacks, reorgs, wrong-chain addresses, pending payments, unavailable Bitcoin Core, and conflicting agent requests. Keep credentials outside the LLM and clients. No external spending cap is effective if the agent can bypass it with direct engine access.
Scope
The existing engine is CURL POST/JSON-first and uses Bitcoin Core-backed on-chain operations. Do not assume Lightning, L402, a native WATCH endpoint, OpenAPI publication, or a released WAB CLI policy module without separate implementation evidence.
Further reading
Related LEARN: How to Test AI Agent Bitcoin Payments on Testnet
https://thamer.org/learn/test-ai-agent-bitcoin-payments-testnet
Related BLOG: What Is WALLET AI BITCOIN? A Developer’s Introduction
https://thamer.org/blog/what-is-wallet-ai-bitcoin-developer-introduction
Related BLOG: Can AI Agents Buy Computing Power with Bitcoin?
https://thamer.org/blog/can-ai-agents-buy-compute-with-bitcoin
