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

# Recurring x402 payments

In this guide, you set up recurring x402 payments by requesting an ERC-20 periodic [Advanced Permissions](/smart-accounts-kit/development/reference/glossary#advanced-permissions)**Advanced Permissions** Fine-grained, wallet execution permissions that dapps can request from MetaMask extension users. Based on ERC-7715. permission from a user.

For example, a user gives your agent permission to spend up to 10 USDC per week. Later, when the agent calls an x402 endpoint, it checks the price, uses the granted permission, and pays.

## Prerequisites

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

## Steps

### 1. Install the dependencies

**npm**

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

```

**Yarn**

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

```

**pnpm**

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

```

**Bun**

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

```

### 2. Set up a Wallet Client

Set up a Wallet Client using Viem's [createWalletClient](https://viem.sh/docs/clients/wallet) function. Use this client to interact with MetaMask.

Extend the Wallet Client with `erc7715ProviderActions` to enable [Advanced Permissions](/smart-accounts-kit/development/reference/glossary#advanced-permissions)**Advanced Permissions** Fine-grained, wallet execution permissions that dapps can request from MetaMask extension users. Based on ERC-7715. requests.

```typescript
import { createWalletClient, custom } from 'viem'
import { erc7715ProviderActions } from '@metamask/smart-accounts-kit/actions'

const walletClient = createWalletClient({
  transport: custom(window.ethereum),
}).extend(erc7715ProviderActions())

```

### 3. Set up an agent account

The session account can be either a [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. or an [EOA](/smart-accounts-kit/development/reference/glossary#externally-owned-account-eoa)**Externally owned account (EOA)** A private-key-controlled account with no built-in programmable execution logic.. This example uses an EOA as the session account.

```typescript
import { privateKeyToAccount } from 'viem/accounts'

const sessionAccount = privateKeyToAccount('0x...')

```

### 4. Request Advanced Permissions

Request Advanced Permissions from the user with the Wallet Client's `requestExecutionPermissions` action.

In this example, you request an [ERC-20 periodic permission](/smart-accounts-kit/development/guides/advanced-permissions/use-permissions/erc20-token/#erc-20-periodic-permission)with a weekly allowance of 10 USDC. This creates a recurring payment budget that your agent can store and reuse for x402 API calls.

See the [requestExecutionPermissions](/smart-accounts-kit/development/reference/advanced-permissions/wallet-client/#requestexecutionpermissions)API reference for more information.

```ts
import { base as chain } from 'viem/chains'
import { parseUnits } from 'viem'

// USDC address on Base.
const tokenAddress = '0x...'

const currentTime = Math.floor(Date.now() / 1000)
const expiry = currentTime + 60 * 60 * 24 * 30 // Permission expires in 30 days.

const grantedPermissions = await walletClient.requestExecutionPermissions([
  {
    chainId: chain.id,
    expiry,
    to: sessionAccount.address,
    permission: {
      type: 'erc20-token-periodic',
      data: {
        tokenAddress,
        periodAmount: parseUnits('10', 6),
        periodDuration: 604800,
        startTime: currentTime,
        justification:
          'Permission for agent to spend up to 10 USDC every week for making x402 API calls',
      },
      isAdjustmentAllowed: false,
    },
  },
])

```

### 5. Create an x402 ERC-7710 client

Create an `x402Erc7710Client` using [createx402DelegationProvider](/smart-accounts-kit/development/reference/x402/#createx402delegationprovider).

The provider creates an [open redelegation](/smart-accounts-kit/development/reference/glossary#open-redelegation)**Open redelegation** A redelegation with no specific delegate, allowing any account to redeem inherited permissions.from the session account using the granted permission. The facilitator can then redeem the redelegated permission context for x402 settlement. The provider uses the session account's address as the redelegation's `from` address.

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

const permission = grantedPermissions[0]

const erc7710Client = new x402Erc7710Client({
  delegationProvider: createx402DelegationProvider({
    account: sessionAccount,
    parentPermissionContext: permission.context,
  }),
})

```

### 6. Register the client

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

```ts
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)

```

### 7. Make the paid request

Call the protected endpoint using `fetchWithPayment`. The x402 payment flow calls your delegation provider to create an open redelegation when the server returns a `402` response.

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

```

You can reuse the same weekly granted permission for additional protected routes and providers in your agent flow. Your agent continues paying until the weekly cap is reached, then resumes after the next weekly period starts.
