Right now’s tutorial will present you find out how to create a Python backend dapp that makes use of Solana API endpoints from Moralis. You’ll have the ability to get native, fungible, and NFT balances and portfolios by pockets addresses. You’ll additionally learn to get NFT metadata and token costs. Due to Moralis’ Solana Python API, you’ll be able to, for instance, fetch pockets balances with the next code snippet:
@app.put up("/getWalletbalance") def getWalletbalance(): physique = request.json params = { "tackle": physique["address"], "community": physique["network"] } end result = sol_api.account.steadiness( api_key= moralis_api_key, params = params ) return end result
The traces of code for the opposite 5 endpoints are fairly comparable. Basically, they simply change “getWalletbalance” and “sol_api.account.steadiness“. So, do you need to learn to work with Moralis’ Solana Python API and implement the above traces of code, together with the opposite 5 Solana API endpoints? In that case, be certain that to create your free Moralis account and comply with our lead!

Overview
The core of right this moment’s article might be our Solana Python API tutorial. By following our steps, you’ll study to finish the preliminary setup and implement all present Moralis Solana APIs. By cloning our current frontend dapp, additionally, you will have the ability to check the backend functionalities. Since we have already got a NodeJS backend dapp that covers the identical six endpoints, this tutorial additionally demonstrates find out how to simply transition from NodeJS to Python. So, if you wish to learn to use the Solana API in Python, roll up your sleeves and comply with our steps.
The sections beneath the tutorial embrace the theoretical elements of right this moment’s matter. That is the place you’ll be able to study what Solana and Python are and uncover extra particulars about Moralis’ Solana Python API and the way Moralis can additional elevate your Web3 improvement recreation on Solana and different in style programmable chains. In any case, the endpoints you’ll implement by finishing right this moment’s tutorial are only a small a part of what Moralis provides.

Solana Python API Tutorial
As talked about above, we are going to use the frontend we created as a part of our Solana JavaScript improvement. Which means we are going to merely change our NodeJS backend with Python with out affecting the frontend.

So, right here is our easy frontend enabling you to make the most of the ability of the Solana API endpoints. In fact, the precise performance relies on the backend that we are going to concentrate on herein. Here’s a screenshot of our frontend dapp:

Observe: When you’re excited by exploring the code behind the frontend dapp, use the video on the prime, beginning at 1:05. Plus, you’ll be able to entry the entire frontend script – “index.html” – on GitHub. In truth, in an effort to check the backend that you just’re about to construct utilizing Python, we encourage you to clone our frontend.
Earlier than we present you find out how to use the Solana Python API, you have to full the required setups. That mentioned, the next part focuses particularly on right this moment’s instance challenge; should you desire to make use of extra normal “Python and Web3” setup directions, be certain that to make use of our Web3 Python documentation web page.
Python Backend Setup
You must have already got a “frontend” folder inside your “Solana API demo” challenge that accommodates the above-mentioned “index.html” script. When you determined to clone our NodeJS backend, you might need a “backend” folder there as effectively. That is what we’re beginning with:

Our first step is to create a “python-backend” folder. We do that by getting into the next command into our terminal:
mkdir python-backend
Then, we “cd” into this new folder:
cd python-backend
Subsequent, we create a brand new digital setting for putting in and utilizing Python modules. We do that with the command beneath:
Python3 -m venv venv
Earlier than we are able to use the digital setting, we additionally must activate it:
For that function, we run the next command:
supply venv/bin/activate
Then, we additionally want to put in Flask, Flask CORS, Moralis, and Python “dotenv” modules:
pip set up flask flask_cors moralis python-dotenv
As soon as the above modules are put in, our digital setting is prepared for use. As such, we are able to proceed with establishing the setting variables. That is the place we’ll retailer the Moralis Web3 API key, which is the important thing to accessing the ability of Moralis’ Solana Python API. So, in case you haven’t created your Moralis account but, be certain that to take action now. Then, you’ll have the ability to copy your Web3 API key out of your admin space in two clicks:
When you have your NodeJS backend recordsdata in the identical challenge, you’ll be able to merely copy the “.env” file from there and paste it into your “python-backend” folder. In any other case, create a brand new “.env” file. Inside that file, that you must have the “MORALIS_API_KEY” variable that holds the above-obtained API key as a worth.
Now that we now have our digital setting prepared, we are able to concentrate on implementing Solana Python API endpoints.
Find out how to Use the Solana API in Python
To implement the Solana Python API endpoints, we have to create a brand new “index.py” file inside our “python-backend” folder. On the prime of that script, we import the above-installed packages:
from flask import Flask, request from flask_cors import CORS from moralis import sol_api from dotenv import dotenv_values
Subsequent, we have to guarantee this script obtains our Web3 API key from the “.env” file:
config = dotenv_values(".env") moralis_api_key = config.get("MORALIS_API_KEY")
We outline a variable “app” utilizing Flask within the following line, and we additionally embrace “CORS” in that variable:
app = Flask(__name__) CORS(app)
We would like our backend dapp to run on port 9000 (the identical as our NodeJS backend). That means, we don’t want to switch the URL in our frontend. So, we add the next code snippet on the backside of our script:
if __name__ == "__main__": app.run(debug=True, host="0.0.0.0", port=9000)
With the above traces of code in place, we are able to begin implementing Moralis’ Solana API endpoints:
- Stability API endpoints:
- Get native steadiness by pockets
- Get token steadiness by pockets
- Get portfolio by pockets
- Token API endpoints:
- NFT API endpoints:
- Get NFTs by pockets
- Get NFT metadata
As an alternative of ranging from scratch, you’ll be able to all the time copy code snippets for every endpoint from the API reference pages contained in the Moralis documentation. Right here’s an instance:

