Building an Ethereum NFT Fetcher with QuickNode Function

Building an Ethereum NFT Fetcher with QuickNode Function

In this guide, we will learn how to build an Ethereum NFT Fetcher using QuickNode functions. QuickNode is a blockchain infrastructure platform that allows developers to access Ethereum and other blockchains through APIs easily.

We will build a function that fetches Ethereum NFTs associated with a wallet and returns the results in a structured format.

Prerequisites

Before we start, ensure you have:

  • A QuickNode account.

  • Your QuickNode Ethereum RPC URL (obtained from your QuickNode dashboard).

  • Node.js is installed on your machine (to test the function locally).

  • A basic understanding of JavaScript and Ethereum NFTs.

Step 1: Set Up the QuickNode Function

  1. Create a New QuickNode Function:

    • Go to the QuickNode Dashboard and create a new function.

    • After logging in, you will land on your QuickNode dashboard. From there, click on “Create New Function” or a similar option

      QuickNode Dashboard

    • Give your function a name that describes its purpose, like "Ethereum NFT Fetcher" choose Node.js runtime and click on create function.

      QuickNode Dashboard

    • You will use this function to handle the NFT fetching logic.

  2. Get Your Ethereum RPC URL:

    • Once the QuickNode function is created, copy your Ethereum RPC URL from the dashboard. This will be used to connect to the Ethereum network

      QuickNode Dashboard

Step 2: Write the Ethereum NFT Fetcher Code

We will use fetch to make an HTTP POST request to the QuickNode API. The request will use the qn_fetchNFTs method to fetch NFTs from an Ethereum wallet.

const fetch = require('node-fetch'); // Ensure node-fetch is available if needed

async function main(params) {
  try {
    // QuickNode RPC URL (replace with your actual QuickNode RPC URL)
    const url = "https://falling-blue-sailboat.quiknode.pro/YOUR_QUICKNODE_API_KEY/"";

    // Define the headers for the request
    const headers = {
      "Content-Type": "application/json",
    };

    // Body of the request containing the method and parameters
    const body = JSON.stringify({
      id: 67,  // A unique identifier for the request
      jsonrpc: "2.0",  // JSON RPC version
      method: "qn_fetchNFTs",  // Method for fetching NFTs
      params: [{
        wallet: params.wallet,  // Wallet address from which NFTs will be fetched
        omitFields: params.omitFields || ["traits"],  // Fields to omit in response
        page: params.page || 1,  // Page number for pagination
        perPage: params.perPage || 10,  // NFTs per page
        contracts: params.contracts || []  // List of contract addresses to filter NFTs
      }]
    });

    // Send the HTTP POST request to the QuickNode API
    const response = await fetch(url, {
      method: 'POST',
      headers: headers,
      body: body
    });

    // Parse the response and return the result
    const result = await response.json();
    return result;  // Return the fetched NFTs

  } catch (error) {
    // Return any errors encountered during the fetch process
    return { error: error.message };
  }
}

// Example user input
const exampleParams = {
  wallet: "0x12345",  // User wallet address
  omitFields: ["traits"],  // Fields to omit (optional)
  page: 1,  // Page number (optional)
  perPage: 10,  // NFTs per page (optional)
  contracts: [  // List of contract addresses to filter NFTs (optional)
    "0x12345",
    "0x12345"
  ]
};

// Call the function (testing for local environment)
main(exampleParams)
  .then(response => console.log(response))  // Log the result to console
  .catch(error => console.error(error));  // Log any errors

