Skip to main content

Wallet API for Snaps

Dapps can install and communicate with Snaps using a subset of the Wallet JSON-RPC API. This page is a reference for those Snaps-specific methods.

note

See the Wallet JSON-RPC API interactive reference for the other methods dapps can call.

wallet_getSnaps

Returns the IDs of the dapp's permitted Snaps and some relevant metadata.

Returns

An object mapping the IDs of permitted Snaps to their metadata:

  • id: string - The ID of the Snap.
  • initialPermissions: string - The initial permissions of the Snap, which will be requested when the Snap is installed.
  • version: string - The version of the Snap.
  • enabled: boolean - Indicates whether the Snap is enabled.
  • blocked: boolean - Indicates whether the Snap is blocked.

Example

await window.ethereum.request({
"method": "wallet_getSnaps",
"params": []
});

wallet_requestSnaps

Requests permission for a dapp to communicate with the specified Snaps and attempts to install them if they're not already installed.

If the Snap version range is specified, MetaMask attempts to install a version of the Snap that satisfies the range. If a compatible version of the Snap is already installed, the request succeeds. If an incompatible version is installed, MetaMask attempts to update the Snap to the latest version that satisfies the range. The request succeeds if the Snap is successfully installed.

If the installation of any Snap fails, this method returns the error that caused the failure.

note

A dapp must call this method on Snap before calling wallet_snap or wallet_invokeSnap on the Snap.

Parameters

An object mapping the IDs of the requested Snaps to optional SemVer version ranges. The SemVer version ranges use the same semantics as npm package.json ranges.

Returns

An object mapping the IDs of permitted Snaps to their metadata:

  • id: string - The ID of the Snap.
  • initialPermissions: string - The initial permissions of the Snap, which will be request when the Snap is installed.
  • version: string - The version of the Snap.
  • enabled: boolean - Indicates whether the Snap is enabled.
  • blocked: boolean - Indicates whether the Snap is blocked.

Example

await window.ethereum.request({
"method": "wallet_requestSnaps",
"params": [
{
"npm:@metamask/example-snap": {},
"npm:fooSnap": {
"version": "^1.0.2"
}
}
]
});

wallet_snap

Calls the specified JSON-RPC API method of the specified Snap. The Snap must be installed and the dapp must have permission to communicate with the Snap, or the request is rejected. The dapp can install the Snap and request permission to communicate with it using wallet_requestSnaps.

This method is synonymous to wallet_invokeSnap.

Parameters

An object containing:

  • snapId: string - The ID of the Snap to invoke.
  • request: object - The JSON-RPC request object to send to the invoked Snap.

Returns

The result of the Snap method call.

Example

await window.ethereum.request({
"method": "wallet_snap",
"params": [
{
"snapId": "npm:@metamask/example-snap",
"request": {
"method": "hello"
}
}
]
});

wallet_invokeSnap

Calls the specified JSON-RPC API method of the specified Snap. The Snap must be installed and the dapp must have permission to communicate with the Snap, or the request is rejected. The dapp can install the Snap and request permission to communicate with it using wallet_requestSnaps.

This method is synonymous to wallet_snap.

Parameters

An object containing:

  • snapId: string - The ID of the Snap to invoke.
  • request: object - The JSON-RPC request object to send to the invoked Snap.

Returns

The result of the Snap method call.

Example

await window.ethereum.request({
"method": "wallet_invokeSnap",
"params": [
{
"snapId": "npm:@metamask/example-snap",
"request": {
"method": "hello"
}
}
]
});