How to Develop an NFT with the ERC-721 Standard
Build an ERC-721 NFT contract with OpenZeppelin: write the token, add metadata and royalties, then test on Sepolia and deploy to Ethereum.
On this page
Short answer
To develop an NFT with ERC-721, write a Solidity contract that inherits from OpenZeppelin's ERC721 base, add a mint function using _safeMint, attach metadata through tokenURI, set royalties with EIP-2981, then test the contract on the Sepolia test network and deploy it to Ethereum mainnet.
What is the ERC-721 standard?
ERC-721 is the Ethereum standard for non-fungible tokens: tokens that are each unique, cannot be divided, and carry their own identity on the chain. It defines the functions every ERC-721 contract must provide, so wallets, marketplaces and apps can work with any compliant token without custom code.
The standard was proposed in January 2018 and finalised in June 2018. It arrived after ERC-20 had shown that a shared token interface lets many apps support many tokens, and it applied the same idea to unique assets: art, tickets, certificates, game items and membership passes.
A token under ERC-721 is identified by its contract address plus a token ID. Two tokens from the same contract can have different IDs, different metadata and different owners, which is what makes each one non-fungible.
For the concepts alone, read what ERC-721 is and how it works first. This article is the development guide: contract, metadata, royalties, testing and deployment.
What must an ERC-721 contract do?
An ERC-721 contract must expose a fixed set of functions so that any wallet or marketplace can read ownership and move tokens. If you have not settled on a standard, compare the NFT standards before building. The table below lists the core functions and what each one does.
| Function | What it does |
|---|---|
| balanceOf(owner) | Returns how many tokens one address owns |
| ownerOf(tokenId) | Returns which address owns one token |
| safeTransferFrom(from, to, tokenId) | Moves a token, checking the receiver can hold it |
| transferFrom(from, to, tokenId) | Moves a token without the receiver check |
| approve(to, tokenId) | Lets one address move one token |
| getApproved(tokenId) | Shows which address is approved for one token |
| setApprovalForAll(operator, approved) | Approves an address for all of the owner's tokens |
| isApprovedForAll(owner, operator) | Checks an all-tokens approval |
The contract must also emit three events: Transfer when a token moves or is minted, Approval when one token is approved, and ApprovalForAll when an operator is approved. Marketplaces and indexers listen to these events to track ownership without reading every block by hand.
The metadata extension adds name, symbol and tokenURI. It is optional in the standard but almost every real collection includes it, because wallets and marketplaces need the name and the metadata link to display a token.
How do you write the contract?
Start from a tested base instead of writing the standard from scratch. OpenZeppelin's ERC721 contract implements the core and metadata extensions, and its ERC721URIStorage extension stores a separate metadata link per token. The general workflow is covered in how to write a smart contract.
Your contract inherits from ERC721 and, when you need per-token metadata, ERC721URIStorage. The constructor sets the collection name and symbol. A mint function then creates tokens: it takes a recipient address and a token ID, calls _safeMint to assign the token, and calls _setTokenURI to attach the metadata link.
Control who can mint. A common pattern is to let only the contract owner mint, using OpenZeppelin's Ownable, or to sell minting rights through a public function with a price and a per-wallet limit. Keep the mint function small: one job, a few checks, then the mint.
Add a supply cap if the collection has a fixed size. Store a maximum token count and refuse to mint past it. A cap is easy to add now and hard to add later, because changing the rules after launch needs a new contract or an upgrade path.
How do you build the metadata?
Each token's metadata is a JSON file with a name, a description, an image link and a list of traits. Wallets and marketplaces read this file through tokenURI, so the file decides what a buyer sees: the picture, the title and the properties. For the full creation flow, see how to create an NFT token.
By default, tokenURI returns the base URI plus the token ID. If the base URI is ipfs://QmExample/ then token 42 resolves to ipfs://QmExample/42. Keep the base URI stable, because changing it after minting changes what every token points to.
For one-of-one collections, ERC721URIStorage lets you set a different URI per token with _setTokenURI. That costs more gas than a shared base URI, so use it only when tokens genuinely differ in structure, not just in one trait value.
Store the files somewhere durable. IPFS is the usual choice for NFT metadata and images, often through a pinning service so the files stay available. A link to a server you control can disappear or change, which breaks the token's display.
How do you add royalties?
Royalties follow EIP-2981, a separate standard that reports how much of a resale should go to the creator. Your contract implements royaltyInfo, which takes a token ID and a sale price and returns a receiver address and a royalty amount.
OpenZeppelin's ERC2981 extension does the arithmetic for you. Call _setDefaultRoyalty with the receiver and the fee in basis points: 500 means 5 percent, so a sale at 2 ETH reports a royalty of 0.1 ETH. You can also set a different royalty per token with _setTokenRoyalty.
Know the limit: EIP-2981 only reports the royalty, it does not force a marketplace to pay it. Check how the marketplaces you care about handle royalties before you promise a rate to artists or partners. The safety checks around royalties and minting are covered in ERC-721 best practices.
Set the royalty once and leave it alone. Changing it after sales have started confuses buyers and can break listings that already read the value. If the rate must change, announce it before the next sale, not after.
How do you test and deploy in 7 steps?
Build in order and check each step before moving on. The steps below take a contract from an empty folder to a live collection.
- Write the contract. Inherit from ERC721 and ERC721URIStorage, add the mint function and the royalty setup, then compile.
- Write unit tests. Test minting, transfers, approvals, the supply cap and the royalty maths before touching a live network.
- Deploy to Sepolia. Use the Sepolia test network with free test ETH from a faucet, and confirm the contract address on the testnet explorer.
- Mint test tokens. Mint a few tokens, read their metadata through tokenURI, and check the images load in a wallet.
- Verify the source code. Verify the contract on the explorer so anyone can read it. Unverified contracts are a warning sign for buyers.
- Audit before real money. Get a security review if the collection handles real funds. Fix what the review finds and retest.
- Deploy to mainnet. Deploy the same tested code to Ethereum mainnet, verify it, then mint the first real tokens.
Example (hypothetical): a gallery mints 500 tickets
Example (hypothetical): a gallery sells 500 exhibition tickets as NFTs at 0.05 ETH each. If every ticket sells, the gallery collects 500 x 0.05 = 25 ETH.
Minting happens in batches of 50 to keep each transaction small. Ten batches mint all 500 tokens, and each token gets its own metadata link through _setTokenURI. The gallery sets a 5 percent royalty with _setDefaultRoyalty.
After the exhibition, 40 tickets change hands on the secondary market at an average of 0.08 ETH. Total resale volume is 40 x 0.08 = 3.2 ETH, and the 5 percent royalty sends 3.2 x 0.05 = 0.16 ETH back to the gallery.
The gallery checks the result on the explorer: 500 tokens exist, and the royalty payments match 0.16 ETH. The check takes minutes and confirms the contract behaved as designed.
What are the common mistakes?
The most common ERC-721 development mistakes are writing the standard from scratch, skipping the receiver check, and promising royalties the contract cannot enforce.
- Writing the standard from scratch. Use OpenZeppelin's tested ERC721 base. A hand-written transfer function is the most common source of lost tokens.
- Forgetting the receiver check. Use _safeMint and safeTransferFrom so tokens cannot be sent to a contract that cannot hold them.
- Mutable metadata links. A base URI that can change after minting lets the art change after the sale. Freeze it or document the change process.
- Promising enforced royalties. EIP-2981 reports a royalty; it does not collect it. Say what the contract does, not what you wish it did.
- Skipping the testnet. Deploying straight to mainnet turns every bug into a real loss. Test on Sepolia first.
- No supply cap. An open mint with no cap can flood the collection. Decide the maximum before the first mint.
Key takeaways
- ERC-721 defines the functions every non-fungible token contract must provide
- Start from OpenZeppelin's ERC721 base instead of writing the standard yourself
- Attach metadata through tokenURI, with a stable base URI or per-token URIs
- Report royalties with EIP-2981, and check that marketplaces pay them
- Test on Sepolia, verify the source, then deploy the same code to mainnet
Frequently asked questions
Do I have to write the ERC-721 functions myself?
No. OpenZeppelin's ERC721 contract already implements the required functions, including balanceOf, ownerOf, safeTransferFrom and approve. You inherit from it and add only your mint function, supply rules and royalty setup, which keeps the audited logic intact.
What does tokenURI point to?
It points to the token's metadata file, usually a JSON file on IPFS with the name, description, image link and traits. By default it returns the base URI plus the token ID, so token 42 with base URI ipfs://QmExample/ resolves to ipfs://QmExample/42.
How do royalties work with ERC-721?
Royalties use EIP-2981, which adds a royaltyInfo function that reports a receiver and an amount for a given sale price. A 5 percent royalty on a 2 ETH sale reports 0.1 ETH. The standard only reports the royalty, so check that your marketplaces actually pay it.
Which test network should I use?
Use Sepolia, Ethereum's main public test network, with free test ETH from a faucet. Deploy there, mint test tokens, check the metadata in a wallet, and verify the source code on the testnet explorer before deploying the same code to mainnet.
Can one contract mint different kinds of NFTs?
Yes, if each token has its own ID and metadata. One contract can issue art, tickets and passes as separate tokens. If you need many identical copies of the same item, ERC-1155 fits better, because it handles batches of identical tokens in one contract.
Does ERC-721 work on chains other than Ethereum?
Yes. Any EVM-compatible chain supports ERC-721 contracts, including Polygon, BNB Smart Chain, Avalanche and Arbitrum. The contract code stays the same; only the network settings, gas token and explorer change when you deploy elsewhere.