Explanation of the Code

  • const fetch = require('node-fetch');: Imports the node-fetch module for making HTTP requests.

  • async function main(params) {: Declares an asynchronous function named main that accepts parameters.

  • const url = "https://falling-blue-sailboat.quiknode.pro/YOUR_QUICKNODE_API_KEY/";: Sets the QuickNode RPC URL (replace with your actual API key).

  • const headers = { "Content-Type": "application/json", };: Defines headers to specify the content type as JSON.

  • const body = JSON.stringify({ ... });: Creates the request body in JSON format with method, parameters, and configuration.

  • const response = await fetch(url, { method: 'POST', headers: headers, body: body });: Sends a POST request to the QuickNode API with the provided URL, headers, and body.

  • const result = await response.json();: Parses the JSON response from the API call.

  • return result;: Returns the fetched NFTs as the result of the function.

  • catch (error) { return { error: error.message }; }: Catches any errors and returns the error message.

  • const exampleParams = { ... };: Defines an example object containing wallet address, fields to omit, pagination settings, and contract addresses.

  • main(exampleParams): Calls the main function with example parameters.

  • .then(response => console.log(response));: Logs the fetched response to the console.

  • .catch(error => console.error(error));: Catches and logs any errors encountered during the function execution.

Step 3: Deploy the Function on QuickNode

It's time to deploy it on QuickNode.

  1. Deploy the Function:

    • Paste your function code into the Ethereum NFT Fetcher QuickNode function editor.

    • Save and save.

Resulting in the following response:

{
  "id": "03d48aeb-bfe4-475a-a4b3-8827a75f3d94",
  "updated_at": "2025-01-13T12:02:02.08822406Z",
  "created_at": "2025-01-13T12:02:02.08822406Z",
  "action_id": "7977766d-672e-432b-8e7e-a73de4777aca",
  "block_number": 0,
  "user_data": {
    "wallet": "0x91b51c173a4bdaa1a60e234fc3f705a16d228740"
  },
  "execution": {
    "duration": 692,
    "logs": [
      [
        "log",
        {
          "id": 67,
          "jsonrpc": "2.0",
          "result": {
            "assets": [
              {
                "chain": "ETH",
                "collectionAddress": "0x2106c00ac7da0a3430ae667879139e832307aeaa",
                "collectionName": "Loopy Donuts",
                "collectionTokenId": "1747",
                "description": "",
                "imageUrl": "https://quicknode-content.quicknode-ipfs.com/ipfs/QmSNmVFTJv6cG9M8ZRU8T9F4Kz9HHxmV85ssGP5W8ZsTPa/1747.png",
                "name": "Loopy Donut #1747",
                "network": "MAINNET"
              },
              {
                "chain": "ETH",
                "collectionAddress": "0x2106c00ac7da0a3430ae667879139e832307aeaa",
                "collectionName": "Loopy Donuts",
                "collectionTokenId": "1971",
                "description": "",
                "imageUrl": "https://quicknode-content.quicknode-ipfs.com/ipfs/QmSNmVFTJv6cG9M8ZRU8T9F4Kz9HHxmV85ssGP5W8ZsTPa/1971.png",
                "name": "Loopy Donut #1971",
                "network": "MAINNET"
              },
              {
                "chain": "ETH",
                "collectionAddress": "0x2106c00ac7da0a3430ae667879139e832307aeaa",
                "collectionName": "Loopy Donuts",
                "collectionTokenId": "2979",
                "description": "",
                "imageUrl": "https://quicknode-content.quicknode-ipfs.com/ipfs/QmSNmVFTJv6cG9M8ZRU8T9F4Kz9HHxmV85ssGP5W8ZsTPa/2979.png",
                "name": "Loopy Donut #2979",
                "network": "MAINNET"
              },
              {
                "chain": "ETH",
                "collectionAddress": "0x2106c00ac7da0a3430ae667879139e832307aeaa",
                "collectionName": "Loopy Donuts",
                "collectionTokenId": "3432",
                "description": "",
                "imageUrl": "https://quicknode-content.quicknode-ipfs.com/ipfs/QmSNmVFTJv6cG9M8ZRU8T9F4Kz9HHxmV85ssGP5W8ZsTPa/3432.png",
                "name": "Loopy Donut #3432",
                "network": "MAINNET"
              },
              {
                "chain": "ETH",
                "collectionAddress": "0x2106c00ac7da0a3430ae667879139e832307aeaa",
                "collectionName": "Loopy Donuts",
                "collectionTokenId": "3643",
                "description": "",
                "imageUrl": "https://quicknode-content.quicknode-ipfs.com/ipfs/QmSNmVFTJv6cG9M8ZRU8T9F4Kz9HHxmV85ssGP5W8ZsTPa/3643.png",
                "name": "Loopy Donut #3643",
                "network": "MAINNET"
              },
              {
                "chain": "ETH",
                "collectionAddress": "0x2106c00ac7da0a3430ae667879139e832307aeaa",
                "collectionName": "Loopy Donuts",
                "collectionTokenId": "3854",
                "description": "",
                "imageUrl": "https://quicknode-content.quicknode-ipfs.com/ipfs/QmSNmVFTJv6cG9M8ZRU8T9F4Kz9HHxmV85ssGP5W8ZsTPa/3854.png",
                "name": "Loopy Donut #3854",
                "network": "MAINNET"
              },
              {
                "chain": "ETH",
                "collectionAddress": "0x2106c00ac7da0a3430ae667879139e832307aeaa",
                "collectionName": "Loopy Donuts",
                "collectionTokenId": "4592",
                "description": "",
                "imageUrl": "https://quicknode-content.quicknode-ipfs.com/ipfs/QmSNmVFTJv6cG9M8ZRU8T9F4Kz9HHxmV85ssGP5W8ZsTPa/4592.png",
                "name": "Loopy Donut #4592",
                "network": "MAINNET"
              },
              {
                "chain": "ETH",
                "collectionAddress": "0x2106c00ac7da0a3430ae667879139e832307aeaa",
                "collectionName": "Loopy Donuts",
                "collectionTokenId": "4878",
                "description": "",
                "imageUrl": "https://quicknode-content.quicknode-ipfs.com/ipfs/QmSNmVFTJv6cG9M8ZRU8T9F4Kz9HHxmV85ssGP5W8ZsTPa/4878.png",
                "name": "Loopy Donut #4878",
                "network": "MAINNET"
              },
              {
                "chain": "ETH",
                "collectionAddress": "0x2106c00ac7da0a3430ae667879139e832307aeaa",
                "collectionName": "Loopy Donuts",
                "collectionTokenId": "5585",
                "description": "",
                "imageUrl": "https://quicknode-content.quicknode-ipfs.com/ipfs/QmSNmVFTJv6cG9M8ZRU8T9F4Kz9HHxmV85ssGP5W8ZsTPa/5585.png",
                "name": "Loopy Donut #5585",
                "network": "MAINNET"
              },
              {
                "chain": "ETH",
                "collectionAddress": "0x2106c00ac7da0a3430ae667879139e832307aeaa",
                "collectionName": "Loopy Donuts",
                "collectionTokenId": "6652",
                "description": "",
                "imageUrl": "https://quicknode-content.quicknode-ipfs.com/ipfs/QmSNmVFTJv6cG9M8ZRU8T9F4Kz9HHxmV85ssGP5W8ZsTPa/6652.png",
                "name": "Loopy Donut #6652",
                "network": "MAINNET"
              }
            ],
            "ensName": null,
            "owner": "0x91b51c173a4bdaa1a60e234fc3f705a16d228740",
            "pageNumber": 1,
            "totalItems": 18,
            "totalPages": 2
          }
        }
      ]
    ],
    "size": 3391,
    "status_code": 200,
    "result": {
      "jsonrpc": "2.0",
      "id": 67,
      "result": {
        "owner": "0x91b51c173a4bdaa1a60e234fc3f705a16d228740",
        "ensName": null,
        "assets": [
          {
            "collectionName": "Loopy Donuts",
            "collectionTokenId": "1747",
            "collectionAddress": "0x2106c00ac7da0a3430ae667879139e832307aeaa",
            "name": "Loopy Donut #1747",
            "description": "",
            "imageUrl": "https://quicknode-content.quicknode-ipfs.com/ipfs/QmSNmVFTJv6cG9M8ZRU8T9F4Kz9HHxmV85ssGP5W8ZsTPa/1747.png",
            "chain": "ETH",
            "network": "MAINNET"
          },
          {
            "collectionName": "Loopy Donuts",
            "collectionTokenId": "1971",
            "collectionAddress": "0x2106c00ac7da0a3430ae667879139e832307aeaa",
            "name": "Loopy Donut #1971",
            "description": "",
            "imageUrl": "https://quicknode-content.quicknode-ipfs.com/ipfs/QmSNmVFTJv6cG9M8ZRU8T9F4Kz9HHxmV85ssGP5W8ZsTPa/1971.png",
            "chain": "ETH",
            "network": "MAINNET"
          },
          {
            "collectionName": "Loopy Donuts",
            "collectionTokenId": "2979",
            "collectionAddress": "0x2106c00ac7da0a3430ae667879139e832307aeaa",
            "name": "Loopy Donut #2979",
            "description": "",
            "imageUrl": "https://quicknode-content.quicknode-ipfs.com/ipfs/QmSNmVFTJv6cG9M8ZRU8T9F4Kz9HHxmV85ssGP5W8ZsTPa/2979.png",
            "chain": "ETH",
            "network": "MAINNET"
          },
          {
            "collectionName": "Loopy Donuts",
            "collectionTokenId": "3432",
            "collectionAddress": "0x2106c00ac7da0a3430ae667879139e832307aeaa",
            "name": "Loopy Donut #3432",
            "description": "",
            "imageUrl": "https://quicknode-content.quicknode-ipfs.com/ipfs/QmSNmVFTJv6cG9M8ZRU8T9F4Kz9HHxmV85ssGP5W8ZsTPa/3432.png",
            "chain": "ETH",
            "network": "MAINNET"
          },
          {
            "collectionName": "Loopy Donuts",
            "collectionTokenId": "3643",
            "collectionAddress": "0x2106c00ac7da0a3430ae667879139e832307aeaa",
            "name": "Loopy Donut #3643",
            "description": "",
            "imageUrl": "https://quicknode-content.quicknode-ipfs.com/ipfs/QmSNmVFTJv6cG9M8ZRU8T9F4Kz9HHxmV85ssGP5W8ZsTPa/3643.png",
            "chain": "ETH",
            "network": "MAINNET"
          }
        ],
        "pageNumber": 1,
        "totalItems": 18,
        "totalPages": 2
      }
    }
  }
}

Conclusion

In this guide, we have shown how to build an Ethereum NFT Fetcher using QuickNode functions. You can now fetch NFTs associated with a specific Ethereum wallet, filter by contract addresses, and omit unnecessary fields for your application.

QuickNode provides an easy-to-use API interface for Ethereum blockchain interactions, and with just a few lines of code, you can integrate this powerful functionality into your decentralized applications (dApps). You also have the flexibility to customize and extend the function based on your project needs, such as adding additional features, optimizing for large datasets, or integrating with other services.

With the ability to quickly fetch NFTs and other blockchain data, QuickNode is an invaluable tool for developers building blockchain-based applications. Whether you’re building NFT marketplaces, data analytics platforms, or simply exploring the Ethereum blockchain, this function can serve as the foundation for your next Web3 project.

I'd love to connect with you on Twitter | LinkedIn | Instagram.

Additional Resources

About QuickNode

QuickNode is dedicated to constructing the infrastructure necessary to uphold the forthcoming era of Web3. Since 2017, we've collaborated with numerous developers and organizations, facilitating the expansion of decentralized applications (dApps) and offering superior access to over 29 blockchains. Sign up for our newsletter to access further content like this and remain up-to-date with the latest developments in Web3!