VantaClient

Client SDK for automatic 402 handling and payments.

Constructor

import { VantaClient } from '@vanta/client'

const client = new VantaClient({
  provider: ethersProvider,         // EIP-1193 provider
  autoApproveThreshold?: '0.01',    // Auto-approve payments under this
  onPaymentRequired?: (challenge) => Promise<boolean>,
  onPaymentSuccess?: (receipt) => void,
  onPaymentError?: (error) => void,
  tokenStorage?: TokenStorage,
  timeout?: 30000,
})

Methods

fetch(url, options?)

Drop-in replacement for fetch() with automatic 402 handling.

const response = await client.fetch('https://api.example.com/data', {
  method: 'GET',
  headers: { 'Content-Type': 'application/json' },
})
const data = await response.json()

request(options)

Advanced request method with full control.

const { data, receipt, token } = await client.request({
  url: 'https://api.example.com/data',
  method: 'POST',
  body: JSON.stringify({ query: 'test' }),
  autoApprove: true,
  maxRetries: 3,
})

getToken(baseUrl)

Get cached token for a base URL.

const token = client.getToken('https://api.example.com')

clearTokens(baseUrl?)

Clear cached tokens.

client.clearTokens() // Clear all
client.clearTokens('https://api.example.com') // Clear specific

Events

client.on('payment:required', (challenge) => { })
client.on('payment:success', (receipt) => { })
client.on('payment:error', (error) => { })
client.on('token:stored', (token) => { })
client.on('token:expired', (baseUrl) => { })