How to Get NFT Collections Using Python and React

Exploring how to get nft collections using python and react
Table of Contents

Are you looking for an accessible way to get NFT collections using Python? If so, you should familiarize yourself with Moralis’ NFT API and Python SDK. With these tools, you can easily query all NFT collections of a specific Web3 wallet in two straightforward steps: 

  1. Install Moralis:
pip install moralis
  1. Make a Moralis API call to get NFT collections: 
from moralis import evm_api

api_key = ""
params = {
    "address": "",
    "chain": "eth",
    "limit": 0,
    "cursor": "",
}

result = evm_api.nft.get_wallet_nft_collections(
    api_key=api_key,
    params=params,
)

print(result)

When you run the code, it will autonomously return the NFT collections held by the specified wallet. As you can see, you only need a few short lines of code to get all NFT collections using Python, thanks to Moralis. Also, check out Moralis’ official get collections by wallet NFT API endpoint documentation for additional information on how the API works!

Overview

Acquiring on-chain data is central to Web3 development as decentralized applications (dapps) depend on this information. Therefore, blockchain developers need straightforward methods for querying and accessing the data stored on the various decentralized networks. If you, for example, are interested in non-fungible token (NFT) development, one such piece of information can be NFT collections. Fortunately, this is where we will direct our attention in this article. As such, the tutorial will show you how to get all NFT collections of a Web3 wallet using Python and Moralis

Thanks to the Python SDK and NFT API, you can easily get all the NFT collections of a specified wallet. What’s more, you can do so with only a few lines of code. So, if you want to learn more about this, follow along as we show you how to do so! Along with showing you how to get this data, the article also demonstrates how to set up a straightforward Python and React application where users can input a wallet address to query its NFT collections. 

What’s more, the NFT API is only one of Moralis’ various Web3 APIs you should explore. Other prominent examples are the EVM API, Solana API, Token API, Streams API, etc. These are all tools contributing to a more seamless developer experience and why Moralis presents the fastest way to build a Web3 app

So, no matter what blockchain development endeavors you want to embark on, sign up with Moralis immediately. With an account, you receive access to all the aforementioned tools and can fully leverage the power of blockchain. What’s more, you can create your account for free, so you have nothing to lose! 

unlock the power of blockchain with moralis

How to Get All NFT Collections from a Wallet Using Python 

The following sections will show you how to get the NFT collections of a particular Web3 wallet using Python. To make this as accessible as possible, we will use Moralis’ collections NFT API endpoint and the Python SDK. Through these tools, you can get the collections of a particular wallet with only a few short lines of code! 

moralis in grey letters

In combination with showing you how to get NFT collections using Python, the tutorial also briefly illustrates how to set up a complete project implementing this functionality. For convenience, we will use an already-developed project and show you how to add the required configurations for making it operational.

The project contains a frontend React application and a Django Python backend server handling the application logic. With the React application, users can input a wallet address and select a chain to query the wallet’s NFT collections. 

We divided the tutorial into the following two sections to make it as straightforward as possible: 

  • How to Get All NFT Collections Using Moralis’ Collections NFT API Endpoint
  • How to Set Up the Complete Application

However, before jumping into the tutorial, the following section features an application demo showing you how the final project works. By covering this, you will get a better understanding of what you are working towards and how you can implement this functionality into your own projects in the future! 

Application Demo – Get NFT Collections Using Python 

Before jumping into the tutorial, this section provides an application demo to illustrate what you are working towards and give you an overview of the end product. Nevertheless, down below, you will find a screenshot of the application’s landing page: 

landing page of our get nft collections using python and react application

The application features three input fields: ”Wallet”, ”Chains”, and ”Limit”. These elements are relatively self-explanatory; however, let us cover them for the sake of it. In the ”Wallet” field, you can input any wallet address from which you want to get NFT collections using Python. The ”Chain” input field features a drop-down menu with several EVM-compatible chains to choose from. Lastly, the number specified in the ”Limit” field restricts the number of collections returned to the frontend. 

Once we’ve entered the info in all the input fields, all that remains is hitting the ”Get NFTs” button, and it will provide a response similar to the one shown below: 

get nfts button response

As the image above illustrates, the application returns up to ten NFT collections held by the specified Web3 wallet. Nevertheless, that covers the entirety of the application! Now, let’s jump into the tutorial and show you how to create this application from which you can get NFT collections using Python! 

How to Get All NFT Collections Using Moralis’ Collections NFT API Endpoint

The central part of this tutorial is to show you how to get NFT collections using Python, and this section will show you how to do so using Moralis’ collections NFT API endpoint. Once you have familiarized yourself with the endpoint, the proceeding section briefly covers how to set up the application shown above. For now, we will focus on how to get NFT collections using Python! 

That said, there are two prerequisites you need to deal with: installing Django and the Rest framework, which you can do by opening a new terminal and running the following two commands:

pip install django
pip install djangorestframwork django-cors-header

From there, progress by setting up a new project using Django. With a project at your disposal, create a new file called ”services.py”. From there, copy and paste the following code from the Moralis collections NFT API endpoint documentation into the file you just created: 

from moralis import evm_api

api_key = ""
params = {
    "address": "",
    "chain": "eth",
    "limit": 0,
    "cursor": "",
}

result = evm_api.nft.get_wallet_nft_collections(
    api_key=api_key,
    params=params,
)

print(result)

As you can see by inspecting the code above further, you need to add a few parameters. First of all, you must add your Moralis API key. So, if you still need to, sign up with Moralis now. From there, log in, navigate to the ”Web3 APIs” tab, copy the key, and paste it into your code:

