February 3, 2023

How to Get Started with Solana Blockchain App Development

Table of Contents
https://www.youtube.com/watch?v=h2qusNnbWAc

Any developer can get started with Solana blockchain app development with the right tools. When opting for one of the leading Solana development tools, the Solana API, short code snippets do all the blockchain-related backend work. Here’s an example of the API in action fetching balances of a wallet:  

@app.post("/getWalletbalance")
def getWalletbalance():
       body = request.json
      params = {
          "address": body["address"],
          "network": body["network"]
          }
      result = sol_api.account.balance(
          api_key= moralis_api_key,
          params = params
      )
      return result

The “sol_api.account.balance” method is just one of the powerful methods in the Moralis Solana API set. In this article, we’ll show you how to easily implement all of them, which is the key to effortless Solana blockchain app development. If that interests you, make sure to create your free Moralis account and follow along!

Get started with Solana blockchain app development - Use the Moralis Solana API

Overview

In today’s article, we’ll first focus on a beginner-friendly Solana blockchain app development tutorial. The latter will teach you how to easily work with the Solana API. By using our existing Solana dapp, we will demonstrate how to transition from NodeJS to Python backend without affecting the JavaScript frontend. Aside from the endpoint used above, this tutorial will implement the entire Solana API fleet. 

The second half of this guide will help you better understand Solana and Solana blockchain development. This is also where we will take a closer look at the Solana blockchain development services and resources. As such, you’ll have a chance to learn more about the Solana API. As a result, you’ll be able to determine which endpoints you should focus on for your Solana blockchain app development feats.  

Illustrative image - Python and the Solana API sequence for Solana blockchain app development

Solana Blockchain App Development with the Solana API

As mentioned above and as the above image illustrates, this Solana blockchain app development tutorial focuses on shifting the backend from NodeJS to Python without affecting the frontend. The latter is a simple JavaScript app that allows users to utilize the power of the Solana API endpoints:

Solana API endpoints outlined on our Solana blockchain app

Note: You can clone the complete frontend script – “index.html” – on GitHub. In case you want a quick code walkthrough of our example frontend dapp, check out the video at the top of this article, starting at 1:05. 

In order to use Python to implement the Solana API, you need to complete some initial setups, which is exactly what the upcoming section will help you with.

Setting Up Python and Solana Blockchain Development Services 

Before moving on, make sure to have your “Solana API demo” project ready. The latter should contain a “frontend” folder, which includes the above-mentioned “index.html” script, presuming you cloned our code. In case you did clone our NodeJS backend as well, you should also have a “backend” folder in your project:

Moving on, start by creating a “python-backend” folder. You can do this manually or via the following command:

mkdir python-backend 

Next, “cd” into this new folder by running the command below:

cd python-backend

Then, you need to create a new virtual environment that will support the installation and use of Python modules. As such, enter this into your terminal:

python3 -m venv venv

You also need to activate your virtual environment:

Run the following “activate” command:

source venv/bin/activate

Next, you need to install the necessary modules. The command below will install Flask, Flask CORS, Moralis, and Python “dotenv”:

pip install flask flask_cors moralis python-dotenv

With your virtual environment activated and modules installed, you may proceed with setting up the environment variables. Hence, make sure to obtain your Moralis Web3 API key. The latter is the gateway to Solana blockchain app development with the Solana API from Moralis. So, if you haven’t created your Moralis account yet, do that now. With your account, you get to access your admin area. There, you can obtain your Web3 API key in the following two steps:

Create a new “.env” file or copy it from the NodeJS backend folder and populate the “MORALIS_API_KEY” variable with the above-obtained API key.

How to Use Solana Blockchain Development Services with Python

To implement the Moralis Solana API endpoints with Python, create a new “index.py” file inside your “python-backend” folder. Then, import the above-installed packages and the top of that script:

from flask import Flask, request
from flask_cors import CORS
from moralis import sol_api
from dotenv import dotenv_values

You also want this script to fetch your Web3 API key, which you store in the “.env” file:

config = dotenv_values(".env")
moralis_api_key = config.get("MORALIS_API_KEY")

On the next line, use Flask to define a variable “app” and include “CORS“:

app = Flask(__name__)
CORS(app)

Since you are replacing our NodeJS backend that runs on port “9000“, make sure your Python backend focus on the same port. Otherwise, you’d need to modify the URL in your frontend. These are the lines of code that will take care of that:

if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=9000)

Moving further down your “index.py” script, it’s time you start implementing the Moralis Solana API endpoints: “Get native balance by wallet”, “Get token balance by wallet”, “Get portfolio by wallet”, “Get token price”, “Get NFTs by wallet”, and “Get NFT metadata”.

The simplest way to use these Solana blockchain development services is to copy the appropriate lines of code from the Solana API reference pages. When working with Python, you need to select that programming language. Here’s the “Get native balance by wallet” endpoint reference page:

