A Next.js starter for building AI agents that can pay and get paid over Lightning, deployed on Vercel.
- Buy-side payments:
agentFetchwithzbdPayL402Invoicefor fetching L402-protected APIs and paying automatically - Sell-side payments:
withPaymentRequiredmiddleware for putting your own API endpoints behind a Lightning paywall - AI SDK integration: Streaming chat endpoint with optional paid context fetched via L402
- Balance endpoint: Check your ZBD wallet balance at any time
- Token caching:
VercelKVTokenCachefor persisting L402 tokens across serverless invocations
- Node.js >= 22
- A Vercel account
- A ZBD API key — get one at developer.zbdpay.com
- An OpenAI API key (optional, only needed for the chat demo)
-
Clone and install
git clone https://github.com/zbdpay/vercel-template.git cd vercel-template npm install -
Initialize your ZBD wallet
npx @zbdpay/agent-wallet init --key YOUR_ZBD_API_KEY
This creates a Lightning address for your agent and prints it out. Save it for the next step.
-
Add Vercel KV
vercel kv add
Or create a KV store from the Vercel dashboard and link it to your project. This is used to cache L402 tokens between requests.
-
Configure environment variables
cp .env.example .env.local
Then open
.env.localand fill in:Variable Description ZBD_API_KEYYour ZBD API key ZBD_LIGHTNING_ADDRESSThe Lightning address from step 2 KV_REST_API_URLFrom Vercel KV dashboard KV_REST_API_TOKENFrom Vercel KV dashboard OPENAI_API_KEYOptional, for the chat endpoint -
Run the dev server
npm run dev
-
Deploy
vercel deploy
With the dev server running on http://localhost:3000:
# Check your wallet balance
curl http://localhost:3000/api/balance
# Buy-side: fetch a paid L402 API (agent pays automatically)
curl -X POST http://localhost:3000/api/agent \
-H "Content-Type: application/json" \
-d '{"url": "https://some-l402-api.example.com/data"}'
# Sell-side: hit the premium endpoint (returns 402 Payment Required)
curl -v http://localhost:3000/api/premium
# Chat with AI (streaming)
curl -X POST http://localhost:3000/api/chat \
-H "Content-Type: application/json" \
-d '{"messages": [{"role": "user", "content": "Hello!"}]}'vercel-template/
├── app/
│ ├── layout.tsx # Root layout
│ ├── page.tsx # Interactive demo UI
│ └── api/
│ ├── agent/route.ts # Buy-side: fetch paid APIs via L402
│ ├── balance/route.ts # Wallet balance check
│ ├── chat/route.ts # AI SDK streaming + optional paid context
│ └── premium/route.ts # Sell-side: L402 paywall (100 sats)
├── lib/
│ ├── kv-token-cache.ts # VercelKV-backed TokenCache implementation
│ └── zbd.ts # Configured agentFetch helper
├── .env.example
├── next.config.ts
├── package.json
└── tsconfig.json