For AI agents: a documentation index is available at /llms.txt. A markdown version of this page is available at the same URL with .md appended (or via Accept: text/markdown).
Skip to main content

x402 API reference

The following API methods are related to x402 to create payments using delegationDelegation The ability for a MetaMask smart account to authorize another account to perform specific executions on its behalf..

createx402DelegationProvider

Creates a delegation provider function to be used with x402Erc7710Client.

The provider creates an open delegationOpen delegation A delegation that leaves the delegate unspecified, allowing any account to redeem it., signs it, and returns an ABI-encoded delegation chain as a hex string. The provider internally appends redeemer, payee, and expiry caveatsCaveat A restriction attached to a delegation that limits how delegated authority can be used. when the existing caveats, or the root delegationRoot delegation The first delegation in a chain, where an account delegates its own authority directly. doesn't have it.

When both redeemers.addresses and facilitatorAddresses from the server's payment requirements are provided, the provider resolves redeemers as the intersection of the two lists. If only one is provided, that list is used. An error is thrown if the resolved list is empty.

Parameters

NameTypeRequiredDescription
accountMaybeDeferred
<Account>
YesThe Viem Account that signs the delegationDelegation The ability for a MetaMask smart account to authorize another account to perform specific executions on its behalf..
environmentMaybeDeferred
<SmartAccountsEnvironment>
NoEnvironment to resolve the smart contracts for the current chain. If omitted, resolved automatically from the chain ID in the payment requirements.
fromMaybeDeferred
<Hex>
NoThe address that is granting the delegationDelegation The ability for a MetaMask smart account to authorize another account to perform specific executions on its behalf.. The default is account.
saltMaybeDeferred
<Hex>
NoThe salt for generating the delegation hash. The default is a random 32-byte value.
caveatsMaybeDeferred
<Caveats>
NoCaveatsCaveat A restriction attached to a delegation that limits how delegated authority can be used. that further refine the authority granted by the delegationDelegation The ability for a MetaMask smart account to authorize another account to perform specific executions on its behalf.. redeemer, allowedTargets, and timestamp caveats are auto-appended if not already present.
parentPermissionContextMaybeDeferred
<PermissionContext>
NoParent chain as Hex or as decoded Delegation values (leaf first). Use this when creating a redelegationRedelegation A delegation that passes on authority granted by a previous delegation..
expirySecondsMaybeDeferred
<number>
NoRelative expiry in seconds. Adds a timestamp caveat if no tighter constraint exists.
redeemersMaybeDeferred
<RedeemersConfig>
NoConstrains the addresses that are allowed to redeem the delegationDelegation The ability for a MetaMask smart account to authorize another account to perform specific executions on its behalf.. Use this to restrict redemption to specific facilitators.

Example

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

const account = privateKeyToAccount(privateKey)

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

parseEip155ChainId

Parses an EIP-155 CAIP network identifier into a numeric chain ID.

Parameters

NameTypeRequiredDescription
networkstringYesEIP-155 CAIP network identifier. For example, eip155:1.

Example

import { parseEip155ChainId } from '@metamask/smart-accounts-kit/experimental'

// Returns 137
const chainId = parseEip155ChainId('eip155:137')