This script shows how to mint an nft.

const API_URL = process.env.API_URL
const PUBLIC_KEY = process.env.PUBLIC_KEY
const PRIVATE_KEY = process.env.PRIVATE_KEY

const { createAlchemyWeb3 } = require("@alch/alchemy-web3")
const web3 = createAlchemyWeb3(API_URL)

const contract = require("../artifacts/contracts/MyNFT.sol/MyNFT.json")
const contractAddress = "0x5a738a5c5fe46a1fd5ee7dd7e38f722e2aef7778"
const nftContract = new web3.eth.Contract(contract.abi, contractAddress)

async function mintNFT(tokenURI) {
  const nonce = await web3.eth.getTransactionCount(PUBLIC_KEY, "latest") //get latest nonce

  //the transaction
  const tx = {
    from: PUBLIC_KEY,
    to: contractAddress,
    nonce: nonce,
    gas: 500000,
    data: nftContract.methods.mintNFT(PUBLIC_KEY, tokenURI).encodeABI(),

  const signPromise = web3.eth.accounts.signTransaction(tx, PRIVATE_KEY)
    .then((signedTx) => {
        function (err, hash) {
          if (!err) {
              "The hash of your transaction is: ",
              "\nCheck Alchemy's Mempool to view the status of your transaction!"
          } else {
              "Something went wrong when submitting your transaction:",
    .catch((err) => {
      console.log("Promise failed:", err)


Here was advised if i wanna mint let say 20 nfts i have to provide 20 tokenURI which is legitimate but i dont know how. This tutorial used Pinata to to upload JSON metadata and image, so if i wanna mint 20 nfts then do i need to upload 20 images and 20 JSON metadata to Pinata? then call mintNft() 20 times by following same steps? I am new to NFT so need advise in it. Thanks.

1 Answer 1


If you want to mint 20 NFTs with the contract shown in your example you have to write 20 contracts and implement the 20 ipfs addresses you get from pinata.

But you can make it easier with only one coctract. If you want, I can show you how.

Greetings -Shadow

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.