@@ -9,10 +9,8 @@ import TabItem from "@theme/TabItem";
9
9
10
10
# Connect to MetaMask using JavaScript
11
11
12
- Get started with [ MetaMask SDK] ( https://github.com/MetaMask/metamask-sdk ) in your JavaScript dapp. You can set up the SDK in the following ways:
13
-
14
- - [ Quickstart template] ( #set-up-using-a-template ) - Clone the template to set up a JavaScript dapp.
15
- - [ Manual setup] ( #set-up-manually ) - Set up MetaMask SDK in an existing dapp.
12
+ Get started with MetaMask SDK in your JavaScript dapp.
13
+ You can [ download the quickstart template] ( #set-up-using-a-template ) or [ manually set up the SDK] ( #set-up-manually ) in an existing dapp.
16
14
17
15
<p align =" center " >
18
16
<a href =" https://metamask-javascript-demo.vercel.app/ " target =" _blank " >
@@ -34,25 +32,25 @@ Get started with [MetaMask SDK](https://github.com/MetaMask/metamask-sdk) in you
34
32
npx degit MetaMask/metamask-sdk-examples/quickstarts/javascript metamask-javascript
35
33
```
36
34
37
- > ` degit ` is a tool that enables cloning only the directory structure from a GitHub repository, without retrieving the entire repository.
38
-
39
35
2 . Navigate into the repository:
40
36
41
37
``` bash
42
38
cd metamask-javascript
43
39
```
44
40
45
41
<details >
46
- <summary >GitHub clone instead of degit? </summary >
42
+ <summary >Degit vs. Git clone </summary >
47
43
<div >
48
- Clone the MetaMask SDK examples repository and navigate into the ` quickstarts/javascript ` directory:
49
44
50
- ``` bash
51
- git clone https://github.com/MetaMask/metamask-sdk-examples
52
- cd metamask-sdk-examples/quickstarts/javascript
53
- ```
45
+ ` degit ` is a tool that enables cloning only the directory structure from a GitHub repository, without retrieving the entire repository.
46
+
47
+ Alternatively, you can use ` git clone ` , which will download the entire repository.
48
+ To do so, clone the MetaMask SDK examples repository and navigate into the ` quickstarts/javascript ` directory:
54
49
55
- > Note: _ this will download the entire repository._
50
+ ``` bash
51
+ git clone https://github.com/MetaMask/metamask-sdk-examples
52
+ cd metamask-sdk-examples/quickstarts/javascript
53
+ ```
56
54
57
55
< /div>
58
56
< /details>
@@ -89,11 +87,11 @@ The following are examples of using the SDK in various JavaScript environments:
89
87
<TabItem value="Web dapps">
90
88
91
89
```javascript
92
- import { MetaMaskSDK } from ' @metamask/sdk'
90
+ import { MetaMaskSDK } from " @metamask/sdk"
93
91
94
92
const MMSDK = new MetaMaskSDK({
95
93
dappMetadata: {
96
- name: ' Example JavaScript dapp' ,
94
+ name: " Example JavaScript dapp" ,
97
95
url: window.location.href,
98
96
// iconUrl: "https://mydapp.com/icon.png" // Optional
99
97
},
@@ -110,7 +108,7 @@ const MMSDK = new MetaMaskSDK({
110
108
<script>
111
109
const MMSDK = new MetaMaskSDK.MetaMaskSDK({
112
110
dappMetadata: {
113
- name: ' Example JavaScript dapp' ,
111
+ name: " Example JavaScript dapp" ,
114
112
url: window.location.href,
115
113
// iconUrl: "https://mydapp.com/icon.png" // Optional
116
114
},
@@ -137,13 +135,13 @@ Connect to MetaMask and get the provider for RPC requests:
137
135
const provider = MMSDK.getProvider ()
138
136
139
137
const accounts = await MMSDK.connect ()
140
- console .log (' Connected account:' , accounts[0 ])
138
+ console.log(" Connected account:" , accounts[0])
141
139
142
140
const result = await provider.request({
143
- method: ' eth_accounts' ,
141
+ method: " eth_accounts" ,
144
142
params: [],
145
143
})
146
- console .log (' eth_accounts result:' , result)
144
+ console.log(" eth_accounts result:" , result)
147
145
` ` `
148
146
149
147
`MMSDK.connect ()` handles cross-platform connection (desktop and mobile), including deeplinking.
@@ -155,7 +153,7 @@ Use `provider.request()` for arbitrary [JSON-RPC requests](/wallet/reference/jso
155
153
| Method | Description |
156
154
| --------------------------------------------------------------------------------- | -------------------------------------------------------- |
157
155
| [` connect()` ](../reference/sdk-methods.md#connect) | Triggers wallet connection flow |
158
- | [ ` connectAndSign({ msg: ' ...' }) ` ] ( ../reference/sdk-methods.md#connectandsign ) | Connects and prompts user to sign a message |
156
+ | [` connectAndSign({ msg: " ..." })` ](../reference/sdk-methods.md#connectandsign) | Connects and prompts user to sign a message |
159
157
| [` getProvider()` ](../reference/sdk-methods.md#getprovider) | Returns the provider object for RPC requests |
160
158
| [` provider.request({ method, params })` ](/wallet/reference/provider-api/# request) | Calls any Ethereum JSON‑RPC method |
161
159
| [Batched RPC](../guides/batch-requests.md) | Use ` metamask_batch` to group multiple JSON-RPC requests |
@@ -168,22 +166,22 @@ const accounts = await MMSDK.connect()
168
166
169
167
// 2. Connect and sign in one step
170
168
const signResult = await MMSDK.connectAndSign({
171
- msg: ' Sign in to Dapp ' ,
169
+ msg: " Sign in to the dapp " ,
172
170
})
173
171
174
172
// 3. Get provider for RPC requests
175
173
const provider = MMSDK.getProvider ()
176
174
177
175
// 4. Make an RPC request
178
176
const result = await provider.request({
179
- method: ' eth_accounts' ,
177
+ method: " eth_accounts" ,
180
178
params: [],
181
179
})
182
180
183
181
// 5. Batch multiple RPC requests
184
182
const batchResults = await provider.request({
185
- method: ' metamask_batch' ,
186
- params: [{ method: ' eth_accounts' }, { method: ' eth_chainId' }],
183
+ method: " metamask_batch" ,
184
+ params: [{ method: " eth_accounts" }, { method: " eth_chainId" }],
187
185
})
188
186
` ` `
189
187
0 commit comments