Backend & Scripting Applications
Back-end applications are suitable for when you need to perform actions from your wallet or simply need to read data, rather than connecting to your user's wallets.
You can build back-end applications or scripts by using any of our SDKs:
Installation
- Javascript
- Python
- Go
- Unity
npm install @thirdweb-dev/sdk ethers@5
pip install thirdweb-sdk
go get github.com/thirdweb-dev/go-sdk
https://portal.thirdweb.com/gamingkit/setting-up/installation
Instantiating the SDK
There are two different kinds of SDK instances you can create:
- Read-only: Select a network and connect to it to read data from the blockchain.
- Read-Write: Connect a wallet using either a private key or signer/provider and write transactions directly from the wallet.
Read-only Connection
Provide the name of the chain you want to connect to as a string.
- Javascript
- Python
- Go
- Unity
import { ThirdwebSDK } from "@thirdweb-dev/sdk";
// Create a READ-ONLY instance of the ThirdwebSDK on the Polygon network
const sdk = new ThirdwebSDK("polygon"); // configure this to your network
from thirdweb import ThirdwebSDK
# You can create new READ-ONLY instance of the SDK to use by just passing in a network name
sdk = ThirdwebSDK("mumbai")
package main
import (
"fmt"
"github.com/thirdweb-dev/go-sdk/thirdweb"
)
func main() {
// Creates a new READ-ONLY instance of the SDK to get read-only data for your contracts, you can pass:
// - a chain name (mainnet, goerli, polygon, mumbai, avalanche, fantom)
// - a custom RPC URL
sdk, err := thirdweb.NewThirdwebSDK("mumbai", nil)
if err != nil {
panic(err)
}
// Now we can interact with the SDK, like displaying the connected chain ID
chainId, err := sdk.GetChainID()
if err != nil {
panic(err)
}
fmt.Println("New SDK instance create on chain", chainId)
}
using UnityEngine;
using Thirdweb; // 1. Import the ThirdwebSDK
public class DemoClass : MonoBehaviour
{
// 2. Create a ThirdwebSDK instance for us to use throughout the class
private ThirdwebSDK sdk;
void Start()
{
// 3. When the app starts, set up the ThirdwebSDK
// Below, we're setting up a read-only instance on the "goerli" test network.
sdk = new ThirdwebSDK("goerli");
}
}
From a Private key
This instantiates the SDK with write permissions directly from a wallet's private key.
If you expose your private key, anyone can access your wallet's funds. Please proceed carefully.
Ensure you store and access your private key securely.
- Check if you need to use a private key for your application.
- Never directly expose your private key in your source code.
- Never commit any file that may contain your private key to your source control.
- Never use a private key for a frontend (website/dapp) application.
If you are unsure how to securely store and access your private key, please do not proceed.
- Javascript
- Python
- Go
- Unity
import { ThirdwebSDK } from "@thirdweb-dev/sdk";
const sdk = ThirdwebSDK.fromPrivateKey(
// Learn more about securely accessing your private key: https://portal.thirdweb.com/sdk/set-up-the-sdk/securing-your-private-key
"<your-private-key-here>",
"polygon", // configure this to your network
);
from thirdweb import ThirdwebSDK
from thirdweb.types.nft import NFTMetadataInput
import os
# Learn more about securely accessing your private key: https://portal.thirdweb.com/sdk/set-up-the-sdk/securing-your-private-key
PRIVATE_KEY = "<your-private-key-here>"
# Now you can create a new instance of the SDK with your private key
sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, "mumbai")
package main
import (
"fmt"
"encoding/json"
"github.com/thirdweb-dev/go-sdk/thirdweb"
)
func main() {
// Learn more about securely accessing your private key: https://portal.thirdweb.com/sdk/set-up-the-sdk/securing-your-private-key
privateKey := "..."
// Instantiate the SDK with your privateKey
sdk, err := thirdweb.NewThirdwebSDK("mumbai", &thirdweb.SDKOptions{
PrivateKey: privateKey,
})
if err != nil {
panic(err)
}
}
// Unity does not support instantiating the SDK with a private key.
From a Signer / Provider
You can use a signer such as one from an Ethers Web3Provider to instantiate the SDK.
- Javascript
- Python
- Go
- Unity
import { ThirdwebSDK } from "@thirdweb-dev/sdk";
// Instantiate the ThirdwebSDK using the signer
// the signer variable comes from a signer you have previously created,
// or from our React SDK's useSigner hook.
const sdk = ThirdwebSDK.fromSigner(
signer,
"polygon", // configure this to your network
);
from thirdweb import ThirdwebSDK
# Now you can create a new instance of the SDK with the signer.
# Here, the signer variable comes from a signer you have previously created.
sdk = ThirdwebSDK("mumbai", signer)
// NOTE: Go does not support instantiating the SDK from a signer.
package main
import (
"fmt"
"encoding/json"
"github.com/thirdweb-dev/go-sdk/thirdweb"
)
func main() {
// Get your private key securely (preferably from an environment variable)
privateKey := "..."
// Instantiate the SDK with your privateKey
sdk, err := thirdweb.NewThirdwebSDK("mumbai", &thirdweb.SDKOptions{
PrivateKey: privateKey,
})
if err != nil {
panic(err)
}
}
// Unity does not support instantiating the SDK with a signer.
// Learn how to connect users wallets to your Unity app:
// https://portal.thirdweb.com/gamingkit/setting-up/connect-wallets
Configuring Chain
If you are using one of our default chains
, provide the name of the chain as a string.
View all pre-defined chains
- Ethereum:
"ethereum"
- Goerli:
"goerli"
- Polygon:
"polygon"
- Mumbai:
"mumbai"
- Arbitrum One:
"arbitrum"
- Arbitrum Goerli:
"arbitrum-goerli"
- Optimism:
"optimism"
- Optimism Goerli Testnet:
"optimism-goerli"
- Binance SmartChain:
"binance"
- Binance SmartChain Testnet:
"binance-testnet"
- Fantom Opera:
"fantom"
- Fantom Testnet:
"fantom-testnet"
- Avalanche C Chain:
"avalanche-fuji"
- Avalanche Fuji Testnet:
"avalanche-fuji-testnet"
- Localhost:
"localhost"
For non-default chains, import one of the 700+ chains available in the
@thirdweb-dev/chains
package.
You can install this package with:
- npm
- Yarn
npm install @thirdweb-dev/chains
yarn add @thirdweb-dev/chains
Then, import the chain you want to use and pass it as the argument, rather than a string.
import { ThirdwebSDK } from "@thirdweb-dev/sdk";
import { Gnosis } from "@thirdweb-dev/chains";
const sdk = ThirdwebSDK.fromPrivateKey(
// Learn more about securely accessing your private key: https://portal.thirdweb.com/sdk/set-up-the-sdk/securing-your-private-key
"<your-private-key-here>",
Gnosis,
);
Custom Chains
If your chain is not included in the @thirdweb-dev/chains
package,
you can provide the chain information yourself as an object.
import { ThirdwebSDK } from "@thirdweb-dev/sdk";
const sdk = ThirdwebSDK.fromPrivateKey(
// Learn more about securely accessing your private key: https://portal.thirdweb.com/sdk/set-up-the-sdk/securing-your-private-key
"<your-private-key-here>",
{
// === Required information for connecting to the network === \\
chainId: 59140, // Chain ID of the network
// Array of RPC URLs to use
rpc: ["<your-rpc-url-here>"],
// === Information for adding the network to your wallet (how it will appear for first time users) === \\
// Information about the chains native currency (i.e. the currency that is used to pay for gas)
nativeCurrency: {
decimals: 18,
name: "Consensys ETH",
symbol: "crETH",
},
shortName: "czkevm", // Display value shown in the wallet UI
slug: "consensys", // Display value shown in the wallet UI
testnet: true, // Boolean indicating whether the chain is a testnet or mainnet
chain: "ConsenSys", // Name of the network
name: "ConsenSys zkEVM Testnet", // Name of the network
},
);
Local Nodes
If you are running a local node using a tool such as
Hardhat or
Anvil, provide "localhost"
as the name.
You can then deploy or import your contracts to the dashboard to interact with them in your app.