New: Introducing PHP and Composer Support.Read the Announcement
Socket
Book a DemoInstallSign in
Socket

@metamask/connect-multichain

Package Overview
Dependencies
Maintainers
3
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@metamask/connect-multichain

Multichain package for MetaMask Connect

latest
Source
npmnpm
Version
0.5.3
Version published
Weekly downloads
41
-84.05%
Maintainers
3
Weekly downloads
 
Created
Source

@metamask/connect-multichain

Core multichain connectivity library for MetaMask Connect SDK. Supports multiple blockchain networks including EVM chains (Ethereum, Polygon, etc.) and non-EVM chains (Solana, etc.).

This package provides support for connecting to MetaMask and using the CAIP Multichain API which agnostically supports making requests to multiple blockchain ecosystems simultaneously.

Installation

yarn add @metamask/connect-multichain

or

npm install @metamask/connect-multichain

Quick Start

import {
  createMultichainClient,
  getInfuraRpcUrls,
} from '@metamask/connect-multichain';

const client = await createMultichainClient({
  dapp: {
    name: 'My DApp',
    url: 'https://mydapp.com',
  },
  api: {
    supportedNetworks: {
      // use the `getInfuraRpcUrls` helper to generate a map of Infura RPC endpoints
      ...getInfuraRpcUrls(INFURA_API_KEY),
      // or specify your own CAIP Chain ID to rpc endpoint mapping
      'eip155:1': 'https://mainnet.example.io/rpc',
      'eip155:137': 'https://polygon-mainnet.example.io/rpc',
    },
  },
});

// Connect to MetaMask with specific chain scopes
await client.connect(['eip155:1', 'eip155:137'], []);

// Invoke methods on specific chains
const blockNumber = await client.invokeMethod({
  scope: 'eip155:1',
  request: {
    method: 'eth_blockNumber',
    params: [],
  },
});

Usage

Browser (Web)

import { createMultichainClient } from '@metamask/connect-multichain';

const client = await createMultichainClient({
  dapp: {
    name: 'My Web DApp',
    url: 'https://mydapp.com',
    iconUrl: 'https://mydapp.com/icon.png',
  },
  api: {
    supportedNetworks: {
      'eip155:1': 'https://mainnet.infura.io/v3/YOUR_KEY',
    },
  },
  ui: {
    preferExtension: true, // Prefer browser extension over mobile QR
    showInstallModal: false, // Show modal to install extension
    headless: false, // Set true for custom QR UI
  },
});

Node.js

import { createMultichainClient } from '@metamask/connect-multichain';

const client = await createMultichainClient({
  dapp: {
    name: 'My Node App',
    url: 'https://mydapp.com',
  },
  api: {
    supportedNetworks: {
      'eip155:1': 'https://mainnet.infura.io/v3/YOUR_KEY',
    },
  },
});

// Connect will display QR code in terminal
await client.connect(['eip155:1'], []);

React Native

import { Linking } from 'react-native';
import { createMultichainClient } from '@metamask/connect-multichain';

const client = await createMultichainClient({
  dapp: {
    name: 'My RN App',
    url: 'https://mydapp.com',
  },
  api: {
    supportedNetworks: {
      'eip155:1': 'https://mainnet.infura.io/v3/YOUR_KEY',
    },
  },
  mobile: {
    preferredOpenLink: (deeplink) => {
      Linking.openURL(deeplink);
    },
  },
});

TypeScript

This package is written in TypeScript and includes full type definitions. No additional @types package is required.

API Reference

createMultichainClient(options)

Factory function to create a new Multichain SDK instance.

Parameters

OptionTypeRequiredDescription
dapp.namestringYesName of your dApp
api.supportedNetworksRpcUrlsMapYesMap of CAIP chain IDs to RPC URLs
dapp.urlstringNoURL of your dApp
dapp.iconUrlstringNoIcon URL for your dApp
dapp.base64IconstringNoBase64-encoded icon (alternative to iconUrl)
storageStoreClientNoCustom storage adapter
ui.factoryBaseModalFactoryNoCustom modal factory
ui.headlessbooleanNoRun without UI (for custom QR implementations)
ui.preferExtensionbooleanNoPrefer browser extension (default: true)
ui.showInstallModalbooleanNoShow installation modal
mobile.preferredOpenLink(deeplink: string, target?: string) => voidNoCustom deeplink handler
mobile.useDeeplinkbooleanNoUse metamask:// instead of universal links
analytics.integrationTypestringNoIntegration type for analytics
transport.extensionIdstringNoCustom extension ID
transport.onNotification(notification: unknown) => voidNoNotification handler
debugbooleanNoEnable debug logging

Returns

Promise<MetaMaskConnectMultichain> - A fully initialized SDK instance.

