Alchemy: How to Mint an NFT Using Web3.js

Learn how to mint an NFT using Web3.js.

What is Web3.js?

Web3.js is an Ethereum JavaScriptAPI made up of a collection of libraries that enables you to interact with remote Ethereum nodes using HTTP or WebSocket.

Read below to learn how to mint an NFT using Web3.js.

Prerequisites:

1. We’ll start by uploading an image to Filebase for us to use as an NFT.

To do this, navigate to console.filebase.com. If you don’t have an account already, sign up, then log in.

2. Select ‘Buckets’ from the left side bar menu, or navigate to console.filebase.com/buckets.

Select ‘Create Bucket’ in the top right corner to create a new bucket for your NFTs.

3. Enter a bucket name and choose the IPFS storage network to create the bucket.

Bucket names must be unique across all Filebase users, be between 3 and 63 characters long, and can contain only lowercase characters, numbers, and dashes.

4. Next, select the bucket from your list of buckets, then select ‘Upload’ in the top right corner to upload your image file.

5. Select your image to be uploaded.

Once uploaded, it will be listed in the bucket.

6. You can view the object’s IPFS CID in the CID column, or you can click on your uploaded object to display the metadata for the object, which includes the IPFS CID.

Choose the method you prefer, and take note of the IPFS CID for your image. We will reference this later.

7. Next, create a new file called nft-metadata.json file.

Input the following content into this file:

{
    "attributes" : [ {
      "trait_type" : "Type",
      "value" : "Robot"
    }, {
      "trait_type" : "Eye color",
      "value" : "Blue"
    } ],
    "description" : "Filebase NFT.",
    "image" : "https://ipfs.filebase.io/ipfs/FILEBASE_IPFS_CID",
    "name" : "Filebase Robot"
}

Replace the name, description, and attributes with any values you want. Then replace the FILEBASE_IPFS_CID value with the IPFS CID you took note of earlier.

8. Then, upload this nft-metadata.json file to your Filebase IPFS bucket using the same workflow we used to upload our image files.

Take note of the IPFS CID for your nft-metadata.json file.

9. Next, install the web3.js package:

npm install @alch/alchemy-web3

10. Create a new file called mint-nft.js with the following content:

require('dotenv').config();

const API_URL = process.env.API_URL;
const { createAlchemyWeb3 } = require("@alch/alchemy-web3");
const web3 = createAlchemyWeb3(API_URL);
const contract = require("../artifacts/contracts/MyNFT.sol/MyNFT.json");
console.log(JSON.stringify(contract.abi));

11. Run this script with the command:

node mint-nft.js

Take note of the contract address that’s returned from this script.

12. Now, let’s head over to Alchemy and either sign up for an account or login.

13. From the Alchemy dashboard, we need to create an app.

Select the ‘Create App’ button to get started.

14. Create a new app. For our example, we called our app Token.

Set the chain to ‘Ethereum’, and the network to ‘Göerli’.

15. Then, from our App’s page, select ‘View Key’:

16. Copy your app’s HTTP Key:

17. Open the .env file in your project directory.

Replace the existing content with the following:

API_URL = "ALCHEMY_API_ENDPOINT”

PRIVATE_KEY = "WALLET_PRIVATE_ADDRESS"

PUBLIC_KEY = "WALLET_PUBLIC_ADDRESS"

18. Open your mint-nft.js file.

Replace the existing contents with the following:

require('dotenv').config();
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 = "CONTRACT_ADDRESS";
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,
    'maxPriorityFeePerGas': 2999999987,
    'data': nftContract.methods.mintNFT(PUBLIC_KEY, tokenURI).encodeABI()
  };

  const signedTx = await web3.eth.accounts.signTransaction(tx, PRIVATE_KEY);
  const transactionReceipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
  
  console.log(`Transaction receipt: ${JSON.stringify(transactionReceipt)}`);
}

mintNFT("https://ipfs.filebase.io/ipfs/FILEBASE_IPFS_CID");

Replace CONTRACT_ADDRESS with the contract address returned from step 10 and replace FILEBASE_IPFS_CID with the CID of your nft-metadata.json file.

19. Run your script with the command:

node mint-nft.js

You’ve deployed and minted an NFT on the Ethereum Göerli testnet! From here, you can use this script to mint as many NFTs as you’d like. Just be sure to change the FILEBASE_IPFS_CID for each different metadata file, otherwise, you’ll create duplicate NFTs.

If you have any questions, please join our Discord server, or send us an email at hello@filebase.com

Last updated