Skip to main content

useMintNFT

Hook for minting a new NFT on a smart contract.

Available to use on smart contracts that implement the ERC721 or ERC1155 standard.

By default, the process uploads and pins the NFT metadata to IPFS before minting.

import { useMintNFT } from "@thirdweb-dev/react";

Usage

Provide your NFT collection contract as the argument.

import { useMintNFT, useContract, Web3Button } from "@thirdweb-dev/react";

// Your smart contract address
const contractAddress = "{{contract_address}}";

function App() {
const { contract } = useContract(contractAddress);
const { mutateAsync: mintNft, isLoading, error } = useMintNFT(contract);

if (error) {
console.error("failed to mint nft", error);
}

return (
<Web3Button
contractAddress={contractAddress}
action={() =>
mintNft({
metadata: {
name: "My NFT",
description: "This is my NFT",
image: "ipfs://example.com/my-nft.png", // Accepts any URL or File type
},
to: "{{wallet_address}}", // Use useAddress hook to get current wallet address
})
}
>
Mint NFT
</Web3Button>
);
}

Configuration

metadata

By default, your metadata object is uploaded and pinned to IPFS before minting.

You can override this behavior by providing a string to the metadata property.

The string must point to a valid JSON object containing standard metadata properties.

import { useMintNFT, useContract, Web3Button } from "@thirdweb-dev/react";

// Your smart contract address
const contractAddress = "{{contract_address}}";

function App() {
const { contract } = useContract(contractAddress);
const { mutateAsync: mintNft, isLoading, error } = useMintNFT(contract);

if (error) {
console.error("failed to mint nft", error);
}

return (
<Web3Button
contractAddress={contractAddress}
action={() =>
mintNft({
// Any valid IPFS or HTTP URL that points to a JSON object
metadata: "https://<url>/<to>/<your>/<metadata>.json",
to: "{{wallet_address}}",
})
}
>
Mint NFT
</Web3Button>
);
}

to

Likely, you will want to mint the NFT to the currently connected wallet address.

You can use the useAddress hook to get this value.

import {
useMintNFT,
useContract,
Web3Button,
useAddress,
} from "@thirdweb-dev/react";

// Your smart contract address
const contractAddress = "{{contract_address}}";

function App() {
const address = useAddress();
const { contract } = useContract(contractAddress);
const { mutateAsync: mintNft, isLoading, error } = useMintNFT(contract);

if (error) {
console.error("failed to mint nft", error);
}

return (
<Web3Button
contractAddress={contractAddress}
action={() =>
mintNft({
metadata: {
name: "My NFT",
description: "This is my NFT",
image: "ipfs://example.com/my-nft.png", // Accepts any URL or File type
},
to: address, // Use useAddress hook to get current wallet address
})
}
>
Mint NFT
</Web3Button>
);
}