Solana blockchain app development documentation page from Moralis

Implementing Solana API Endpoints

Return to your “index.py” script to define routes and functions for each endpoint below the “CORS(app)” line. As far as the “get native balance by wallet” endpoint goes, the lines of code from the introduction gets the job done:

@app.post("/getWalletbalance")
def getWalletbalance():
       body = request.json
       params = {
          "address": body["address"],
          "network": body["network"]
          }
       result = sol_api.account.balance(
          api_key= moralis_api_key,
          params = params
       )
       return result

The top line – “@app.post(“/getWalletbalance“) – creates a new route in Python. With “def getWalletbalance():“, you define the function for the endpoint at hand. Using “body = request.json“, you read the JSON data that Moralis fetches and parses for you. Then, you define the endpoint’s parameters (“address” and “network“). Using the “sol_api.account.balance” method with the parameters and your Web3 API key, you store the data under the “result” variable. Finally, you use “return result” to return results. 

Python Snippets of Code for All Solana API Endpoints

When it comes to other Solana API endpoints, the exact same principles are at play. Essentially, you can use the same lines of code as presented above. However, you do need to change the routes, function names, and methods to match the endpoint. To save you some time, you can find the snippets of code for the remaining five Solana API endpoints below:

  • Implementing the “getTokenbalance” endpoint:
@app.post("/getTokenbalance")
def getTokenbalance():
      body = request.json
      params = {
          "address": body["address"],
          "network": body["network"]
          }
      result = sol_api.account.get_spl(
          api_key= moralis_api_key,
          params = params
      )
      return result
  • Implementing the “getNfts” endpoint:  
@app.post("/getNfts")
def getNfts():
      body = request.json
      params = {
          "address": body["address"],
          "network": body["network"]
          }
      result = sol_api.account.get_nfts(
          api_key= moralis_api_key,
          params = params
      )
      return result
  • Implementing the “getPortfolio” endpoint:
@app.post("/getPortfolio")
def getPortfolio():
      body = request.json
      params = {
          "address": body["address"],
          "network": body["network"]
          }
      result = sol_api.account.get_portfolio(
          api_key= moralis_api_key,
          params = params
      )
      return result
  • Implementing the “getNFTMetadata” endpoint:
@app.post("/getNFTMetadata")
def getNFTMetadata(): 
      body = request.json
      params = {
          "address": body["address"],
          "network": body["network"]
          }
      result = sol_api.nft.get_nft_metadata(
          api_key= moralis_api_key,
          params = params
      )
      return result
  • Implementing the “getTokenPrice” endpoint:
@app.post("/getTokenPrice")
def getTokenPrice():
      body = request.json
      params = {
          "address": body["address"],
          "network": body["network"]
          }
      result = sol_api.token.get_token_price(
          api_key= moralis_api_key,
          params = params
      )
      return result

Note: The complete “index.py” script is waiting for you on our GitHub repo page. 

Exploring the Results of Your Solana Blockchain App Development 

Make sure you are inside your “python-backend” folder. Then use the following command to run “index.py”:

python3 index.py

The above command starts your backend on port “9000“. To access the power of your backend, you also need to start your frontend. You can do this with the “Live Server” extension in Visual Studio Code (VSC). Just right-click on “index.html” and select the “Open with Live server” option:

Finally, you get to play around with your frontend dapp and test all Solana API endpoints. The following are our examples for the “Get Native Balance by Wallet” and “Get Token Balance by Wallet” options:

  • The “Get Native Balance by Wallet” demo:
  • The “Get Token Balance by Wallet” demo:

Exploring Solana Blockchain App Development

If you like getting your hands dirty, you enjoyed the above tutorial and most likely created your own instance of our example Solana blockchain app. However, it is important that you understand the theoretical basics behind Solana blockchain app development. So, let’s first answer the “what is Solana?” question.    

Title - Solana Blockchain Development for Apps

What is Solana?

Solana is a non-EVM-compatible programmable blockchain. It is public and open-source. Solana supports smart contract development (on-chain programs), token creation, and all sorts of dapps (decentralized applications). Like all leading programmable chains, Solana utilizes its native coin, “SOL”, to provide network security via Solana’s hybrid DeFi staking consensus. SOL is also used to cover transaction fees on Solana. Like most cryptocurrencies, it can be used to transfer value on the Solana network. 

Raj Gokal and Anatoly Yakovenko launched Solana back in 2017. Both of these devs are still deeply involved with Solana through Solana Labs. The latter is a technology company that builds tools, products, and reference implementations that expand the Solana ecosystem.

Note: To learn more about Solana, use the “what is Solana?” link above. 

Title - Solana blockchain development

What is Solana Blockchain Development?

Solana blockchain development is any sort of dev activity that revolves around the Solana blockchain. At its core, it refers to the creation and continuous improvement (updates implementation) of the Solana blockchain itself. This is what Solana’s core team and community focus on. 

