You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: sdk/connect/javascript.md
+58-53Lines changed: 58 additions & 53 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,23 +11,24 @@ import TabItem from "@theme/TabItem";
11
11
12
12
Get started with MetaMask SDK in your JavaScript dapp.
13
13
14
+
## Prerequisites
15
+
16
+
-[Node.js](https://nodejs.org/) version 19 or later installed.
17
+
- A package manager installed, such as [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm), [Yarn](https://yarnpkg.com/), [pnpm](https://pnpm.io/installation), or [bun](https://bun.sh/).
18
+
-[MetaMask](https://metamask.io/) installed in your browser or on mobile.
19
+
20
+
14
21
## Steps
15
22
16
23
### 1. Install the SDK
17
24
18
-
Install the SDK in an existing JavaScript project using npm or Yarn:
25
+
Install the SDK in an existing JavaScript project:
19
26
20
-
```bash
27
+
```bash npm2yarn
21
28
npm install @metamask/sdk
22
29
```
23
30
24
-
or
25
-
26
-
```
27
-
yarn add @metamask/sdk
28
-
```
29
-
30
-
### 2. Use the SDK
31
+
### 2. Initialize the SDK
31
32
32
33
The following are examples of using the SDK in various JavaScript environments:
33
34
@@ -39,22 +40,12 @@ import { MetaMaskSDK } from "@metamask/sdk"
These examples configure the SDK with the following options:
83
74
84
-
The SDK accepts several [configuration options](../reference/sdk-options.md) when initializing.
85
-
For example:
75
+
-[`dappMetadata`](../reference/sdk-options.md#dappmetadata) - Ensures trust by showing your dapp's `name`, `url`, and `iconUrl` during connection.
76
+
77
+
-[`infuraAPIKey`](../reference/sdk-options.md#infuraapikey) - Enables read-only RPC and load‑balancing.
78
+
79
+
### 3. Connect and use provider
80
+
81
+
Connect to MetaMask and get the provider for RPC requests:
86
82
87
83
```javascript
88
-
constMMSDK=newMetaMaskSDK({
89
-
// Required - Your dapp's info
90
-
dappMetadata: {
91
-
name:"Your Dapp Name",
92
-
url:window.location.href,
93
-
},
94
-
95
-
// Optional - Infura API key for read-only RPC calls
96
-
infuraAPIKey:process.env.INFURA_API_KEY,
97
-
98
-
// Optional - Customize modal display
99
-
headless:false,
84
+
constprovider=MMSDK.getProvider()
85
+
86
+
constaccounts=awaitMMSDK.connect()
87
+
console.log("Connected account:", accounts[0])
88
+
89
+
constresult=awaitprovider.request({
90
+
method:"eth_accounts",
91
+
params: [],
100
92
})
93
+
console.log("eth_accounts result:", result)
101
94
```
102
95
103
-
### 4. Call common methods
96
+
`MMSDK.connect()` handles cross-platform connection (desktop and mobile), including deeplinking.
97
+
98
+
Use `provider.request()` for arbitrary [JSON-RPC requests](/wallet/reference/json-rpc-methods) like `eth_chainId` or `eth_getBalance`, or for [batching requests](../guides/batch-requests.md) via `metamask_batch`.
0 commit comments