thirdweb

Code Examples

Last updated:

December 3, 2021

Easy copy and paste snippets to use our TypeScript SDK

First, we'll set up our local environment, which we'll be the same for all snippets in the page, and then feel free to go and pick up the one you need.

Set up local environment

The starting code for this examples can be found on this GitHub repository, so feel free to clone this instead of doing it yourself.

Let's first create a folder and a TypeScript file, you can create these manually or do it from the terminal.

mkdir typescript-sdk cd typescript-sdk touch sdk.ts

Install required dependencies

Next we'll initialize the project and install the required dependencies:

  • @3rdweb/sdk - thirdweb TypeScript SDK.
  • ethers - will give us very useful utils.
  • dotenv - to hide our private key.
  • typescript - since this is a TypeScript project
  • @types/node - typing for node as a dev dependency.
  • tslib - utility package
npm init -y npm install @3rdweb/sdk ethers dotenv npm install --save-dev typescript @types/node ts-node tslib

Import the SDK

// Importing libraries import { ThirdwebSDK } from "@3rdweb/sdk"; import { ethers } from "ethers"; import dotenv from "dotenv"; //Importing private key dotenv.config();

We also need to create a .env file with our PRIVATE_KEY, you can check how to get your MetaMask private key, on this guide.

Don't commit this PRIVATE_KEY to GitHub or anywhere else, don't use it on Repl.it, you will lose your funds, we recommend to use a Dev wallet instead of your real wallet.

.env
PRIVATE_KEY=<your-private-key-here>

You should create a .gitignore file and add this line before commiting to GitHub.

.gitignore
node_modules .env

Instantiate the SDK

On ethers.getDefaultProvider, you need to add whatever RPC you're using, in this example, we're using Polygon Mumbai Testnet.

//Instantiate 3rdweb SDK const sdk = new ThirdwebSDK( new ethers.Wallet( // Your wallet private key process.env.PRIVATE_KEY as string, // RPC URL, we'll use Polygon Mumbai ethers.getDefaultProvider("https://rpc-mumbai.maticvigil.com") ) );

Replace <MODULE_ADDRESS> with the module address found in the dashboard.


Ready to build your first web3 app? Get early access & add web3 features to your project today.

Contents

Set up local environment

Install required dependencies

Import the SDK

Instantiate the SDK