On the other hand, Solana blockchain development can also refer to Solana smart contract building and the creation of dapps that interact with this popular decentralized network. For most developers, this is far more exciting and accessible, especially when they use the right Solana blockchain development services, resources, and tools.

Solana + Rust

Which Programming Language is Used for Solana Development?

Rust is the programming language that Solana supports for writing on-chain programs. So, if you want to code your unique and advanced smart contracts for Solana, you’ll want to be skilled in Rust. As two alternatives to Rust, you may also create Solana on-chain programs with C or C++. However, if you want to create NFTs on Solana, you can do so without any advanced programming skills, thanks to some neat Solana dev tools (more on that below).

When it comes to Solana blockchain app development, you’ve already learned that NodeJS and Python can get the job done. Moreover, Moralis also supports cURL, Go, and PHP. As such, you can use different legacy programming languages to build killer Solana dapps.

Solana blockchain development services title on top of a Solana motherboard

Solana Blockchain Development Services and Resources

The Solana API from Moralis provides the best Solana blockchain development services. They come in the following Web3 data endpoints, which you can use with all leading programming languages/frameworks:

  • Balance API endpoints:
    • Get native balance by wallet
    • Get token balance by wallet
    • Get portfolio by wallet
  • Token API endpoints:
    • Get token price
  • NFT API endpoints:
    • Get NFTs by wallet
    • Get NFT metadata

With the above APIs supporting Solana’s mainnet and devnet, you can fetch NFT metadata, wallet portfolios, token balances, and SPL token prices. As such, you can use the Moralis Solana API in countless ways. For instance, you can build NFT marketplaces, token price feeds, portfolio dapps, and much more. 

Aside from Web3 data, all dapps also need user-friendly Web3 authentication. This is where the Moralis Authentication API enters the scene. The latter supports Ethereum and all leading EVM-compatible chains and Solana. With the “Request challenge” and “Verify challenge” endpoints, you can incorporate seamless Web3 logins for all leading Solana wallets into your dapps. If you are unsure how to answer the “what is a Solana wallet?” question, explore our article on that subject.

When it comes to creating Solana on-chain programs, Solana smart contract examples can save you a lot of time. Also, if you want to create NFTs on this network, Solana NFT mint tools simplify things immensely, such as Metaplex Candy Machine. If you decide to dive deeper into Solana development, you’ll also want to get familiar with Solana Labs’ native programs and Solana Program Library (SPL).   

Nonetheless, a reliable Solana testnet faucet will provide you with testnet SOL to test your dapps and smart contracts on the Solana devnet:

Solana testnet faucet landing page for Solana blockchain app developers

How to Get Started with Solana Blockchain App Development – Summary

We covered quite a distance in today’s article. In the first half of this Solana blockchain app development guide, you had a chance to follow our tutorial and use Python to create an example Solana backend dapp. As for the second part of today’s article, we ensured you know what Solana is and what Solana blockchain development entails. You also learned that Rust is the leading programming language for creating Solana smart contracts. Finally, we covered the best Solana blockchain development services, resources, and tools. As such, you are all set to start BUIDLing killer Solana dapps!

If you have your own ideas and the required skills, use the information obtained herein and join the Web3 revolution. However, if you need more practice or some other ideas, make sure to explore our Solana tutorials, which await you in the Moralis docs, on our blockchain development videos, and our crypto blog. These are also the outlets for exploring Web3 development on other leading blockchains and learning about other practical tools. Great examples are our gwei to ETH ​​calculator, Goerli faucet, Sepolia testnet faucet, and list of Web3 libraries. If you’re not fixated on Solana, you can even use Notify API alternatives to listen to blockchain wallet and smart contract addresses. Or, make sure to check out our guide on how to create your own ERC-20 token. These are just some of the countless options that Moralis provides.

It’s also worth pointing out that blockchain offers many great job opportunities, and becoming blockchain-certified can help you land your dream position in crypto. If that interests you, make sure to consider enrolling in Moralis Academy! We recommend starting with blockchain and Bitcoin fundamentals.  

Moralis Money
Stay ahead of the markets with real-time, on-chain data insights. Inform your trades with true market alpha!
Moralis Money
Related Articles
October 15, 2023

#1 API for Crypto Prices – Fastest Way to Get Prices of Crypto

August 8, 2022

How to Buy NFT Outfits In-Game

January 11, 2023

Solana Wallet Overview – What is a Solana Wallet? 

December 10, 2022

How to Get All Tokens Owned by a Wallet in 5 Steps

December 20, 2022

Ethers.js Dapp Development – How to Use Ethers.js

January 18, 2024

What is an MPC Wallet and a Multisig Wallet? A Full Comparison

December 8, 2023

What are NFT Dapps? In-Depth Guide to Decentralized NFT Apps