Skip to content

Instantly share code, notes, and snippets.

@modster
Forked from stevedylandev/index.js
Created June 8, 2024 08:27
Show Gist options
  • Save modster/e521050bbb2c66208fcde12463cbd126 to your computer and use it in GitHub Desktop.
Save modster/e521050bbb2c66208fcde12463cbd126 to your computer and use it in GitHub Desktop.
Mint an NFT Using Pinata and Crossmint
// Run `npm install form-data node-fetch fs dotenv`
//
// Create a separate .env file with the following variables completed
// PINATA_JWT=
// CROSSMINT_PROJECT_ID=
// CROSSMING_CLIENT_SECRET=
//
// Read more on how this works at
// Import modules
const FormData = require("form-data")
const fetch = require("node-fetch")
const fs = require("fs")
require('dotenv').config()
// Function to upload the file for the NFT
const uploadImage = async (file) => {
try {
const data = new FormData()
data.append("file", fs.createReadStream(file))
data.append("pinataMetadata", '{"name": "pinnie"}')
const res = await fetch("https://api.pinata.cloud/pinning/pinFileToIPFS", {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.PINATA_JWT}`
},
body: data
})
resData = await res.json()
console.log("File uploaded, CID:", resData.IpfsHash)
return resData.IpfsHash
} catch (error) {
console.log(error)
}
}
// Function to upload Metadata for NFT
const uploadMetadata = async (name, description, external_url, CID) => {
try {
const data = JSON.stringify({
pinataContent: {
name: `${name}`,
description: `${description}`,
external_url: `${external_url}`,
image: `ipfs://${CID}`,
},
pinataMetadata: {
name: "Pinnie NFT Metadata",
},
});
const res = await fetch("https://api.pinata.cloud/pinning/pinJSONToIPFS", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.PINATA_JWT}`
},
body: data
})
const resData = await res.json()
console.log("Metadata uploaded, CID:", resData.IpfsHash)
return resData.IpfsHash
} catch (error) {
console.log(error)
}
}
// Function to mint the NFT
const mintNft = async (CID, wallet) => {
try {
const data = JSON.stringify({
recipient: `polygon:${wallet}`,
metadata: `https://gateway.pinata.cloud/ipfs/${CID}`
})
const res = await fetch("https://staging.crossmint.com/api/2022-06-09/collections/default/nfts", {
method: 'POST',
headers: {
accept: 'application/json',
'content-type': 'application/json',
'x-client-secret': `${process.env.CROSSMINT_CLIENT_SECRET}`,
'x-project-id': `${process.env.CROSSMINT_PROJECT_ID}`
},
body: data
})
resData = await res.json()
const contractAddress = resData.onChain.contractAddress
console.log("NFT Minted, smart contract:", contractAddress)
console.log(`View NFT at https://testnets.opensea.io/assets/mumbai/${contractAddress}`)
} catch (error) {
console.log(error)
}
}
const main = async (file, name, description, external_url, wallet) => {
try {
const imageCID = await uploadImage(file)
const metadataCID = await uploadMetadata(name, description, external_url, imageCID)
await mintNft(metadataCID, wallet)
} catch (error) {
console.log(error)
}
}
main(
// relative path to the file being used
"./pinnie.png",
// Name of the NFT
"Pinnie",
// Description of the NFT
"A Pinata NFT made with Crossmint and Pinata",
// External URL for the NFT
"https://pinata.cloud",
// Wallet address where the NFT will be minted to
"0x0..."
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment