10 min read
|
Published
|
Updated

ERC721 Token Standard: How NFTs Work

What the ERC721 standard defines, how ownership, approvals and transfers work, how it differs from ERC20, and how to send ERC721 tokens by API.

Chapters
C
Chaingateway Team
Blockchain Experts

Reviewed by Maurice Sobiera

ERC721 is the Ethereum standard for non-fungible tokens (NFTs). It defines a common interface that a smart contract implements so that every token has a unique ID, exactly one owner, and the same set of functions for transfers and approvals. Wallets, marketplaces and APIs can then handle any ERC721 contract in the same way.

What does the ERC721 standard define?

The standard is specified in EIP-721. A compliant contract must implement the ERC721 interface and the ERC165 interface, which lets other contracts ask whether a contract supports an interface. The ERC721 interface ID is 0x80ac58cd.

The interface has three events and eight functions (safeTransferFrom comes in two forms):

ItemTypePurpose
TransfereventEmitted whenever ownership of a token changes, including minting and burning
ApprovaleventEmitted when the approved address for one token changes
ApprovalForAlleventEmitted when an owner enables or disables an operator for all of their tokens
balanceOf(owner)functionNumber of tokens an address owns
ownerOf(tokenId)functionAddress that owns one token
safeTransferFrom(from, to, tokenId)functionTransfers a token and checks that a receiving contract can handle it
transferFrom(from, to, tokenId)functionTransfers a token without that check
approve(to, tokenId)functionLets one address move one specific token
setApprovalForAll(operator, approved)functionLets an operator move all tokens of the caller
getApproved(tokenId)functionReturns the approved address of one token
isApprovedForAll(owner, operator)functionTells whether an operator is approved for an owner

safeTransferFrom has a second form with an extra data argument. When the receiver is a smart contract, both forms call onERC721Received on it and revert if the contract does not return the expected value. This protects tokens from being sent to contracts that cannot move them again. transferFrom has no such check, and EIP-721 states that the caller is responsible for confirming that the receiver can handle NFTs.

Two optional extensions exist. The metadata extension adds name(), symbol() and tokenURI(tokenId), where the URI usually points to a JSON file with a name, description and image. The enumeration extension adds totalSupply() and functions to list tokens by index.

How do you create an ERC721 token?

You write a smart contract that implements the interface, compile it, and deploy the bytecode to the Ethereum blockchain. Most teams start from a maintained library instead of writing the functions by hand. OpenZeppelin’s ERC721 documentation describes a maintained implementation, and the web-based Remix IDE can compile and deploy it without local tooling.

The deployment returns a contract address. Test the contract on a testnet first, then deploy to mainnet.

What is a contract address, and why does it matter?

The contract address is the only reliable identifier of an ERC721 token collection. Names and symbols are not unique: anyone can deploy a contract that calls itself the same as an existing collection. A token is fully identified by the pair of contract address and token ID.

When you integrate an ERC721 token, always store both values. Two different contracts can each have a token with ID 1.

What is the difference between ERC721 and ERC20?

ERC20 tokens are fungible: every unit is interchangeable, and the contract only stores a balance per address. ERC721 tokens are non-fungible: each token ID has its own owner and its own metadata. EIP-721 was designed on the ERC20 model, and it names the lack of per-asset tracking as the reason ERC20 does not fit NFTs.

The practical differences for an integration are small but easy to miss:

  • ERC20 transfers take an amount, ERC721 transfers take a token ID.
  • balanceOf on an ERC721 contract counts tokens, not value.
  • ERC721 has per-token approvals in addition to operator approvals.

For a longer comparison with ERC1155, which can hold fungible and non-fungible tokens in one contract, read ERC-721 vs ERC-1155 and Token standards explained. Our ERC20 explanation is in The ERC20 token standard.

Can gas fees be paid with ERC721 tokens?

No. Gas on Ethereum is always paid in ETH, even if the transaction only moves an ERC721 token. The sending address needs enough ETH to cover the gas, and the fee is the gas used multiplied by the gas price. You can read the current price on Etherscan’s gas tracker, and ethereum.org explains how the fee is calculated.

How much does it cost to create an ERC721 token?

Deploying the contract costs gas, and the amount depends on the size of the contract you deploy. Minting each token costs additional gas, which is paid either by the contract owner or by the user who mints, depending on how the contract is written. Estimate the deployment on a testnet with eth_estimateGas before you deploy to mainnet, then multiply the result by the current gas price.

The token standard itself is only a technical interface. Whether a use is regulated depends on what the token represents and where the parties are located. Selling tokens, accepting them as payment, or offering them as shares of a company can bring licensing, tax and Know Your Customer (KYC) or Anti Money Laundering (AML) obligations. Ask a lawyer who knows your jurisdiction before you launch a business model around them.

How do you send ERC721 tokens automatically?

You can send ERC721 tokens by script through the Chaingateway REST API instead of using a wallet for each transfer. The endpoint is POST /api/v2/ethereum/transactions/erc721, and it takes the sending address, the contract address, the receiver and the token ID. You pay gas in ETH from the sending address, and equivalent endpoints exist for Polygon and Arbitrum (erc721) and for BNB Smart Chain (bep721).

Terminal window
curl -X POST https://app.chaingateway.io/api/v2/ethereum/transactions/erc721 \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
-H "X-Network: testnet" \
-d '{
"from": "0xSenderAddress",
"contractaddress": "0xYourErc721ContractAddress",
"to": "0xReceiverAddress",
"tokenId": 1,
"password": "wallet-password"
}'

The required fields are from, contractaddress, to and tokenId. Instead of password, you can pass a privatekey. Optional fields such as gas, gasprice, maxFeePerGas, maxPriorityFeePerGas and nonce let you control the fee and the order of transactions. A successful call returns HTTP 201 with the transaction ID in data.txid. Set the X-Network: testnet header to try the call on a test network first. The request parameters are listed in the API reference.

For a step-by-step view of ownership checks, signing and confirmation, see Transfer NFTs via API.

Are there more token standards?

Yes. ERC20 covers fungible tokens, and ERC-1155 is a multi-token standard that can manage fungible and non-fungible tokens in a single contract. On other networks the equivalents have different names, for example TRC-20 on TRON and SPL on Solana.

How we write these articles

Ready to build this yourself? Get your API key — 7-day trial, no card required — or see the Ethereum API for the full endpoint reference.

C
Chaingateway Team
Blockchain Experts

The Chaingateway team is dedicated to simplifying blockchain integration for developers worldwide.