Magic Quickstart

Magic Quickstart

Magic makes it easy to authenticate users and integrate them into your Web3 dApps quickly.

The quickstart options below show how you can use Magic in a Next.js app to connect to an EVM-based testnet, but you can follow a similar flow with Magic’s mobile SDKs or integrate with any of the 25+ blockchains supported by Magic.

How do you want to get started?

Integrating Magic into a new or existing app is as simple as installing the SDK, initializing Magic with your Magic Publishable API Key and chosen blockchain, and authenticating your users with magic.wallet.connectWithUI(). The sections below go through each of these steps one at a time.

If you want to jump straight into the code, check out this Github Repository or tinker directly in the browser with the Codesandbox version.

#Install the SDK

Navigate to your project directory and install the Magic SDK as a project dependency.

01npm install magic-sdk

#Get your Magic Publishable API Key

Grab your Magic Publishable API Key from your Magic Dashboard. If you haven’t already, you’ll need to sign up for a free developer account.

#Initialize Magic

To get started, simply initialize an instance of Magic with your Publishable API Key and your choice of blockchain. Then initialize your chosen blockchain library, like Web3.js or Ethers.js, with the RPC provider from the Magic instance.

01const magic = new Magic(process.env.NEXT_PUBLIC_MAGIC_API_KEY, {
02  network: {
03    rpcUrl: "<>",
04    chainId: 11155111,
05  },
08const web3 = new Web3((magic as any).rpcProvider))

The above code snippet initializes Magic with a public Sepolia Testnet URL. You can point the instance to a different chain by modifying the URL and Chain ID. Magic seamlessly supports over 25 different blockchains.

#Authenticate your users

Authenticating your users is as easy as calling magic.wallet.connectWithUI(). This will display Magic's Login UI. Magic will handle authentication using Email OTP with no additional code needed from your app. You log your users out by calling magic.user.logout().

In addition to the flow provided by the Login UI, you can customize a Dedicated Wallet to use a variety of authentication options like SMS login, OAuth through popular social login providers, and more.

#Display the authenticated user’s wallet

Display the authenticated user’s wallet with magic.wallet.showUI(). This will show the user’s wallet using Magic’s Widget UI.

#Interact with the network

Magic integrates with all popular blockchain libraries so that you don’t have to change anything about how you interact with the blockchain. For example, if you’re using Ethereum or other EVM chains, you can get the user’s wallet address or sign and send transactions the same way you normally would using Web3.js or Ethers.js.

01function sendEth(amount: number, recipientAddress: string) {
02  const senderAddress = web3.eth.getAccounts().[0]
04  const txnParams = {
05    from: senderAddress,
06    to: recipientAddress,
07    value: web3.utils.toWei(amount, "ether"),
08    gas: 21000,
09  }
11  web3.eth
12    .sendTransaction(txnParams as any)
13    .on("transactionHash", (txHash: string) => {
14      console.log("Transaction hash:", txHash)
15    })
16    .then((receipt: any) => {
17      console.log("Transaction receipt:", receipt)
18    })

#Customize Your App

This application uses our Dedicated Wallet. The Dedicated Wallet meets the widest variety of needs while still being incredibly simple to implement. In addition to the baked-in Login UI, it has plenty of customization options, supports social login through providers like GitHub and Discord, allows for enterprise multi-factor authentication, and more.

#Next Steps

We have a suite of resources to help developers and companies with a wide variety of use cases. Below are some ideas to get you started, but feel free to browse our documentation or reach out with specific questions.