Wallgent
Guides

Add Payments with the Vercel AI SDK

Define Wallgent operations as Vercel AI SDK tools for use with generateText and streamText.

Prerequisites

1. Install

npm install @wallgent/sdk ai @ai-sdk/openai

2. Define Tools

The Vercel AI SDK uses tool() to define typed tool schemas. Wrap Wallgent operations as tools:

import { Wallgent } from "@wallgent/sdk"
import { tool } from "ai"
import { z } from "zod"

const wg = new Wallgent("wg_test_your_key_here")

const checkBalance = tool({
  description: "Check the available balance of a wallet",
  parameters: z.object({
    walletId: z.string().describe("The wallet ID to check"),
  }),
  execute: async ({ walletId }) => {
    const balance = await wg.wallets.getBalance(walletId)
    return balance
  },
})

const sendPayment = tool({
  description: "Send a payment between two wallets",
  parameters: z.object({
    from: z.string().describe("Source wallet ID"),
    to: z.string().describe("Destination wallet ID"),
    amount: z.string().describe("Amount in USD"),
    description: z.string().optional().describe("Payment description"),
  }),
  execute: async ({ from, to, amount, description }) => {
    const txn = await wg.payments.send({ from, to, amount, description })
    return txn
  },
})

3. Use with generateText

import { generateText } from "ai"
import { openai } from "@ai-sdk/openai"

const result = await generateText({
  model: openai("gpt-4o"),
  tools: { checkBalance, sendPayment },
  maxSteps: 5,
  prompt: "Check the ops wallet balance, then send $25 to the vendor wallet.",
})

console.log(result.text)

4. Next.js API Route

// app/api/agent/route.ts
import { streamText } from "ai"
import { openai } from "@ai-sdk/openai"

export async function POST(req: Request) {
  const { messages } = await req.json()

  const result = streamText({
    model: openai("gpt-4o"),
    tools: { checkBalance, sendPayment },
    maxSteps: 5,
    messages,
  })

  return result.toDataStreamResponse()
}

The tools work with generateText, streamText, and any AI SDK-compatible model provider (OpenAI, Anthropic, Google, etc.).


Next Steps

On this page