Getting Started

In this quickstart guide, we'll create a basic script to generate a wallet using a private key and send a transaction. We’ll assume you already have a TypeScript project created.

  • Install the SDK

    To get started, install the thirdweb SDK using your preferred package manager.

    npm i thirdweb
  • Create a thirdweb client

    Get a project secret key from https://thirdweb.com/create-api-key by creating a new project and add it to your .env.

    THIRDWEB_SECRET_KEY=[YOUR PROJECT SECRET KEY]

    Create a thirdweb client in your script.

    import { createThirdwebClient } from "thirdweb";
    const client = createThirdwebClient({
    // use `secretKey` for server side or script usage
    secretKey: process.env.THIRDWEB_SECRET_KEY,
    });
  • Read Contract State

    A client is all you need to start reading blockchain data.

    • Import the extensions you want to use.
    • Define a contract with getContract at a given address and chain.
    • Call the extension function to read the data.
    import { getContract } from "thirdweb";
    import { sepolia } from "thirdweb/chains";
    // 1. import the extension you want to use
    import { getNFTs } from "thirdweb/extensions/erc1155";
    // 2. get the contract
    const contract = getContract({
    client,
    address: "0x1234...",
    chain: sepolia,
    });
    // 3. call the extension function
    const nfts = await getNFTs({
    contract,
    });
    console.log(nfts);
  • Generate a wallet

    To perform transactions from your script, you'll need an account. Let's generate a wallet from your project secret key.

    import { inAppWallet } from "thirdweb/wallets";
    // create or access a wallet
    const wallet = inAppWallet();
    const account = await wallet.connect({
    client: TEST_CLIENT,
    strategy: "backend", // we use backend strategy to generate a wallet from a secret key
    walletSecret: "my-test-wallet-secret", // use this secret to access the same wallet across multiple scripts
    });
    // Get the address of the account
    const address = account.address;
    console.log("Connected as", address);
  • Read Wallet Data

    Let's read balance of the account you just created, you'll need funds to perform transactions.

    import { getWalletBalance } from "thirdweb/wallets";
    // Get the balance of the account
    const balance = await getWalletBalance({
    account,
    chain: sepolia,
    });
    console.log("Balance:", balance.displayValue, balance.symbol);
  • Send a transaction

    With the account created and funded, you can now send a transaction.

    • Import the extension you want to use.
    • Define a contract with getContract at a given address and chain.
    • Call the extension function to prepare the transaction.
    • Send the transaction.
    import { getContract, sendTransaction } from "thirdweb";
    // 1. Import the extension you want to use
    import { transfer } from "thirdweb/extensions/erc20";
    // 2. Define the contract
    const contract = getContract({
    client,
    address: "0x1234...",
    chain: sepolia,
    });
    // 3. Call the extension function to prepare the transaction
    const transaction = transfer({
    contract,
    to: "0x1234...",
    amount: "0.01",
    });
    // 4. Send the transaction
    const result = await sendTransaction({
    transaction,
    account,
    });
    console.log("Transaction hash:", result.transactionHash);

    You can also call generic contract functions using the prepareContractCall function by just specifying the solidity method signature you want to call. The arguments will be automatically inferred based on the method signature.

    import {
    getContract,
    prepareContractCall,
    sendTransaction,
    } from "thirdweb";
    import { sepolia } from "thirdweb/chains";
    import { toWei } from "thirdweb/utils";
    // 1. Define the contract
    const contract = getContract({
    client,
    address: "0x1234...",
    chain: sepolia,
    });
    // 2. Prepare the transaction
    const transaction = prepareContractCall({
    contract,
    // Pass the method signature that you want to call
    method: "function mintTo(address to, uint256 amount)",
    // and the params for that method
    // Their types are automatically inferred based on the method signature
    params: ["0x123...", toWei("100")],
    });
    // 3. Send the transaction
    const result = await sendTransaction({
    transaction,
    account,
    });
    console.log("Transaction hash:", result.transactionHash);
  • Conclusion

    You've now learned the basics of how to use the thirdweb SDK to read and write to the blockchain. You can now start building your own applications and explore the full potential of the SDK.

    View the full SDK reference.