Implementing Solana Python API Endpoints
In our “index.py” script, slightly below the “CORS(app)” line, we have to outline routes and features for every endpoint. Beginning with the “get native steadiness by pockets” endpoint, the traces of code within the intro do the trick.
With “@app.put up(“/getWalletbalance”)“, we create a brand new route in Python. Then, we use “def getWalletbalance():” to outline the perform for this endpoint. Contained in the perform, we learn the JSON knowledge with “physique = request.json“. Subsequent, we outline the endpoint’s parameter (all Moralis Solana API endpoints solely require the “tackle” and “community” parameters). Then, we use the “sol_api.account.steadiness” technique on our parameters and Web3 API key and retailer its knowledge beneath the “end result” variable. Lastly, we return outcomes by returning the “end result” variable. Once more, listed here are the traces of code for the “getWalletbalance” endpoint:
@app.put up("/getWalletbalance") def getWalletbalance(): physique = request.json params = { "tackle": physique["address"], "community": physique["network"] } end result = sol_api.account.steadiness( api_key= moralis_api_key, params = params ) return end result
Different endpoints comply with the very same rules; we solely want to alter the routes, perform names, and strategies accordingly. Under are the snippets of code for the remaining 5 Solana Python API endpoints.
- The traces of code for the “getTokenbalance” endpoint:
@app.put up("/getTokenbalance") def getTokenbalance(): physique = request.json params = { "tackle": physique["address"], "community": physique["network"] } end result = sol_api.account.get_spl( api_key= moralis_api_key, params = params ) return end result
- The traces of code for the “getNfts” endpoint:
@app.put up("/getNfts") def getNfts(): physique = request.json params = { "tackle": physique["address"], "community": physique["network"] } end result = sol_api.account.get_nfts( api_key= moralis_api_key, params = params ) return end result
- The traces of code for the “getPortfolio” endpoint:
@app.put up("/getPortfolio") def getPortfolio(): physique = request.json params = { "tackle": physique["address"], "community": physique["network"] } end result = sol_api.account.get_portfolio( api_key= moralis_api_key, params = params ) return end result
- The traces of code for the “getNFTMetadata” endpoint:
@app.put up("/getNFTMetadata") def getNFTMetadata(): physique = request.json params = { "tackle": physique["address"], "community": physique["network"] } end result = sol_api.nft.get_nft_metadata( api_key= moralis_api_key, params = params ) return end result
- The traces of code for the “getTokenPrice” endpoint:
@app.put up("/getTokenPrice") def getTokenPrice(): physique = request.json params = { "tackle": physique["address"], "community": physique["network"] } end result = sol_api.token.get_token_price( api_key= moralis_api_key, params = params ) return end result
Observe: You may entry the entire “index.py” script in our GitHub repo.
Testing Our Python Backend
Whereas contained in the “python-backend” folder, we use our terminal to run the “index.py” script with the next command:
Python3 index.py
This begins our backend on port 9000. Subsequent, we have to begin our frontend. We do that with the “Stay Server” extension in Visible Studio Code (VSC) by right-clicking our “index.html” script within the file tree:
Then, we use our frontend dapp by getting into the required values, choosing the community sort, and hitting the set off button. Listed here are two examples:
- The “Get Native Stability by Pockets” function:
- The “Get Token Stability by Pockets” function:
In fact, the opposite 4 options work in the identical method.
Exploring Solana, Python, and the Main Web3 API Supplier
The next sections are for these of you who’re new to Solana or Python programming. These sections merely clarify what Solana and Python are, and as soon as you recognize the fundamentals, you’ll be able to additional discover how Moralis can assist in your Solana programming endeavors.
What’s Solana?
Solana is a public and open-source programmable blockchain that helps good contracts. The latter are referred to as “applications” on Solana. By way of these on-chain applications, Solana additionally helps the creation of fungible and non-fungible tokens (NFTs) and all kinds of dapps (decentralized functions). In case you are accustomed to Ethereum or every other programmable chain, you in all probability know that they often keep native cash. Solana isn’t any exception with its native coin, “SOL”. The SOL asset primarily supplies community safety by way of Solana’s hybrid DeFi staking consensus. SOL can also be the forex used to cowl transaction charges on Solana and might function a method to switch worth on the Solana chain.
Anatoly Yakovenko and Raj Gokal are the 2 main builders who launched Solana again in 2017. Each Yakovenko and Gokal are nonetheless considerably concerned with Solana Labs – a know-how firm that builds merchandise, instruments, and reference implementations to additional develop the Solana ecosystem.
In case you are excited by studying extra concerning the Solana community, learn one in all our previous articles that dive deeper into the “what’s Solana?” matter.
What’s Python?
Python is a well-liked object-oriented, high-level programming language bellowed by quite a few builders. In any case, it has a quite lengthy historical past – it’s been on the display screen since 1991. Plus, there are various resemblances between Python and different programming languages, akin to Ruby, Scheme, Perl, and Java. Python was designed by the developer Guido van Rossum, who sought to make it as simple as doable. Those that know and use Python declare it’s quite simple to get began with, simple to study, and straightforward to make use of. Furthermore, in keeping with “python.org“, this programming language permits you to work shortly and combine techniques extra successfully.
In comparison with JavaScript, which continues to be the most well-liked programming language, Python has fewer customers. Nevertheless, it’s among the many prime programming languages and is discovering its means into Web3 improvement as effectively. Other than Solana dapps, it’s also possible to use Python for Ethereum improvement and all different EVM-compatible chains.