const client = await createMultichainClient({
  dapp: { name: 'My DApp', url: 'https://mydapp.com' },
  api: {
    supportedNetworks: {
      'eip155:1': 'https://mainnet.infura.io/v3/KEY',
      'eip155:137': 'https://polygon-mainnet.infura.io/v3/KEY',
    },
  },
});

MetaMaskConnectMultichain

The main SDK class extending MultichainCore.

Methods

connect(scopes, caipAccountIds, sessionProperties?, forceRequest?)

Connects to MetaMask with specified chain scopes.

Parameters

NameTypeRequiredDescription
scopesScope[]YesArray of CAIP-2 chain identifiers to request permission for
caipAccountIdsCaipAccountId[]YesArray of CAIP-10 account identifiers to request (pass [] if no specific accounts should be requested)
sessionPropertiesSessionPropertiesNoAdditional session properties
forceRequestbooleanNoForce a new connection request even if already connected

Returns

Promise<void>

await client.connect(
  ['eip155:1', 'eip155:137'], // Chain scopes to request
  ['eip155:1:0x...'], // Specific accounts to request
);
disconnect()

Disconnects from the wallet and cleans up resources.

Parameters

None.

Returns

Promise<void>

await client.disconnect();
invokeMethod(options)

Invokes an RPC method on a specific chain.

Parameters

NameTypeRequiredDescription
options.scopeScopeYesThe CAIP-2 chain identifier to invoke the method on
options.request.methodstringYesThe RPC method name
options.request.paramsunknown[]NoThe method parameters

Returns

Promise<Json> - The result of the RPC method call.

const result = await client.invokeMethod({
  scope: 'eip155:1',
  request: {
    method: 'eth_getBalance',
    params: ['0x...', 'latest'],
  },
});

Properties

PropertyTypeDescription
statusConnectionStatusConnection status ( 'loaded', 'pending', 'connecting', 'connected', 'disconnected')
providerMultichainApiClientMultichain API client
transportExtendedTransportActive transport layer

Events

// Session changes
client.on('wallet_sessionChanged', (session) => {
  console.log('Session updated:', session);
});

// QR code display (for custom UI)
client.on('display_uri', (uri) => {
  console.log('Display QR code:', uri);
});

// Connection state changes
client.on('stateChanged', (status) => {
  console.log('Status:', status);
});

MultichainCore

Abstract base class providing core multichain functionality.

Methods

on(event, handler)

Registers an event handler.

Parameters

NameTypeRequiredDescription
eventstringYesThe event name to listen for
handlerFunctionYesThe callback function to invoke when the event is emitted

Returns

void

off(event, handler)

Removes an event handler.

Parameters

NameTypeRequiredDescription
eventstringYesThe event name to stop listening for
handlerFunctionYesThe callback function to remove

Returns

void

emit(event, args)

Emits an event to all registered handlers.

Parameters

NameTypeRequiredDescription
eventstringYesThe event name to emit
argsanyNoArguments to pass to the event handlers

Returns

void

Utilities

getInfuraRpcUrls(infuraApiKey)

Generates Infura RPC URLs for common networks keyed by CAIP Chain ID.

Parameters

NameTypeRequiredDescription
infuraApiKeystringYesYour Infura API key

Returns

A map of hex chain IDs to Infura RPC URLs. See https://docs.metamask.io/services

import { getInfuraRpcUrls } from '@metamask/connect-multichain';

const rpcUrls = getInfuraRpcUrls('YOUR_INFURA_KEY');
// {
//   'eip155:1': 'https://mainnet.infura.io/v3/YOUR_KEY',
//   'eip155:137': 'https://polygon-mainnet.infura.io/v3/YOUR_KEY',
//   'eip155:11155111': 'https://sepolia.infura.io/v3/YOUR_KEY',
//   ...
// }

Errors

The package exports various error classes for handling specific error conditions:

  • ProtocolError - Base protocol error
  • StorageError - Storage operation errors
  • RpcError - RPC request errors
import { ProtocolError } from '@metamask/connect-multichain';

try {
  await client.connect(['eip155:1'], []);
} catch (error) {
  if (error instanceof ProtocolError) {
    console.log('Protocol error:', error.code, error.message);
  }
}

Headless Mode

For custom QR code implementations, use headless mode:

const client = await createMultichainClient({
  dapp: { name: 'My DApp' },
  api: { supportedNetworks: { 'eip155:1': 'https://...' } },
  ui: { headless: true },
});

// Listen for QR code URIs
client.on('display_uri', (uri) => {
  // Display your custom QR code with this URI
  displayMyCustomQRCode(uri);
});

await client.connect(['eip155:1'], []);

Standards

Contributing

This package is part of a monorepo. Instructions for contributing can be found in the monorepo README.

License

MIT

Keywords

MetaMask

FAQs

Package last updated on 05 Feb 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts