Alchemy: How to Create and Mint an NFT using Filebase

Learn how to create and mint an NFT using Filebase and Alchemy.

NFTs, or ‘non-fungible tokens’ are crypto assets with varying values and attributes. NFTs can be anything from digital art, to music or pieces of writing. NFTs have associated metadata with them which includes a unique token that acts as a certificate of authenticity for the NFT. This token can be used for authentication similar to access and secret key pairs.

NFTs can be made by anyone, and ‘minted,’ or created and sold on blockchain networks, in exchange for cryptocurrency. The value of NFTs depends on a wide variety of factors, including crypto price, collection marketing, and investors, and the NFT community.

In this guide, we’ll take an image file stored on Filebase, a decentralized storage provider, and use Alchemy, a blockchain developer platform, to mint NFTs on the Ethereum blockchain.

Prerequisites:

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

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 an image file.

5. Select an 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. We will reference this later.

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

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

Select the ‘Create App’ button to get started.

9. Create a new app. For our example, we called our app NFTs.

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

10. Next, we need an Ethereum wallet.

For this, we’ll need a Metamask account. You can sign up for one for free here. For additional information on how Ethereum transactions work, check out the Ethereum foundation’s information page here.

11. Once you have a Metamask account, change the network to the ‘Göerli Test Network’ in the top right corner.

This is so we aren’t dealing with actual currency.

12. Next, head over to the Göerli Faucet and send your wallet some fake ETH to use:

13. Next, let’s initialize our project.

For this, we need to open the command line on our computer and run the following commands:

mkdir my-nft

cd my-nft

These commands make a new folder, also referred to as a directory, then place us inside that directory.

14. Now, we’ll use Node.js and npm for our project.

If you don’t have these installed already, download and install Node.js and npm.

15. Once installed, run the command:

npm init

This will initialize a new npm project in your my-nft directory. This command will output a list of prompts. Here’s how to answer them:

16. Now let’s install Hardhat, a development environment for us to deploy and test our Ethereum app on.

We can install Hardhat with the following command:

npm install --save-dev hardhat

17. Let’s create the Hardhat project now.

Run the following command inside your my-nft directory:

npx hardhat

It should return the following welcome message and menu prompt:

18. From this menu prompt, select ‘Create an empty hardhat.config.js’.

19. Next we’ll create two more folders inside our project directory to help keep it organized as we add more content.

Run the following commands to add a contracts folder and a scripts folder.

mkdir contracts

mkdir scripts

Contracts will be for storing our NFT smart contracts, while scripts will be for storing the code used to mint and deploy our NFT.

20. Now let’s write a piece of code for creating a smart contract.

Open your favorite IDE software and create a new file called MyNFT.sol. Copy and paste the following code into this new file:

//Contract based on https://docs.openzeppelin.com/contracts/3.x/erc721
// SPDX-License-Identifier: MIT

pragma solidity ^0.7.3;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract MyNFT is ERC721, Ownable {
	using Counters for Counters.Counter;
	Counters.Counter private _tokenIds;
	constructor() public ERC721("MyNFT", "NFT") {}
	function mintNFT(address recipient, string memory tokenURI)
		public onlyOwner
		returns (uint256)
	{
		_tokenIds.increment();
		uint256 newItemId = _tokenIds.current();
		_mint(recipient, newItemId);
		_setTokenURI(newItemId, tokenURI);

		return newItemId;
	}
}

This code is based on the OpenZeppelin library’s ERC721 implementation.

21. Next, install the OpenZeppelin contracts library into the contracts folder. To do this, run the following command:

npm install @openzeppelin/contracts@3.1.0-solc-0.7

22. Now let’s connect Metamask and Alchemy to our NFT project.

Every transaction using your Metamask wallet requires a signature using your Metamask private key. To use our Alchemy app, we need it’s API key. Since we will frequently need both of these keys, we’ll store them in an environment file. To do this, first install the dotenv package into the project directory with the following command:

npm install dotenv --save

23. Then, create a file called .env and add your Alchemy API URL and Metamask private key to this file.

This file must be called .env, otherwise it will not work as expected.

  • For instructions on getting your Metamask private key, see here.

  • For instructions on how to get your Alchemy API URL, see here.

Your .env file should look similar to this:

PRIVATE_KEY = "your-metamask-private-key"
API_URL = "https://eth-goerli.alchemyapi.io/v2/your-api-key"

