|
| 1 | +--- |
| 2 | +title: Embedded Wallets SDK for Node.js |
| 3 | +sidebar_label: Getting Started |
| 4 | +description: 'MetaMask Embedded Wallets SDK for Node.js | Backend Documentation' |
| 5 | +--- |
| 6 | + |
| 7 | +import TabItem from '@theme/TabItem' |
| 8 | +import Tabs from '@theme/Tabs' |
| 9 | +import SdkTroubleshootingIntro from '../_common/_sdk-troubleshooting-intro.mdx' |
| 10 | + |
| 11 | +## Overview |
| 12 | + |
| 13 | +The MetaMask Embedded Wallets Node.js SDK is a backend solution designed for server-side authentication and key management. This SDK enables seamless integration of Web3 authentication into backend applications, AI agents, and programmatic use cases. |
| 14 | + |
| 15 | +Unlike frontend SDKs, the Node.js SDK is **stateless and sessionless**, making it ideal for: |
| 16 | + |
| 17 | +- Backend AI agents |
| 18 | +- Server-side wallet operations |
| 19 | +- Programmatic blockchain interactions |
| 20 | +- Custodial wallet services, without key management and recovery worries. |
| 21 | + |
| 22 | +## Key Features |
| 23 | + |
| 24 | +- **Stateless Architecture**: No session management required |
| 25 | +- **Multi-Chain Support**: EVM chains, Solana, and other blockchains |
| 26 | +- **Custom Authentication**: Mandatory custom auth with single key share |
| 27 | +- **Private Key Access**: Direct access to private keys for any blockchain |
| 28 | +- **Backend-Optimized**: Designed specifically for server environments |
| 29 | + |
| 30 | +## Requirements |
| 31 | + |
| 32 | +- Node.js 18+ |
| 33 | +- Custom authentication setup (mandatory) |
| 34 | +- Web3Auth Dashboard project configuration |
| 35 | + |
| 36 | +## Installation |
| 37 | + |
| 38 | +Install the Web3Auth Node SDK |
| 39 | + |
| 40 | +```bash npm2yarn |
| 41 | +npm install --save @web3auth/node-sdk |
| 42 | +``` |
| 43 | + |
| 44 | +## Setup |
| 45 | + |
| 46 | +:::info Prerequisites |
| 47 | + |
| 48 | +Before you start, make sure you have: |
| 49 | + |
| 50 | +1. Registered on the [**Web3Auth Dashboard**](https://dashboard.web3auth.io/) |
| 51 | +2. Set up a project with a custom **Auth Connection** (mandatory for Node.js SDK) |
| 52 | + |
| 53 | +::: |
| 54 | + |
| 55 | +### 1. Custom Authentication Setup (Required) |
| 56 | + |
| 57 | +The Node.js SDK **only supports custom authentication**. You must create a custom auth connection in the Web3Auth Dashboard: |
| 58 | + |
| 59 | +1. Go to [Web3Auth Dashboard](https://dashboard.web3auth.io/) |
| 60 | +2. Select your project |
| 61 | +3. Navigate to **Authentication** → **Custom Connections** |
| 62 | +4. Click **Create connections** |
| 63 | +5. Configure your auth connection with your custom JWT details |
| 64 | + |
| 65 | +:::info |
| 66 | + |
| 67 | +You can refer to the [Custom JWT Setup](/embedded-wallets/authentication/custom-connections/custom-jwt/) guide to learn more. |
| 68 | + |
| 69 | +::: |
| 70 | + |
| 71 | +### 2. SDK Configuration |
| 72 | + |
| 73 | +Create a Web3Auth instance with your client ID, web3auth network name, and chain information: |
| 74 | + |
| 75 | +```javascript |
| 76 | +const web3auth = new Web3Auth({ |
| 77 | + clientId: 'YOUR_CLIENT_ID', // Get your Client ID from Web3Auth Dashboard |
| 78 | + web3AuthNetwork: 'sapphire_mainnet', // or 'sapphire_devnet' |
| 79 | +}) |
| 80 | +``` |
| 81 | + |
| 82 | +> The chain information is optional and will be used to setup the provider for connecting to the chain. If not provided, the first chain in the list will be used. |
| 83 | +
|
| 84 | +### 3. Initialize Web3Auth |
| 85 | + |
| 86 | +Initialize the Web3Auth instance during your application startup: |
| 87 | + |
| 88 | +```javascript |
| 89 | +await web3auth.init() |
| 90 | +``` |
| 91 | + |
| 92 | +### 4. Authenticate Users |
| 93 | + |
| 94 | +Use the connect method with your custom authentication parameters: |
| 95 | + |
| 96 | +```javascript |
| 97 | +const result = await web3auth.connect({ |
| 98 | + authConnectionId: 'YOUR_AUTH_CONNECTION_ID', // Your custom authentication connection name |
| 99 | + idToken: 'USER_ID_TOKEN', // JWT token from your auth system |
| 100 | +}) |
| 101 | +``` |
| 102 | + |
| 103 | +## Configuration Options |
| 104 | + |
| 105 | +<Tabs |
| 106 | + defaultValue="basic-config" |
| 107 | + values={[ |
| 108 | + { label: "Basic Configuration", value: "basic-config" }, |
| 109 | + { label: "Advanced Configuration", value: "advanced-config" }, |
| 110 | + ]} |
| 111 | +> |
| 112 | + |
| 113 | +<TabItem value="basic-config"> |
| 114 | + |
| 115 | +```javascript |
| 116 | +const { Web3Auth } = require('@web3auth/node-sdk') |
| 117 | + |
| 118 | +const web3auth = new Web3Auth({ |
| 119 | + clientId: 'YOUR_CLIENT_ID', |
| 120 | + web3AuthNetwork: 'sapphire_mainnet', // or 'sapphire_devnet' |
| 121 | +}) |
| 122 | + |
| 123 | +await web3auth.init() |
| 124 | +``` |
| 125 | + |
| 126 | +</TabItem> |
| 127 | + |
| 128 | +<TabItem value="advanced-config"> |
| 129 | + |
| 130 | +```javascript |
| 131 | +const { Web3Auth } = require('@web3auth/node-sdk') |
| 132 | + |
| 133 | +const web3auth = new Web3Auth({ |
| 134 | + clientId: 'YOUR_CLIENT_ID', |
| 135 | + web3AuthNetwork: 'sapphire_mainnet', // or 'sapphire_devnet' |
| 136 | + defaultChainId: '0x1', // or '0x89' for Polygon |
| 137 | + enableLogging: true, |
| 138 | + sessionTime: 3600, |
| 139 | +}) |
| 140 | + |
| 141 | +await web3auth.init() |
| 142 | +``` |
| 143 | + |
| 144 | +</TabItem> |
| 145 | + |
| 146 | +</Tabs> |
| 147 | + |
| 148 | +## Configuration Parameters |
| 149 | + |
| 150 | +<Tabs |
| 151 | + defaultValue="table" |
| 152 | + values={[ |
| 153 | + { label: "Table", value: "table" }, |
| 154 | + { label: "Interface", value: "interface" }, |
| 155 | + ]} |
| 156 | +> |
| 157 | + |
| 158 | +<TabItem value="table"> |
| 159 | + |
| 160 | +| Parameter | Type | Default | Description | |
| 161 | +| ----------------- | --------- | -------- | ---------------------------------------------------------------------------------------------------------------------------- | |
| 162 | +| `clientId` | `string` | Required | Your Web3Auth client ID | |
| 163 | +| `web3AuthNetwork` | `string` | Required | Network: 'sapphire_mainnet' or 'sapphire_devnet' | |
| 164 | +| `defaultChainId` | `string` | Optional | Chain ID to use for the default chain (e.g., '0x1' for Ethereum). If not provided, the first chain in the list will be used. | |
| 165 | +| `chains` | `object` | Optional | Chains to use for the authentication. It takes `Chains` as a value. | |
| 166 | +| `enableLogging` | `boolean` | Optional | Setting to true will enable logs. Default is false. | |
| 167 | +| `usePnPKey` | `boolean` | Optional | Setting to true will use the PnP key. Default is false. | |
| 168 | +| `useDKG` | `boolean` | Optional | Setting to true will use the DKG. Default is false. | |
| 169 | +| `checkCommitment` | `boolean` | Optional | Setting to true will check the commitment. Default is true. | |
| 170 | + |
| 171 | +</TabItem> |
| 172 | + |
| 173 | +<TabItem value="interface"> |
| 174 | + |
| 175 | +```javascript |
| 176 | +export interface Web3AuthOptions { |
| 177 | + /** |
| 178 | + * Client id for web3auth. |
| 179 | + * You can obtain your client id from the web3auth developer dashboard. |
| 180 | + * You can set any random string for this on localhost. |
| 181 | + */ |
| 182 | + clientId: string; |
| 183 | + |
| 184 | + /** |
| 185 | + * Web3Auth Network to use for login |
| 186 | + * @defaultValue mainnet |
| 187 | + */ |
| 188 | + web3AuthNetwork?: WEB3AUTH_NETWORK_TYPE; |
| 189 | + |
| 190 | + /** |
| 191 | + * multiple chain configurations, |
| 192 | + * only provided chains will be used |
| 193 | + */ |
| 194 | + chains?: CustomChainConfig[]; |
| 195 | + |
| 196 | + /** |
| 197 | + * default chain Id to use |
| 198 | + */ |
| 199 | + defaultChainId?: string; |
| 200 | + |
| 201 | + /** |
| 202 | + * setting to true will enable logs |
| 203 | + * |
| 204 | + * @defaultValue false |
| 205 | + */ |
| 206 | + enableLogging?: boolean; |
| 207 | + |
| 208 | + /** |
| 209 | + * setting this to true returns the same key as web sdk (i.e., plug n play key) |
| 210 | + * By default, this sdk returns SFAKey |
| 211 | + */ |
| 212 | + usePnPKey?: boolean; |
| 213 | + |
| 214 | + /** |
| 215 | + * set this to true when you wants keys/shares to be generated by a dkg network |
| 216 | + * |
| 217 | + * Default:- false for sapphire network and always true for legacy networks. |
| 218 | + * Legacy networks doesnt support non dkg flow. So this is always true for legacy networks. |
| 219 | + */ |
| 220 | + useDKG?: boolean; |
| 221 | + |
| 222 | + /** |
| 223 | + * setting this to true will check the commitment of the shares |
| 224 | + * |
| 225 | + * @defaultValue true |
| 226 | + */ |
| 227 | + checkCommitment?: boolean; |
| 228 | +} |
| 229 | +``` |
| 230 | + |
| 231 | +</TabItem> |
| 232 | + |
| 233 | +</Tabs> |
| 234 | + |
| 235 | +## Usage |
| 236 | + |
| 237 | +```javascript |
| 238 | +// focus-start |
| 239 | +const { Web3Auth } = require('@web3auth/node-sdk') |
| 240 | +// focus-end |
| 241 | + |
| 242 | +// Dashboard Registration |
| 243 | +const clientId = |
| 244 | + 'BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ' |
| 245 | + |
| 246 | +// Auth Connection |
| 247 | +const authConnectionId = 'w3a-node-demo' |
| 248 | + |
| 249 | +// focus-start |
| 250 | +const web3auth = new Web3Auth({ |
| 251 | + clientId, |
| 252 | + web3AuthNetwork: 'sapphire_mainnet', |
| 253 | +}) |
| 254 | + |
| 255 | +await web3auth.init() |
| 256 | +// focus-end |
| 257 | + |
| 258 | +const privateKey = await fs.readFile('privateKey.pem', 'utf8') |
| 259 | + |
| 260 | +var idToken = jwt.sign( |
| 261 | + { |
| 262 | + sub: '9fcd68c4-af50-4dd7-adf6-abd12a13cb32', |
| 263 | + name: 'Web3Auth DevRel Team', |
| 264 | + |
| 265 | + aud: 'urn:api-web3auth-io', // -> to be used in Custom Authentication as JWT Field |
| 266 | + iss: 'https://web3auth.io', // -> to be used in Custom Authentication as JWT Field |
| 267 | + iat: Math.floor(Date.now() / 1000), |
| 268 | + exp: Math.floor(Date.now() / 1000) + 60 * 60, |
| 269 | + }, |
| 270 | + privateKey, |
| 271 | + { algorithm: 'RS256', keyid: '2ma4enu1kdvw5bo9xsfpi3gcjzrt6q78yl0h' } |
| 272 | +) |
| 273 | + |
| 274 | +console.log('\x1b[33m%s\x1b[0m', 'JWT Token:', idToken) |
| 275 | + |
| 276 | +// focus-start |
| 277 | +const result = await web3auth.connect({ |
| 278 | + authConnectionId, |
| 279 | + idToken, |
| 280 | +}) |
| 281 | +// focus-end |
| 282 | +``` |
0 commit comments