This page may contain legacy content

To get our most up-to-date content please access our documentation

September 18, 2021

NFT Programming for Beginners

Table of Contents

The world of non-fungible tokens, commonly referred to as NFTs, has exploded in popularity. In fact, the NFT industry allows artists to earn tens of millions of dollars from their digital art. The advent of NFTs is now commanding such massive valuations that digital creators in the art, finance, and crypto community are beginning to wake up to the transformational impact of NFTs. Therefore, learning NFT development is a great way to advance your Web3 career. However, if you’re a beginner in the NFT space, you might be wondering how to get started with NFT development. Therefore, we’ll show you in this article the necessary steps and teach you NFT programming for beginners.

As a beginner, you might also be wondering what are NFTs? NFTs are non-fungible digital assets that represent unique items on various blockchains. However, the purpose of this article is not to explain in detail what NFTs are. Still, if you’d like to read up on that topic before going further in this tutorial, check out the following article from Moralis’ blog for more information: “Non-Fungible Tokens Explained – What are NFTs?”.

Instead, if you are a programmer looking to get started with blockchain and NFT development, you’ve come to the right place. Here, you’ll get a basic overview of how to program or develop an NFT and deploy it using a hassle-free middleware platform such as Moralis. 

Ultimate NFT Programming Course

So, why will we be using Moralis in this ultimate NFT programming for beginners course? The reason is simple. Moralis is a capable platform that provides seamless hosting, and a full suite of products to help you with NFT programming. This includes a powerful deep index NFT API to use in the creation of your NFTs or NFT platforms. Moreover, Moralis also helps you manage your NFTs down the line. On top of using Moralis and its next-gen Web3 services to help speed up your projects, you’ll also get tips on which open source tools to use in the process of NFT development. 

More advanced, blockchain-savvy developers will find this article to be a helpful and quick introduction to creating their own NFTs. If you prefer a detailed video tutorial, you can go straight to Moralis’ YouTube channel and enjoy this well-explained guide on how to get started immediately with NFT development:

https://www.youtube.com/watch?v=tBMk1iZa85Y

For those who are still in the process of learning programming languages and building their stack, you will benefit from this guide by learning NFT development on a conceptual level. It will give you an idea of the demands of such a project before you immerse yourself fully into NFT programming. 

How to Get Started with NFT Programming

Given the demand for NFTs and NFT platforms today, it’s not surprising that they’ve piqued the interest of developers who want to get started in this exciting and lucrative blockchain space. 

People think NFT programming is complex and difficult. While it does have certain prerequisites, it is not as intimidating as it sounds. Lately, it has been made easier by open source tools that allow you to import reliable smart contracts and not have to start from scratch in building your NFT projects. Once you have the required skills and knowledge and become familiar with the tools to make your build easier, you’ll find that NFT development can be quite simple, easy, and fun!

Prerequisites of NFT Programming

For starters, you need to be familiar with the concepts of blockchain, smart contracts, Web3, DeFi, dApps, and how such ecosystems work. You also need a thorough understanding of decentralized networks such as Ethereum. Ideally, you should already have explored various ways of building on Ethereum, such as programming dApps or creating ERC-20 tokens. Furthermore, you should already know programming languages such as JavaScript and Solidity.

JavaScript and Solidity, as well as formats such as JSON, are important in creating smart contracts and interacting with the blockchain. They are also necessary to understand the role of various Web3 tools and middleware platforms such as Moralis that will help you optimize your builds. 

To get a solid foundation on JavaScript and how it relates to blockchain, check out the following course from Ivan on Tech Academy: “JavaScript Programming for Blockchain Developers Course”. This is an excellent intro to the subject, providing you with a unique perspective of JavaScript’s importance in blockchain and NFT development. The other must-have on your programming skill set, Solidity, is known as the “language of smart contracts”. Solidity is purposely built for Ethereum and smart contract creation. You can learn more about this through Ivan on Tech Academy’s introductory course explaining Ethereum smart contract programming.

