Getting Started
To get started, install the thirdweb package into your Python project:
pip install thirdweb-sdk
You will require an API key to use thirdweb’s infrastructure services with the SDK. If you haven’t created a key yet you can do so for free from the thirdweb dashboard.
Instantiating the SDK
Once you have all the necessary dependencies, you can follow the following setup steps to get started with the SDK read-only functions:
from thirdweb import ThirdwebSDK
from thirdweb.types import SDKOptions
# Get your secret key from the thirdweb api keys dashboard
SECRET_KEY = "..."
# You can create a new instance of the SDK to use by passing in a network name and your api key
sdk = ThirdwebSDK("mumbai", options=SDKOptions(secret_key=SECRET_KEY))
The SDK supports the mainnet
, rinkeby
, goerli
, polygon
, mumbai
, fantom
, and avalanche
networks.
Alternatively, if you want to use your own custom RPC URL, you can pass in the RPC URL directly as follows:
from thirdweb import ThirdwebSDK
# Set your RPC_URL
RPC_URL = "https://rpc-mainnet.matic.network"
# And now you can instantiate the SDK with it
sdk = ThirdwebSDK(RPC_URL)
Working With Contracts
Once you instantiate the SDK, you can use it to access your thirdweb contracts. You can use the SDK's contract getter functions like get_token
, get_edition
, get_nft_collection
, and get_marketplace
to get the respective SDK contract instances. To use an NFT Collection contract for example, you can do the following.
# Add your NFT Collection contract address here
NFT_COLLECTION_ADDRESS = "0x.."
# And you can instantiate your contract with just one line
nft_collection = sdk.get_nft_collection(NFT_COLLECTION_ADDRESS)
# Now you can use any of the read-only SDK contract functions
nfts = nft_collection.get_all()
print(nfts)
Signing Transactions
⚠️ Never commit private keys to file tracking history, or your account could be compromised.
Meanwhile, if you want to use write functions as well and connect a signer, you can use the following setup:
from thirdweb import ThirdwebSDK
from thirdweb.types.nft import NFTMetadataInput
import os
# Get your secret key from the thirdweb api keys dashboard
SECRET_KEY = "..."
# This PRIVATE KEY is coming from your environment variables. Make sure to never put it in a tracked file or share it with anyone.
PRIVATE_KEY = os.environ.get("PRIVATE_KEY")
# Now you can create a new instance of the SDK with your private key
sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, "mumbai", options=SDKOptions(secret_key=SECRET_KEY))
# Instantiate a new NFT Collection contract as described above.
NFT_COLLECTION_ADDRESS = "0x.."
nft_collection = sdk.get_nft_collection(NFT_COLLECTION_ADDRESS)
# Now you can use any of the SDK contract functions including write functions
nft_collection.mint(NFTMetadataInput.from_json({ "name": "Cool NFT", "description": "Minted with the Python SDK!" }))