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
| Name | Type | Required | Description |
|---|---|---|---|
account | MaybeDeferred<Account> | Yes | The Viem Account that signs the delegationDelegation The ability for a MetaMask smart account to authorize another account to perform specific executions on its behalf.. |
environment | MaybeDeferred<SmartAccountsEnvironment> | No | Environment to resolve the smart contracts for the current chain. If omitted, resolved automatically from the chain ID in the payment requirements. |
from | MaybeDeferred<Hex> | No | The 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. |
salt | MaybeDeferred<Hex> | No | The salt for generating the delegation hash. The default is a random 32-byte value. |
caveats | MaybeDeferred<Caveats> | No | CaveatsCaveat 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. |
parentPermissionContext | MaybeDeferred<PermissionContext> | No | Parent 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.. |
expirySeconds | MaybeDeferred<number> | No | Relative expiry in seconds. Adds a timestamp caveat if no tighter constraint exists. |
redeemers | MaybeDeferred<RedeemersConfig> | No | Constrains 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
- Delegation
- Redelegation
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,
}),
})
import { createx402DelegationProvider } from '@metamask/smart-accounts-kit/experimental'
const parentPermissionContext = '0x...' // Previously stored/issued permission context.
const delegationProvider = createx402DelegationProvider({
account,
parentPermissionContext,
})
parseEip155ChainId
Parses an EIP-155 CAIP network identifier into a numeric chain ID.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
network | string | Yes | EIP-155 CAIP network identifier. For example, eip155:1. |
Example
import { parseEip155ChainId } from '@metamask/smart-accounts-kit/experimental'
// Returns 137
const chainId = parseEip155ChainId('eip155:137')