Skip to main content

Mint an NFT Collection with Python

· 3 min read
Raza Zaidi

In this guide, you'll learn how to mint your own NFT using Python! We'll be using the thirdweb dashboard and python SDK to complete this project.

Let's get started!

Dashboard setup

The first thing we'll need to do is head to the thirdweb dashboard.

You'll need to connect your wallet, so if you don't have a one, check out our guide on how to Create a MetaMask wallet.

Click "Deploy new contract" in the top right corner, then "Deploy Now" on the NFT Collection contract.

Set up your NFT Collection with some metadata, including image, name, description, and royalty settings.

Create a Python file

Go ahead and create a new Python file. Let's call it python_nft.py and open it in your favorite code editor. We'll use Visual Studio.

Next up, use the following code in your terminal to install the SDK!

# thirdweb-sdk
pip install thirdweb-sdk

Setup the SDK

Now that we have our environment setup with the thirdweb python SDK, we can get started. In our python_nft.py file, we can use the following code:

python_nft.py
from thirdweb import ThirdwebSDK
from thirdweb.types.nft import NFTMetadataInput

# Learn more about securely accessing your private key: https://portal.thirdweb.com/web3-sdk/set-up-the-sdk/securing-your-private-key
PRIVATE_KEY = "<your-private-key-here>"

# Set the network you want to operate on, or add your own RPC URL here
NETWORK = "mumbai"

# Finally, you can create a new instance of the SDK to use
sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, NETWORK)
danger

Ensure you store and access your private key securely.

  • Never commit any file that may contain your private key to your source control.

Learn more about securely accessing your private key.

Define the NFT Collection contract

Now that we have an SDK instance setup, we can get the interface for our NFT Collection!

Our NFT Collection that we deployed from the dashboard has its own unique address that can be used to identify it, which can be found next to your contract name on the thirdweb dashboard.

We can now use this address to use our NFT Collection with the SDK with the following code:

python_nft.py
# Instantiate a new NFT Collection contract as described above.
NFT_COLLECTION_ADDRESS = "0x.."
nft_collection = sdk.get_nft_collection(NFT_COLLECTION_ADDRESS)

Time to mint the NFT!

Now we can mint a new NFT with the sdk! The arguments passed over here are the same as minting an NFT inside the dashboard. If you want to familiarize yourself with the process, check out the dashboard!

python_nft.py
# 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!",
"image": "ipfs://QmdFeKxt6FJUNvaGgzYuYNRbpNWyHxP2PFzjsgPf1eD2Jf"
}))

Results

Get the balance to check it out or head over to the dashboard.

python_nft.py
# check your balance to check if you minted an nft!
print(nft_collection.balance())

So the only thing left is to run our code! Open a terminal and paste the following:

python3 python_nft.py

That's it!

You have minted your own NFT using thirdweb and the Python SDK, you should be proud!