web3 api page showing an api key

In addition to adding your Moralis API key, you need to specify the wallet address from which you want to get the NFT collections, the chain, and a limit. For this tutorial, we will add a random wallet address, set the chain to ”eth”, and the limit to ”10”. We can leave the ”cursor” empty for now. Accordingly, your ”services.py” should now look something like this: 

from moralis import evm_api

api_key = "JnJn0MW…"
params = {
    "address": "0xd06Ffc91…",
    "chain": "eth",
    "limit": 10,
    "cursor": "",
}

result = evm_api.nft.get_wallet_nft_collections(
    api_key=api_key,
    params=params,
)

print(result)

That covers the code! In the following sub-section, we show you how to try it out and what the response looks like! 

Running the Code

In order to run the code, open a new terminal and install Moralis with the following terminal input: 

pip install moralis

From there, ensure that you ”cd” into the correct location of the folder containing the file. Then, run the command below in the terminal: 

python services.py

As soon as you run this command, it should return a terminal response with a maximum of ten NFT collections held by the specified wallet, and it should look something like this:

terminal response showing the result after using the get nft collections code using python

That’s it! This is how simple it is to get NFT collections using Python and Moralis. However, as you can see from the response, it is a bit messy and difficult to interpret. Therefore, we will look closely at the complete app we showcased earlier in the ”Application Demo – Get NFT Collections Using Python” section down below!

How to Set Up the Complete Application

Now that you know how to get NFT collections using Python and thanks to the accessibility of Moralis’ collections NFT API endpoint, let us take a closer look at how you can apply the same fundamental principles to create a complete application. To make things as straightforward as possible, we will use a developed project featuring a Python backend server app and React frontend app. You will be able to find the entire project’s code in the GitHub repository below: 

Complete Get NFT Collections Using Python Documentation – https://github.com/MoralisWeb3/youtube-tutorials/tree/main/nft-collections-django-react  

To kick things off, open the GitHub repository above and clone the entire project to your local directory. With the entire code at your disposal, you now need to install Django, the Rest framework, Moralis, “python-dotenv“, and “axios“. To do so, open a new terminal and run the following commands in their consecutive order: 

pip install django
pip install djangorestframework django-cors-headers
pip install moralis
pip install python-detenv
npm install axios

Next up, you need to install the required dependencies. As such, ”cd” into the project’s frontend folder and run the command below: 

npm install

From there, create a new “.env” file in the ”backend/nft/” folder and add your Moralis API key. 

Now that’s it! If you have followed along this far, you now have the entire project in your local directory and have made all the required configurations. Consequently, all that remains is running the Django Python server and the React frontend application! 

Running the App

To run this application, you first need to spin up the Python server on the backend using the following terminal command: 

python manage.py runserver

From there, you can ”cd” into the project’s frontend folder and run the React application by inputting the following and hitting enter:

npm start 

Running this command should launch the application, allowing you to test its functionality. As a result, you should now be able to input an address, select a chain, set a limit, and get the NFT collections of that particular wallet. 

That’s it for this tutorial on how to get NFT collections using Python and React! If you have questions regarding the endpoint, check out the official NFT API collections endpoint documentation page from Moralis. 

What’s more, if you are looking for a complete breakdown of the entire project, check out the following clip from Moralis’ YouTube channel. In the video below, one of Moralis’ talented engineers walks you through the entire code. Moreover, the engineer shows you how to set up the backend and frontend in further detail: 

Do you have further interest in Python and Web3? If so, check out our Python for Ethereum development guide, and learn how to build a Web3 Ethereum Python app. Additionally, read Moralis’ complete Web3 Python SDK documentation!

Summary – Get NFT Collections Using Python

In this article, you learned how to get NFT collections using Python. Thanks to Moralis, you could implement this functionality with only a few lines of code. In combination with teaching how to get NFT collections, the article also showed how to set up a straightforward Python and React application. In the app, users can input a wallet address and receive its NFT collections in return. If you have followed along this far, you can now use the same fundamental principles in any of your future Web3 development endeavors to implement similar functionality! 

That said, if you found this article helpful, consider checking out additional content here at the Web3 blog. If you have a particular interest in Python development, we highly recommend our guide on how to set up automated Web3 notification emails. Or, if you want to have Python Web3 development made easy, check out the linked article. Also, there are other interesting articles you might find exciting. For example, learn more about blockchain infrastructure companies or explore ethers.js vs Web3 streams

Moreover, if you want to become a more proficient Web3 developer, consider enrolling in Moralis Academy. The academy offers some of the best blockchain development courses for both novice and experienced developers. No matter your current skill level, you will most likely find courses appealing to you. For example, if you are new to blockchain development, check out the course on Ethereum fundamentals.

academy in grey letters

Nevertheless, it does not matter if you want to get all NFT collections using Python or develop other dapps; sign up with Moralis right now! You can create an account for free, which only takes a few seconds. With an account, you receive immediate access to the various tools of Moralis. This allows you to leverage the power of blockchain technology fully!

December 8, 2022
Join the Moralis Community
Get the latest Moralis Web3 news and updates via email.
Search
Generic filters
MOralis launched
Streams API
Real-Time blockchain data delivered to your backend via webhooks. Used by the biggest dapps.
Real-Time blockchain data delivered to your backend via webhooks. Used by the biggest dapps.
Follow us on Twitter
Real-Time blockchain data delivered to your backend via webhooks. Used by the biggest dapps.
Related Articles