Recently I'm build some ERC721 nft contract.

I know that smart contract can receive ETH, so my question is that is it possible to write a payable function, and upon receive some ETH from someone, the contract will mint an NFT right away and send to the msg sender?

If not, what will be the recommended way to buy an NFT with ETH instead of going through some third party platform like OpenSea?


  • 2
    Yes, everybody is doing it this way -- unless you need random minting which is a little more complicated Sep 15, 2021 at 13:27
  • @WilliamEntriken Hi! thanks for the reply. Do you have some examples of this kind of contract? like those written by openzeppelin. Because I can't seem to find an open-source template..
    – johnhckuo
    Sep 15, 2021 at 14:24
  • Yes, here is an example contract which accepts direct Ether payment and vends an ERC-721 token. It is linked from the ERC-721 standard as a demo and it was recently re-released os open source: github.com/su-squares/ethereum-contract/blob/master/contracts/… Sep 16, 2021 at 13:48

1 Answer 1


yes indeed. Here's a small example of how we do it:

function mint(uint _parentTokenIndex) public payable 
    require(msg.value >= NFT_PRICE, "Not enough ETH sent.");
    require(!_assetExists[_parentTokenIndex], "The asset does not meet the unique constraint.");

    _mint(msg.sender, _id);
    _setTokenURI(_id, METADATA_URL);
    _assetExists[_parentTokenIndex] = true;

Steps are like this:

  • Make your function payable.
  • Add a requirement for sent eth: require(msg.value >= NFT_PRICE, "Not enough ETH sent.");.
  • Mint your NFT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.