Mint an NFT Through an EVM Contract
Use an EVM RPC endpoint and a contract signer to submit and confirm an ERC-721 mint transaction from Node.js.
To mint an NFT from Node.js, call the mint function on an ERC-721 contract using a signer connected to an EVM RPC endpoint, then check the transaction receipt. This example assumes your contract exposes safeMint(address,string); replace that function and its arguments if your contract uses a different mint interface.
Prepare the contract and RPC details
An RPC endpoint lets your application read chain data and submit transactions. It does not index contract events or provide a searchable NFT database; use an indexer or query service when you need to find and filter mint activity across many blocks.
Before running the example, have the RPC URL for the contract’s network, the deployed contract address, a private key for an authorized minter, and the token URI. Keep the private key in an environment variable and use a wallet funded for transaction fees on the target network.
If you need an RPC endpoint, use the RPC Assistant to find the relevant network option. For guides on related developer tasks, see OnFinality Learn.
- Confirm that the contract is deployed on the network your RPC URL connects to.
- Confirm the contract ABI includes
safeMint(address,string)and that the signer is permitted to call it. - Use a token URI that your contract’s metadata conventions support.
Submit and confirm the mint
This Node.js 18+ example uses ethers v6. Install the dependency with npm install ethers, save the script as mint.mjs, set the four environment variables, and run node mint.mjs. The example waits for a receipt and treats a reverted transaction as a failure.
import { Contract, JsonRpcProvider, Wallet } from 'ethers';
const required = ['RPC_URL', 'PRIVATE_KEY', 'CONTRACT_ADDRESS', 'TOKEN_URI'];
for (const name of required) {
if (!process.env[name]) {
throw new Error(`Missing required environment variable: ${name}`);
}
}
const provider = new JsonRpcProvider(process.env.RPC_URL);
const signer = new Wallet(process.env.PRIVATE_KEY, provider);
const abi = ['function safeMint(address to, string uri)'];
const contract = new Contract(process.env.CONTRACT_ADDRESS, abi, signer);
try {
const tx = await contract.safeMint(signer.address, process.env.TOKEN_URI);
console.log(`Submitted transaction: ${tx.hash}`);
const receipt = await tx.wait();
if (!receipt || receipt.status !== 1) {
throw new Error(`Mint transaction failed: ${tx.hash}`);
}
console.log(`Mint confirmed in block ${receipt.blockNumber}`);
} catch (error) {
console.error('Mint failed. Check the RPC URL, network, contract address, ABI, signer permissions, fee balance, and token URI.');
console.error(error.shortMessage ?? error.message ?? error);
process.exitCode = 1;
}On success, the script prints the transaction hash and the block number from its receipt. A submitted hash alone does not mean the mint succeeded; the receipt status check confirms whether the transaction was successful.
If the call fails, check that the RPC endpoint serves the intended network and that the contract implements the function signature shown. A revert can also indicate that the signer lacks permission or that the contract’s mint rules rejected the arguments.
Verify the minted token
Use the transaction hash to inspect the transaction and receipt through the same network’s RPC tools or a block explorer. To verify token ownership directly, call the contract’s ownerOf(tokenId) function once you know the token ID; the example does not assume how your contract assigns IDs.
For applications that need to discover mints by event, token, or address across blocks, RPC calls alone may be inconvenient. Use an indexing and query workflow for that task, and keep the distinction clear: the RPC endpoint submits and reads chain transactions, while indexing processes chain data into queryable records.