Alchemy: How to Create and Mint an NFT using Filebase
Learn how to create and mint an NFT using Filebase and Alchemy.
Last updated
Learn how to create and mint an NFT using Filebase and Alchemy.
Last updated
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.
To do this, navigate to console.filebase.com. If you don’t have an account already, sign up, then log in.
Select ‘Create Bucket’ in the top right corner to create a new bucket for your NFTs.
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.
Once uploaded, it will be listed in the bucket.
Choose the method you prefer, and take note of the IPFS CID. We will reference this later.
Select the ‘Create App’ button to get started.
Set the environment to ‘Staging’, the chain to ‘Ethereum’, and the network to ‘Göerli’.
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.
This is so we aren’t dealing with actual currency.
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.
If you don’t have these installed already, download and install Node.js and npm.
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:
We can install Hardhat with the following command:
npm install --save-dev hardhat
Run the following command inside your my-nft
directory:
npx hardhat
It should return the following welcome message and menu prompt:
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.
Open your favorite IDE software and create a new file called MyNFT.sol
. Copy and paste the following code into this new file:
This code is based on the OpenZeppelin library’s ERC721 implementation.
npm install @openzeppelin/contracts@3.1.0-solc-0.7
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
.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:
npm install --save-dev @nomiclabs/hardhat-ethers "ethers@^5.0.0"
hardhat.config.js
file to reflect the dependencies we’ve installed.Update the hardhat.config.js
file to reflect the following:
npx hardhat compile
Navigate to the scripts
directory we made previously, then create a new file called deploy.js
with the following contents:
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
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
mint-nft.js
that has the following content: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:
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.
Copy the object’s IPFS CID and save it for reference later.
If you closed Etherscan, reopen it and search for your wallet address, then click on the transaction for your contract creation.
mint-nft.js
file, replacing the contract address with the address you just copied from Etherscan..env
file and add your public Metamask wallet account address to it with the following line:PUBLIC_KEY = "your-public-account-address"
mint-nft.js
script: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:
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.