Skip to main content

Create your own NFT marketplace with Python

· 3 min read
Raza Zaidi

In this guide, we'll create a marketplace smart contract using Python!

Intro

In a marketplace, users can list NFTs for sale. Opensea is an example of a marketplace.

The first thing we need to do is head over to the dashboard and deploy a new Marketplace contract.

  • Click Deploy New contract
  • Click Deploy Now on the Marketplace contract
  • Configure your metadata, and deploy the contract to the Mumbai (MATIC) test network.

Creating listings

You can easily create listings on your marketplace using the dashboard.

But in this guide, we'll create them using Python!

Go ahead and create a new Python file. Let's call it marketplace.py, and open it in your favorite code editor.

Use the following command in your terminal to install the thirdweb SDK in the project!

pip install thirdweb-sdk
pip install python-dotenv

Now we can import the SDK, along with the other libraries we're going to use.

marketplace.py
from thirdweb import ThirdwebSDK
from thirdweb.types import NewDirectListing

To sign transactions from your wallet, you need to instantiate the SDK with your wallet's private key.

How to export your private key

Learn how to export your private key from your wallet.

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.

Now we can initialize our SDK:

# Load environment variables into this file
load_dotenv()

# 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)

Creating Listings

Firstly, let's connect to the marketplace contract we just created.

MARKETPLACE_CONTRACT_ADDRESS = "0x..." # Replace with your contract address
marketplace = sdk.get_marketplace(MARKETPLACE_CONTRACT_ADDRESS)

Then use the marketplace to create a new listing!

Here, we define the asset_contract_address and token_id, which identify an NFT that we want to list for sale.

If you don't have any NFTs to list, check out our guide "Mint an NFT Collection with Python" to mint some!

marketplace.direct.create_listing(NewDirectListing(
asset_contract_address= '<your-nft-contract-address-here>', # Replace with your NFT Collection contract address
token_id= 0, # change this to the token ID of the NFT you want to list
start_time_in_seconds= 30000000000,
listing_duration_in_seconds= 10000,
quantity= 1,
currency_contract_address= '0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889', # This is the Wrapped Matic contract address
buyout_price_per_token= 1
))

So the only thing left is to run our code!

Open a terminal and run your marketplace.py file.

python3 marketplace.py

That's it!

Now you have your own marketplace built with the Python SDK.

Learn what else you can do with the SDK on our Python Marketplace documentation.