If you’ve ticked all these boxes, you’re ready to move on to the next step, which is the actual process of NFT development.

NFT Development – A Simplified Guide

While we might not cover all the intricacies of NFT programming or NFT development, this guide will introduce you to the general steps of the process and the tools you can use along the way to help make it simpler.

Steps of NFT Programming

This guide is organized in such a way that it provides you with an overview of the important steps in NFT programming or NFT development. The basic steps of NFT programming are:

  1. The creation of the NFT itself, which involves building a smart contract.
  2. Adding the metadata and finding a host for the metadata.
  3. Listing your NFT on a marketplace. 
  4. Managing your NFT. 

Please Note: In this article, we will focus on the first two steps, namely, NFT creation by building the smart contract based on the ERC-1155 standard and finding a host for the metadata. However, as steps three and four are also important, you may refer to the video on NFT programming referred to earlier to learn them in more detail.

NFT Programming Step 1: Creating the NFT

We are about to create an ERC-1155 token. Note that there are two NFT or non-fungible token standards: ERC-721 and ERC-1155. ERC-1155 is newer and has more functionalities than ERC-721.

Using ERC-1155, the latest standard for creating non-fungible tokens, offers certain advantages, including:

  • More flexibility.
  • Ability to create and use both fungible and non-fungible tokens in the same contract.
  • Doing batch transfers.
  • Better effectiveness in setting and managing parameters.

Building a Smart Contract

To create a smart contract, go to Remix (remix.ethereum.org). Remix lets you access numerous libraries to make it easier to build smart contracts. Such libraries are immensely useful as sources of reliable and tested smart contracts that can be used in NFT development.

Create a New File

Create a new file in your Remix workspace. For this purpose, we are naming it “Opensea_nft”, but you are free to choose any name you like. 

It’s important to reiterate that you must know Solidity before you proceed any further. If you are still learning the ropes of Solidity, check out Moralis’ Youtube channel for more educational tools and a great Solidity tutorial.

Start by typing in “pragma solidity ^0.8.0”. This is the compiler version used for this purpose. Next, import the ERC-1155 token contract from OpenZeppelin. OpenZeppelin’s amazing library of token contracts and other useful contracts provide you with well-tested and reliable smart contracts that you can easily use to perform common functionalities in your builds. 

Finding ERC-1155 Smart Contracts in OpenZeppelin

Here, you can see the OpenZeppelin GitHub page for the ERC-1155 contract:

The ERC-1155 standard provides all the functionality you need for the NFT, including the necessary mapping functions to keep track of balances and the functions for checking balances. It also provides the functions for doing transfers, minting new NFTs (including minting in batches), and burning NFTs. Not all available functionalities will be implemented in this project. We will only be choosing the most relevant ones.

Creating an NFT from the Smart Contract

Import the original contract into the newly created smart contract on Remix. To do this, you can just import the GitHub link.

Proceed to define the new smart contract. Name the new contract (for this purpose, we are calling it “NFTContract”). After this, you can “inherit” from the original ERC-1155 contract. Inheritance in Solidity simply means you take the functionality from this contract. 

After inheriting the contract, you can start implementing your own NFT based on the parent ERC-1155 contract.

Implement Your Own NFT

This is where you specify the items that are unique to your contract. For example, you can name your NFT project and specify how many tokens you want to mint. You will also provide the metadata and indicate who can call functions on your contract. 

Adding Restrictions

To make the minting function exclusive to the owner, you’ll need to use the “Ownable” smart contract, which is available on OpenZeppelin. Go to the OpenZeppelin site and click “Contracts”, then “Access”, then “Ownable.sol”. Import this file into your contract.

