DEV Community

Syed Aabis Akhtar
Syed Aabis Akhtar

Posted on

How to Airdrop yourself SOL tokens on Solana devnet using Python

This is going to be a pretty quick tutorial, we are going to see how we can airdrop ourselves some SOL tokens on devnet using Python. So let's get started!

First of all, you would need Python 3.6+ installed, now install Solathon, which is an easy to use and powerful Python SDK for Solana:

pip install solathon
Enter fullscreen mode Exit fullscreen mode

Now, create a main.py file and add the following code:

from solathon import Client

client = Client("https://api.devnet.solana.com")
Enter fullscreen mode Exit fullscreen mode

Here, we are simply initializing a devnet client, now we would need to specify some variables, have a look into the following:

from solathon import PublicKey
from solathon.utils import sol_to_lamport

public_key = PublicKey("your_public_key")
amount = sol_to_lamport(1)
Enter fullscreen mode Exit fullscreen mode

Here we are creating our public key object using Solathon's PublicKey class by passing our wallet's public key as a string; then we are specifying the amount we wish to airdrop ourselves. Note that we are using sol_to_lamport function from Solathon; that is because the standard unit for amounts on the Solana blockchain is lamports. However, since we are more familiar with using SOL token, we can use this function for easy conversions. Another thing to remember is that you can airdrop yourself 2 SOLs at once.

Now comes the last part, we will use the request_airdrop method from Client to get ourselves one SOL:

response = client.request_airdrop(public_key, amount)
print(response)
Enter fullscreen mode Exit fullscreen mode

And that's it! Here is the full code:

from solathon import Client, PublicKey
from solathon.utils import sol_to_lamport

client = Client("https://api.devnet.solana.com")
public_key = PublicKey("your_public_key")
amount = sol_to_lamport(1)

response = client.request_airdrop(public_key, amount)
print(response)
Enter fullscreen mode Exit fullscreen mode

As you can see, it's quite easy to airdrop yourself some SOL tokens using Python and Solathon SDK. Hopefully this tutorial was helpful and you learned something new; feel free to ask questions if any. See you soon next time! Thanks for reading.

Top comments (0)