Define a typed tool with Zod and your Vercel AI SDK agent can buy gift cards from the edge, with every purchase policy-checked and logged.
import { tool } from "ai";
import { z } from "zod";
const BASE = "https://api.agentrefills.com";
const H = { "Content-Type": "application/json",
Authorization: "Bearer sk_live_..." };
export const buyGiftcard = tool({
description: "Buy a gift card; amountMinor is the value in cents.",
parameters: z.object({
brandId: z.string(),
amountMinor: z.number().int(),
}),
execute: async ({ brandId, amountMinor }) => {
const r = await fetch(`${BASE}/v1/purchases`, {
method: "POST", headers: H,
body: JSON.stringify({ brand_id: brandId,
amount_minor: amountMinor, currency: "USD" }),
});
if (!r.ok) throw await r.json();
return r.json();
},
});
Daily and monthly limits plus a brand allowlist, enforced on every purchase.
Purchases above your threshold are approved by a human in Slack before money moves.
If the policy engine is unreachable or undecided, the purchase is denied, never allowed.
Codes are encrypted at rest, released once, access-logged, and never written to logs or traces.
An idempotency key plus a unique payment-per-task constraint make a double charge structurally impossible.
Every order is reconstructable from an OpenTelemetry trace plus a double-entry ledger, with webhooks on completion.
Send a gift card for a survey, referral or milestone.
Resolve a ticket with a gift card instead of a slow reversal.
Send a scheduled thank-you or birthday gift, within a budget.
Buy stored-value for a team, capped and reconciled.
Declare a tool() with a Zod schema whose execute() POSTs to the AgentRefills /v1/purchases endpoint, then pass it to generateText or streamText. The purchase is policy-checked and budget-capped on the server, so the edge function only ever holds a task id.
Every agent gets its own API key scoped to per-agent daily and monthly spend caps and a brand allowlist. The policy engine enforces the caps on every purchase and fails closed, so the agent can never exceed its budget even if the policy service is unreachable.
Set an approval threshold. Any purchase above it is parked as APPROVAL_REQUIRED and a human is pinged in Slack to approve or reject before money moves. The task resumes automatically once approved.
Start in the sandbox with zero real spend, then flip to production.
Get API keys