Set the mint function to “onlyOwner”. This guarantees that the minting is exclusive to the address that first deployed the contract by making sure that the caller is the same as the owner. Add a “require” statement. What all of this does is make sure that the contract saves the address of the owner and implements a modifier—a snippet of code that is run before the function gets run. 

Your smart contract should look like this:

Burning Tokens

Some might want the ability to burn their own NFTs. All you need to do is to add the “burn” function and the parameters. The “burn” function makes sure that the account balance is greater than the amount you burn. For other parameters, you can choose to add restrictions like the above. It makes sense that the burn function is called only by the contract owner. Therefore, you can also add a “require” function to the code.

Deploy on a Virtual Machine

This contract is now ready to deploy on a virtual machine. To test the smart contract, click on the Ethereum icon on Remix’s left side menu. Make sure you select the NFT contract. Click “deploy”. 

After you click “deploy”, you will be able to view the instance. The instance lists all the functions that can be called in this contract. After that, try calling some of the functions to see if they work. 

Token Balance

You can start by checking your token balance. Under the file name “Artwork”, you can paste the account ID and create the token ID (in this case, it is temporarily called “0”), and click “call”. You will be able to see your balance immediately.

Testing the Mint Function

You can mint new tokens by simply clicking on the “mint” function on your instance and inputting your address, token ID, and the number of tokens you want to mint. Click “transact”.

If you check your token balance again, you will find that these newly minted tokens have been added to your total. You may repeat the process with each new token ID. 

NFT Programming Step 2: Adding Metadata and Finding a Host 

For this next step, you’ll need to understand the metadata structure for each NFT. To get an overview, go to the OpenSea website, and search for where they display the metadata standards. 

The basic structure, written as a JSON string between two curly braces, should include at least the following:

  • Description.
  • External URL: points to where you can view the NFT.
  • Image: a link to the image itself.
  • Name.
  • Attributes.

Create the JSON File

Open up a new folder in your Remix editor. It should be an “index.html” file to be compatible with Moralis. Save the file. Create a JSON file; for this example, we can write “0.json” as a preliminary name and include the actual properties in that file (image, description, name, etc.). It should appear like the example from OpenSea.

After you create the JSON string(s) for your NFT(s), you need to host them somewhere. It can’t be within the smart contract or on the blockchain itself. There needs to be an external link to a JSON file, which will then be put into the smart contract. This is where Moralis comes in.

Host the JSON File(s) on Moralis

Get the URL to host your directory. To do this, you’ll need various CLIs from your Moralis server. To proceed, you need to have a Moralis account. If you haven’t signed up yet, don’t worry. Just go to Moralis’ official site. It’s fast, easy, and free to sign up! 

Once you have a Moralis account set up, create a new server by clicking the button “+ Create a new Server” on the upper right side. After creating your server, click on “View Details” to retrieve the CLI API key and CLI API secret. These are details that you’ll need to input later on in Remix.

On Remix, open up “Terminal” on your menu bar and view “Terminal”. On it, type run “npm install – g – moralis – admin – cli”. When you deploy this, it will ask for your CLI API key and, later on, your CLI API secret. Input the information from your Moralis server onto “Terminal”.

This is now ready to deploy. If everything is working correctly, you should read “Deployed successfully!”.

Copy the URL of this file and visit the URL on your browser. Now get the URL of the artwork you wish to embed in your NFT and paste it as the “image” on your JSON string.

You may also choose to add an external URL (such as an artist’s professional website) to add proper attributions to your artwork or image. If you wish to add attributes after the initial file was deployed, you can edit the file and re-deploy as frequently as necessary.

You can repeat this process as many times as you like for your other image(s). 

Pay Attention to ERC-1155 Standards 

Now that you already know how to create a JSON NFT file and host it on Moralis, it’s time to fine-tune your NFT development. 

The ERC-1155 standard, as indicated in the actual EIP under metadata choices, has a specific format for the JSON ERC-1155 string. You need to check the specification for the ERC-1155 standard indicated under string format: it requires 64 hexadecimal characters with no 0x prefix. So it follows that you need to modify your original file name and pad it up to 64 digits as necessary.

