How to Create a Solana NFT with Python

Alex Tandy
Mysteria
Published in
5 min readOct 25, 2021

--

This article will show you how to mint an NFT using Python on Solana. It will also discuss how to upload files to Arweave.

This document is especially good for people who do not want to use the on-chain Candy Machine program, and instead have a custom minting flow that they need to implement.

I won’t go into the details of what an NFT is. Folks have done that plenty already. But before we get started, make sure you review the prerequisites:

Prerequisites

  1. Access to Command Line
  2. Python Installed
  3. Arweave-Deploy Installed
  4. Metaplex Python Library

And now, let’s break down the Overview of Steps required to mint a NFT using Python on Solana:

Overview of Steps

  1. Uploading Asset File to Arweave: the image/file needs to be uploaded to a hosting server or CDN. In the spirit of decentralization, we will use Arweave; a permanent file storage solution.
  2. Creating the Metaplex Metadata JSON file: this step will define various attributes of your NFT and how it sales will be handled on chain. Importantly, this step is really what attaches the “art”/assets to your Solana token.
  3. Uploading the Metadata JSON file to Arweave: here, we need to make sure the JSON file is hosted somewhere permanently, so we use Arweave. The JSON file is what will be referenced by wallets to render the collectible inside of it.
  4. Minting the NFT: this is where we mint the token and make use of the work in steps 1–3. We do this in Python.

With that said, let’s get going.

Step 1: Uploading Asset File to Arweave

This step is important because it’s basically taking your asset file and putting it on a CDN somewhere. The asset needs to have high uptime and reliability, because otherwise your NFTs won’t be loading in users’ wallets. That would not be OK. So the standard right now is to use Arweave for file uploading.

We chose to upload our files using their command line tool, Arweave-Deploy. Once installed, you will want to claim some Arweave tokens with the faucet, here. Download the keypair JSON file and keep in mind where; you’ll need it shortly.

You’re now ready to upload your file to Arweave. For the purposes of this demo, I’ll use an asset from our game, a png file called divinity.jpg

Our JPG asset from the first round of our game, “The Old Castle”.

You’re now ready to upload the asset to Arweave, using the Arweave command line program:

arweave deploy /path/to/my/divinity.jpg --key-file /path/to/my/arweave-key.json

Once uploaded, you can copy the link to the file on Arweave.

https://arweave.net/Umi1tOw4hIPn8VkJtq_fjamMgT-YCOGa55g22t6sT6M?ext=jpg

Great, your asset is now available on Arweave. Next, creating the token metadata for your NFT.

Step 2: Creating the Metaplex Metadata JSON file

This step is critical to building your NFT and having the token actually show up in a users’ wallet the way you want it to. I advise you to get incredibly familiar with the Metaplex NFT Standard. Here are the docs.

For our divinity.jpg , We’re going to keep the example simple. I’m just going to show you what our JSON looked like.

{
"name": "Divinity - DevNet",
"symbol": "DIV",
"description": "You will probably want to check out https:// solcypher.com to win prizes or thank the creators of this doc.",
"seller_fee_basis_points": 700,
"image": "https://arweave.net/Umi1tOw4hIPn8VkJtq_fjamMgT-YCOGa55g22t6sT6M?ext=jpg",
"attributes": [
{
"trait_type": "cypher",
"value": "Divinity"
},
{
"trait_type": "game",
"value": "The Old Castle"
}
],
"collection": {
"name": "The Old Castle",
"family": "SolCypher"
},
"properties": {
"files": [
{
"uri": "https://oabackuplocationofyourfile.jpg",
"type": "image/jpg"
}
],
"category": "image",
"creators": [
{
"address": "AvFLeGBFDthzdoct5mHpbUE8ZJdYD4oZXpksoRiws8AG",
"share": 100
}
]
}
}

Step 3: Uploading the Metadata JSON file to Arweave

Here, we need to put the JSON file on a CDN (Arweave) so that the crypto wallets like Phantom can read this and use it to render your assets in your Collectibles.

So, take your creafted metadata JSON file from Step 2, and upload it to Arweave, basically the same command as you did in Step 1.

arweave deploy /path/to/my/divinity.json --key-file /path/to/my/arweave-key.json

Wait until the file is uploaded, and keep the uploaded URL. You will need the URL for the next step.

Step 4: Minting the NFT with Python

Maybe this was the step you’ve been waiting for. Here we are.

For this, we used the Metaplex Foundation’s Python API. Get this downloaded and in your working directory. You should be able to mostly follow the examples they’ve provided, but for the purposes of this tutorial, here is ours:

import base58
import json
import os
import metaplex_api
from cryptography.fernet import Fernet
SERVER_DECRYPTION_KEY = Fernet.generate_key().decode("ascii")
TEST_PRIVATE_KEY = os.environ['TEST_PRIVATE_KEY']
TEST_PUBLIC_KEY = "MsE5TofYuKtxgboqwzbLxjFbaNwK41b3YYfX6gyxfcD"
cfg = {
"PRIVATE_KEY": TEST_PRIVATE_KEY,
"PUBLIC_KEY": TEST_PUBLIC_KEY,
"DECRYPTION_KEY": SERVER_DECRYPTION_KEY
}
api = metaplex_api.MetaplexAPI(cfg)
account = metaplex_api.Account(list(base58.b58decode(cfg["PRIVATE_KEY"]))[:32])
api_endpoint = "https://api.devnet.solana.com/"
# requires a JSON file with metadata. best to publish on Arweave
divinity_json_file = "https://arweave.net/lafoms3egQiVeboVVSsgIXRH14DiyxmKLwzD_EWiKv8"
# deploy a contract. will return a contract key.
result = api.deploy(api_endpoint, contract_name, contract_symbol)
contract_key = json.loads(result).get('contract')# conduct a mint, and send to a recipient, e.g. wallet_2
mint_res = api.mint(api_endpoint, contract_key, TEST_PUBLIC_KEY, divinity_json_file)

And with that, you will have minted your NFT, and it should be available in the Collectibles section of you Phantom Wallet!

If you found this document helpful, consider giving us a follow on Twitter, and competing in the first round of our game, “The Old Castle”, which is scheduled to launch in November. The initial prize pool is starting at 10 $SOL ~ about $2000 USD at the time of writing.

You may also send tips to this address:

MsE5TofYuKtxgboqwzbLxjFbaNwK41b3YYfX6gyxfcD

--

--

Alex Tandy
Mysteria

Publisher of “Journey from A to Z with Teacher Judy” (https://link.tandybooks.com/abc). Also, product leader working in tech.