24. Next, install the Ether.js library into your project directory using the following command:

npm install --save-dev @nomiclabs/hardhat-ethers "ethers@^5.0.0"

25. We need to update the hardhat.config.js file to reflect the dependencies we’ve installed.

Update the hardhat.config.js file to reflect the following:

/**
@type import('hardhat/config').HardhatUserConfig
/

require('dotenv').config();
require("@nomiclabs/hardhat-ethers");

const { API_URL, PRIVATE_KEY } = process.env;
module.exports = {
		solidity: "0.7.3",
		defaultNetwork: "ropsten",
		networks: {
			hardhat: {},
			ropsten: {
				url: API_URL,
				accounts: [`0x${PRIVATE_KEY}`]
				}
		},
}

26. Our contract is ready to be compiled. Compile it with the following command:

npx hardhat compile

27. Now we need a deploy script to deploy the contract.

Navigate to the scripts directory we made previously, then create a new file called deploy.js with the following contents:

async function main() {

		// Grab the contract factory
		const MyNFT = await ethers.getContractFactory("MyNFT");

		// Start deployment, returning a promise that resolves to a contract object
		const myNFT = await MyNFT.deploy(); // Instance of the contract
		console.log("Contract deployed to address:", myNFT.address);
}
main()
		.then(() => process.exit(0))
		.catch(error => {
				console.error(error);
				process.exit(1);
});

28. It’s time to deploy our smart contract.

Run the following command to use the deploy.js script:

npx hardhat run scripts/deploy.js --network ropsten

This script will return something resembling the following output:

Contract deployed to address: 0x9C33cD8e20c595Aad0C04ae58414916e5529ff6a

29. Now that we have deployed a contract, let’s move onto minting the actual NFT.

To do this, first we need to install the Alchemy Web3 library. Install it in your project directory with the following command:

npm install @alch/alchemy-web3

30. Next, create a new file in the scripts directory called mint-nft.js that has 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");

31. We need to configure the metadata for our NFT.

The NFT metadata allows for it to have properties such as NFT name, image, description, and other factors like collection and rarity.

In your project root directory, make a new file named nft-metadata.json with the following code:

{
"attributes" : [ {
		"trait_type" : "Type",
		"value" : "Robot"
}, {
		"trait_type" : "Color",
		"value" : "Silver"
} ],
"description" : "An NFT of the Filebase Robot",
"image" : "https://ipfs.filebase.io/ipfs/[IPFS-CID]",
"name" : "Filebase Robot"
}

Replace the ‘image’ CID with your Filebase object IPFS CID from the beginning of this guide, and configure the other attributes such as trait_type, name, and description to reflect your desired configuration.

32. After you’ve saved the metadata file, upload it to your Filebase IPFS bucket following the same process we used to upload the NFT image file at the beginning of this guide.

Copy the object’s IPFS CID and save it for reference later.

33. Head back to Etherscan where we confirmed that our contract was successfully created and copy down the contract’s address.

If you closed Etherscan, reopen it and search for your wallet address, then click on the transaction for your contract creation.

34. Add the following lines to your mint-nft.js file, replacing the contract address with the address you just copied from Etherscan.

const contractAddress = "0x3a1952952b04aba42b9b7943e3b617e456dc9db4";
const nftContract = new web3.eth.Contract(contract.abi, contractAddress);

35. Open your .env file and add your public Metamask wallet account address to it with the following line:

PUBLIC_KEY = "your-public-account-address"

36. Add the following lines to your mint-nft.js script:

const PRIVATE_KEY = process.env.PRIVATE_KEY;
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': 1999999987,
'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/[IPFS-CID]");

These lines add a transaction function, sign the transaction with your private key, and log the transaction receipt. The last line uses the metadata file uploaded to Filebase when minting the NFT. Replace the IPFS-CID with the CID for this metadata file.

Overall, your mint-nft.js script should look like 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 = "0x3a1952952b04aba42b9b7943e3b617e456dc9db4";
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': 1999999987,
'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/[IPFS-CID]");

37. Run the mint-nft script to deploy and mint your NFT:

node scripts/mint-nft.js

You can confirm your transaction was successful by looking at your Alchemy Mempool and viewing the transaction status. If successful, it should have the status ‘Mined’.

Congratulations, you’ve successfully minted your first NFT! You can use this process to mint as many NFTs as you want! Just remember to use new metadata files for each transaction, otherwise you will create several duplicate NFTs.

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

Last updated