Rename File

In this case, because the NFT ID is simply “0”, you can just add 63 more 0’s to make 64. This is important because everyone who queries an ERC-1155 will query according to this specific schema. 

After renaming your file in adherence to the accepted standard, you can save it and once again publish it to the Moralis server.

Deploy Smart Contract

Now that your file is already hosted on a website, you can proceed to deploy your smart contract. 

Get the Base URL

Copy the URL obtained from Moralis and paste it under the constructor of the ERC-1155 contract. To go to the actual NFT JSON file, add “/{ID}.json”. 

The above process is how we provide the URL to an ERC-1155 contract.

NFT Metadata and Marketplaces

Marketplaces such as OpenSea interact with hosted NFT metadata efficiently. Proper formatting will allow such marketplaces to use the same on-chain string to be used for a large number of tokens by defining a URI only once.

Essentially, you are referring to the same space in the blockchain. The URI is only provided once, and the client can replace the “{ID}” with any token ID in hexadecimal form. 

Deploy on Testnet

Your NFT smart contract is now ready to deploy on a testnet to see if it works. This involves getting testnet tokens from faucets via sites such as Rinkeby’s website. You should already be familiar with obtaining Ether from such faucets, as well as using them on Remix for deployment and testing. For this process, you’ll also need a MetaMask account.

Go to Remix and click “Injected Web3” under the “Deploy” tab. Connect your MetaMask account and make sure you’ve selected the “Rinkeby (4) network”. Click “Deploy”.

Go to ropsten.etherscan.io. Paste the transaction hash. You should be able to find your NFT there. When everything is done, you can launch your NFT on the marketplace’s testnet. Simply go to OpenSea and select the testnet option. 

When it’s working perfectly on the testnet, you can deploy it on the mainnet. Simply select “Mainnet” from your MetaMask account. When you go back to OpenSea, select the mainnet instead.

NFT Programming Made Easier with Moralis

Beeple’s groundbreaking NFT artwork “Everydays: The First 5000 Days” (partial image). Photo Credits: Christie’s

As you can see, Moralis helps streamline the NFT programming process by providing hosting for your NFT metadata, so you can host, test and deploy your NFTs in a matter of hours. 

In this article, we’ve only touched on the basic parts of NFT programming. However, to get the full details on how you can create, launch, list, and manage your NFTs, make sure to check out the excellent Moralis NFT video tutorial referred to at the beginning of this article. It’s important to note that aside from being a part of your NFT creation journey, Moralis has tools to manage your NFT(s) in the future. 

DeFi NFT platforms such as NFTmall and SuperFarm have already integrated Moralis’ API, with NFTmall calling it “the Swiss Army knife of Web3 backend development”. With such massive projects discovering the convenience and reliability of Moralis, it’s time to explore NFTs more. 

Consider this as the first step to unlocking the incredible things that Moralis can do to supercharge your NFT builds!

NFT API
Unlock the full potential of your NFT projects with this industry-leading NFT API! Fast, easy, and free.
NFT API
Related Articles
October 18, 2023

NFT API Providers – Comparison of Developer Tools & Resources

February 11, 2023

Exploring the #1 Ethereum Price API to Get Token Prices

August 18, 2022

Exploring the Ultimate NodeJS EVM API

October 12, 2023

Web3 Market Data API – Trending NFTs, ERC20 Tokens by Market Cap & More Web3 Insights

November 15, 2022

Get Wallet Balance – How to Get Wallets Native Crypto Balance

December 13, 2022

Tutorial for Blockchain Developers on How to Get All Transfers of an NFT

December 4, 2022

Ethers.js vs Web3 Streams – The Best Option for Blockchain Developers

November 13, 2022

How to Get All NFTs Owned by an Address – 3-Step Process