> For the complete documentation index, see [llms.txt](/llms.txt).

# Pay for an x402 API with delegation

In this guide, you use a buyer account to access API data from an x402 server by creating a [delegation](/smart-accounts-kit/development/reference/glossary#delegation)**Delegation** The ability for a MetaMask smart account to authorize another account to perform specific executions on its behalf. that authorizes token transfers on your behalf.

You use [createx402DelegationProvider](/smart-accounts-kit/reference/x402/#createx402delegationprovider)to set up an `x402Erc7710Client` with a delegation provider, register it with the x402 client, and use `wrapFetchWithPayment` to automatically handle payment when calling a protected API route.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

- [Install and set up the Smart Accounts Kit.](/smart-accounts-kit/get-started/install/)

## Steps[​](#steps "Direct link to Steps")

### 1. Install the dependencies[​](#1-install-the-dependencies "Direct link to 1. Install the dependencies")

- npm
- Yarn
- pnpm
- Bun

```
npm install @x402/core @x402/fetch @metamask/x402

```

```
yarn add @x402/core @x402/fetch @metamask/x402

```

```
pnpm add @x402/core @x402/fetch @metamask/x402

```

```
bun add @x402/core @x402/fetch @metamask/x402

```

### 2. Create a buyer account[​](#2-create-a-buyer-account "Direct link to 2. Create a buyer account")

Create an account to represent the buyer, the [delegator](/smart-accounts-kit/development/reference/glossary#delegator-account)**Delegator account** The account that creates and signs a delegation to grant limited authority to another account. who creates a delegation.

The delegator must be a [MetaMask smart account](/smart-accounts-kit/development/reference/glossary#metamask-smart-account)**MetaMask smart account** A smart contract account created using the Smart Accounts Kit that supports programmable behavior, flexible signing options, and ERC-7710 delegations.. Use the toolkit's [toMetaMaskSmartAccount](/smart-accounts-kit/reference/smart-account/#tometamasksmartaccount) method to create the buyer account.

Important

Fund the smart account with USDC for the requested payment.

- example.ts
- config.ts

```
import { Implementation, toMetaMaskSmartAccount } from '@metamask/smart-accounts-kit'
import { publicClient, buyerAccount } from './config'

export const buyerSmartAccount = await toMetaMaskSmartAccount({
  client: publicClient,
  implementation: Implementation.Hybrid,
  deployParams: [buyerAccount.address, [], [], []],
  deploySalt: '0x',
  signer: { account: buyerAccount },
})

```

```
import { createPublicClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { base as chain } from 'viem/chains'

export const publicClient = createPublicClient({
  chain,
  transport: http(),
})

export const buyerAccount = privateKeyToAccount('0x<BUYER_PRIVATE_KEY>')

```

### 3. Create an x402 ERC-7710 client[​](#3-create-an-x402-erc-7710-client "Direct link to 3. Create an x402 ERC-7710 client")

Create an `x402Erc7710Client` using [createx402DelegationProvider](/smart-accounts-kit/reference/x402/#createx402delegationprovider). The provider creates an [open](/smart-accounts-kit/development/reference/glossary#open-delegation)**Open delegation** A delegation that leaves the delegate unspecified, allowing any account to redeem it. [root delegation](/smart-accounts-kit/development/reference/glossary#root-delegation)**Root delegation** The first delegation in a chain, where an account delegates its own authority directly., signs it, and returns an ABI-encoded delegation chain when the x402 client needs to pay for a request.

The provider appends [redeemer](/smart-accounts-kit/reference/delegation/caveats/#redeemer), [allowedTargets](/smart-accounts-kit/reference/delegation/caveats/#allowedtargets), and [timestamp](/smart-accounts-kit/reference/delegation/caveats/#timestamp) [caveats](/smart-accounts-kit/development/reference/glossary#caveat)**Caveat** A restriction attached to a delegation that limits how delegated authority can be used. if not already present.

```
import { createx402DelegationProvider } from '@metamask/smart-accounts-kit/experimental'
import { x402Erc7710Client } from '@metamask/x402'

const erc7710Client = new x402Erc7710Client({
  delegationProvider: createx402DelegationProvider({
    account: buyerSmartAccount,
  }),
})

```

### 4. Register the client[​](#4-register-the-client "Direct link to 4. Register the client")

Register the ERC-7710 client with the x402 core client for all EVM networks. Create an HTTP client and a payment-aware `fetch` function using `wrapFetchWithPayment`.

```
import { x402Client, x402HTTPClient } from '@x402/core/client'
import { wrapFetchWithPayment } from '@x402/fetch'

const coreClient = new x402Client().register('eip155:*', erc7710Client)
const httpClient = new x402HTTPClient(coreClient)

const fetchWithPayment = wrapFetchWithPayment(fetch, httpClient)

```

### 5. Make the paid request[​](#5-make-the-paid-request "Direct link to 5. Make the paid request")

Call the protected endpoint using `fetchWithPayment`. It handles the x402 payment flow, calling your delegation provider to create an [open delegation](/smart-accounts-kit/development/reference/glossary#open-delegation)**Open delegation** A delegation that leaves the delegate unspecified, allowing any account to redeem it. when the server returns a `402` response.

```
const paidResponse = await fetchWithPayment('https://api.example.com/paid-endpoint', {
  method: 'GET',
})

```
