|
| 1 | +--- |
| 2 | +description: Methods reference for MetaMask SDK. |
| 3 | +keywords: [SDK, method, methods, dapp] |
| 4 | +toc_max_heading_level: 2 |
| 5 | +--- |
| 6 | + |
| 7 | +# SDK methods |
| 8 | + |
| 9 | +MetaMask SDK provides several convenience methods for connecting to and interacting with MetaMask, including the following. |
| 10 | + |
| 11 | +## `connect` |
| 12 | + |
| 13 | +Connects to MetaMask and requests account access. |
| 14 | + |
| 15 | +### Returns |
| 16 | + |
| 17 | +A promise that resolves to an array of account addresses. |
| 18 | + |
| 19 | +### Example |
| 20 | + |
| 21 | +```javascript |
| 22 | +const accounts = await sdk.connect(); |
| 23 | +console.log("Connected accounts:", accounts); |
| 24 | +``` |
| 25 | + |
| 26 | +## `connectAndSign` |
| 27 | + |
| 28 | +Connects to MetaMask and signs a message in a single operation. |
| 29 | + |
| 30 | +### Parameters |
| 31 | + |
| 32 | +- `msg`: `string` - The message to sign. |
| 33 | + |
| 34 | +### Returns |
| 35 | + |
| 36 | +A promise that resolves to the signature of the signed message. |
| 37 | + |
| 38 | +### Example |
| 39 | + |
| 40 | +```javascript |
| 41 | +const signature = await sdk.connectAndSign({ |
| 42 | + msg: "Hello from my dapp!" |
| 43 | +}); |
| 44 | +console.log("Signature:", signature); |
| 45 | +``` |
| 46 | + |
| 47 | +## `connectWith` |
| 48 | + |
| 49 | +Connects to MetaMask and executes a specific [JSON-RPC method](/wallet/reference/json-rpc-methods). |
| 50 | + |
| 51 | +### Parameters |
| 52 | + |
| 53 | +- `rpc`: `object` - The RPC method to execute |
| 54 | + - `method`: `string` - The RPC method name |
| 55 | + - `params`: `any[]` - The parameters for the RPC method |
| 56 | + |
| 57 | +### Returns |
| 58 | + |
| 59 | +A promise that resolves to the result of the RPC call. |
| 60 | + |
| 61 | +### Example |
| 62 | + |
| 63 | +```javascript |
| 64 | +const result = await sdk.connectWith({ |
| 65 | + rpc: { |
| 66 | + method: "eth_getBalance", |
| 67 | + params: ["0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6", "latest"] |
| 68 | + } |
| 69 | +}); |
| 70 | +console.log("Balance:", result); |
| 71 | +``` |
| 72 | + |
| 73 | +## `getProvider` |
| 74 | + |
| 75 | +Returns the active Ethereum provider object. |
| 76 | + |
| 77 | +### Returns |
| 78 | + |
| 79 | +The active provider, or undefined if no provider is found. |
| 80 | + |
| 81 | +### Example |
| 82 | + |
| 83 | +```javascript |
| 84 | +const provider = sdk.getProvider(); |
| 85 | +if (provider) { |
| 86 | + // Use the provider for RPC calls |
| 87 | + const accounts = await provider.request({ |
| 88 | + method: "eth_requestAccounts" |
| 89 | + }); |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## `terminate` |
| 94 | + |
| 95 | +Terminates the MetaMask connection, switching back to the injected provider if connected via extension. |
| 96 | + |
| 97 | +:::note |
| 98 | +The `disconnect()` SDK method is deprecated. |
| 99 | +Use `terminate()` instead. |
| 100 | +::: |
| 101 | + |
| 102 | +### Example |
| 103 | + |
| 104 | +```javascript |
| 105 | +await sdk.terminate(); |
| 106 | +console.log("Connection terminated"); |
| 107 | +``` |
0 commit comments