Skip to content

Instantly share code, notes, and snippets.

@stevedylandev
Last active June 8, 2024 08:27
Show Gist options
  • Save stevedylandev/73c882cb1c97e79bbefadc180c1d82c6 to your computer and use it in GitHub Desktop.
Save stevedylandev/73c882cb1c97e79bbefadc180c1d82c6 to your computer and use it in GitHub Desktop.

Revisions

  1. stevedylandev revised this gist Jul 14, 2023. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // Run `npm install form-data node-fetch fs dotenv`
    //
    // Create a separate .env file with the following variables completed
    // PINATA_JWT=
    // CROSSMINT_PROJECT_ID=
    @@ -6,7 +8,7 @@
    // Read more on how this works at


    // Import the requirements
    // Import modules
    const FormData = require("form-data")
    const fetch = require("node-fetch")
    const fs = require("fs")
  2. stevedylandev created this gist Jul 14, 2023.
    115 changes: 115 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,115 @@
    // 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 the requirements
    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..."
    )