T402 API Reference - v2.8.0
    Preparing search index...

    Function wrapAxiosWithPayment

    • Wraps an Axios instance with t402 payment handling.

      This function adds an interceptor to automatically handle 402 Payment Required responses by creating and sending payment headers. It will:

      1. Intercept 402 responses
      2. Parse the payment requirements
      3. Create a payment header using the configured t402HTTPClient
      4. Retry the request with the payment header

      Parameters

      • axiosInstance: AxiosInstance

        The Axios instance to wrap

      • client: t402Client

        Configured t402Client instance for handling payments

      Returns AxiosInstance

      The wrapped Axios instance that handles 402 responses automatically

      import axios from 'axios';
      import { wrapAxiosWithPayment, t402Client } from '@t402/axios';
      import { ExactEvmScheme } from '@t402/evm';
      import { privateKeyToAccount } from 'viem/accounts';

      const account = privateKeyToAccount('0x...');
      const client = new t402Client()
      .register('eip155:*', new ExactEvmScheme(account));

      const api = wrapAxiosWithPayment(axios.create(), client);

      // Make a request that may require payment
      const response = await api.get('https://api.example.com/paid-endpoint');

      If no schemes are provided

      If the request configuration is missing

      If a payment has already been attempted for this request

      If there's an error creating the payment header