@ucash/vercel-ai
v0.1.1
Published
Gate Vercel AI SDK routes behind a U.CASH Pay payment (crypto + cards). Non-custodial.
Downloads
49
Maintainers
Readme
@ucash/vercel-ai
Helpers to gate Vercel AI SDK routes behind a U.CASH Pay payment - charge per prompt/generation with crypto or cards. Non-custodial.
Install
npm install @ucash/vercel-aiGate a route
// app/api/chat/route.ts
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { withPayment } from '@ucash/vercel-ai';
export async function POST(req: Request) {
const { paid, messages } = await req.json();
const r = await withPayment({
paid, // client proves it already paid (e.g. a paid transaction id)
charge: { cloud: process.env.UCASH_CLOUD_TOKEN!, amount: '0.05', title: 'AI generation' },
run: async () => {
const result = await streamText({ model: openai('gpt-4o-mini'), messages });
return result.toDataStreamResponse();
},
});
if ((r as any).status === 402) {
return Response.json({ paymentUrl: (r as any).paymentUrl }, { status: 402 });
}
return r as Response;
}The client: on a 402, send the user to paymentUrl; after they pay, retry with paid: true (and verify the transaction via your webhook before trusting it).
Set up your pay.u.cash account
- Sign up at pay.u.cash, verify email.
- Set receive addresses under Settings -> Addresses.
- Create a store at Account -> Stores -> + Add Store; use the Store Cloud Token as
cloud. (Set the webhook URL + verify before trustingpaid.)
License
MIT.