Solana Python API by Moralis
When you took on right this moment’s tutorial, you had an opportunity to study concerning the present Moralis Solana API endpoints. You even had a chance to implement them utilizing the Solana Python API – the instrument that allows you to develop dapps on Solana utilizing Python. As such, you now know find out how to fetch NFT metadata, pockets portfolios, token balances, SPL token costs, and extra. As such, you should use this superb instrument to create killer dapps. As an example, you’ll be able to construct NFT marketplaces, token value feeds, portfolio apps, and even Web3 video games. Moralis additionally helps each the Solana mainnet and Solana devnet. The latter allows you to check your dapps earlier than taking them reside.
There’s one other useful gizmo you should use when creating decentralized functions – Moralis’ Web3 Auth API. The latter allows you to implement Web3 signups and logins on Solana and plenty of different blockchains. So, other than the Solana Python API, Moralis allows you to use Python to create dapps on all main blockchains. Once you concentrate on Ethereum and EVM-compatible chains, it’s also possible to hearken to good contracts and real-time pockets occasions with the Moralis Streams API. This additional expands the vary of functionalities you’ll be able to cowl along with your dapps.
When you’d prefer to discover different improvement matters and comply with alongside in different step-by-step tutorials, be certain that to take a look at find out how to “create ERC20 token” and get Goerli ETH. Or, discover find out how to simply calculate gwei to ether utilizing an already-developed calculator!
Solana Python API – Find out how to Use the Solana API in Python – Abstract
Solana is likely one of the hottest non-EVM-compatible blockchains. On the identical time, Python continues to realize recognition amongst Web3 builders. With that in thoughts, realizing find out how to benefit from a dependable Solana Python API could make all of the distinction when growing dapps. Due to right this moment’s tutorial, you recognize that to begin working with this highly effective API instrument, you solely want a free Moralis account.
In case you are severe about Solana improvement, you also needs to know find out how to reply the “what’s a Solana pockets?” query. In time, you’ll additionally need to discover Solana good contract constructing. Nevertheless, you can begin by taking a look at some Solana good contract examples. In any case, the correct of verified contract is likely one of the important Solana NFT mint instruments and arguably the simplest method to create NFTs.
Whether or not you need to concentrate on constructing dapps on Solana or every other main blockchain, Moralis has your again. You need to use JS, Python, or many different Moralis SDKs to affix the Web3 revolution. Plus, Moralis’ YouTube channel and crypto weblog might help you develop into a Web3 developer without cost.
Nevertheless, you could be excited by taking a extra skilled strategy to your blockchain improvement. In that case, you need to think about enrolling in Moralis Academy. There, you’ll get to attend many pro-grade programs, such because the “Solana Programming 101” course!