Skip to content

LangChain Adapter

The LangChain adapter makes @pqsafe/agent-pay available as a DynamicStructuredTool in any LangChain agent. For a complete step-by-step walkthrough including agent wiring, approval gates, and production patterns, see the LangChain Integration Guide.

Quick reference

import {
generateKeyPair,
createSpendEnvelope,
createSignedEnvelope,
executeAgentPayment,
buildLedgerRecord,
submitToLedger,
} from '@pqsafe/agent-pay'
import { DynamicStructuredTool } from 'langchain/tools'
import { z } from 'zod'
const { secretKey } = await generateKeyPair()
const signedEnvelope = createSignedEnvelope(
createSpendEnvelope({
agentId: 'my-langchain-agent',
maxAmount: 500,
currency: 'USD',
allowedRails: ['airwallex'],
allowedRecipients: ['vendor.com'],
validUntil: new Date(Date.now() + 3_600_000),
}),
secretKey
)
export const pqsafePayTool = new DynamicStructuredTool({
name: 'pqsafe_pay',
description: 'Execute a PQSafe-authorized payment. recipient: vendor.com. amount: USD ≤ 500.',
schema: z.object({
recipient: z.string(),
amount: z.number(),
memo: z.string(),
}),
func: async ({ recipient, amount, memo }) => {
const result = await executeAgentPayment(signedEnvelope, { recipient, amount, memo })
await submitToLedger(buildLedgerRecord(signedEnvelope, result))
return `Payment ${result.status}. TX: ${result.txId}. Rail: ${result.rail}.`
},
})

Full LangChain Integration Guide