How to send Solana transactions using Python ✨
Tags: web3, solana, python, tutorial
Published: 2022-02-17 19:30:12

In this tutorial, we are going to be seeing how we can send a transaction in the Solana blockchain using Python!

For this, you do not need to know any complexity or what happens behind the scenes. We will be using [Solathon](https://solathon.vercel.app), which is an easy to use, feature rich SDK for Solana in Python.

Make sure you have Python 3.7 or above installed, now install the package using the following command:
```bash
pip install solathon
```
First of all we would need to create a client, which we will use to interact with the [Solana's JSON RPC](https://docs.solana.com/developing/clients/jsonrpc-api). For testing purposes, we will be using devnet as it doesn't require you to have real SOL balance. The following code creates a client on devnet along with all the imports we will be requiring later on:
```python
from solathon.core.instructions import transfer
from solathon import Client, Transaction, PublicKey, Keypair

client = Client("https://api.devnet.solana.com")
```
Now, we would need to define three things; first of all, the account which is sending the transaction, second is the account which will receive the transaction, and third is the amount of money we want to send in lamports (which is the standard unit for amounts in transactions on Solana blockchain). Have a look at the code below:
```python
sender = Keypair.from_private_key("you_private_key_here")
receiver = PublicKey("receiver_public_key")
amount = 100  
```
Here, the sender needs to be a keypair, which is a combination of public key and private key, you would need to get your private key from your wallet in order to initialize your Keypair object. Then we need the public key of the account whom we wish to send the transaction to, finally we have the amount which we want to transfer in lamports. You can use the [sol_to_lamport](https://solathon.vercel.app/utility/functions#sol_to_lamport) function provided by Solathon for easy conversions.

Now, each transaction requires you to have certain instructions defined which needs to be executed, here we just need one instruction and that is transfer:
```python
instruction = transfer(
        from_public_key=sender.public_key,
        to_public_key=receiver, 
        lamports=100
    )
```
Now, we need to initialize our transaction object with our list of instructions and signers (which will be only the sender in this case):
```python
transaction = Transaction(instructions=[instruction], signers=[sender])
```
Finally we can send this transaction and print the results:
```python
result = client.send_transaction(transaction)
print("Transaction result: ", result)
```
Here is the entire code:
```python
from solathon.core.instructions import transfer
from solathon import Client, Transaction, PublicKey, Keypair

client = Client("https://api.devnet.solana.com")

sender = Keypair.from_private_key("your_private_key")
receiver = PublicKey("receiver_public_key")
amount = 100  

instruction = transfer(
        from_public_key=sender.public_key,
        to_public_key=receiver, 
        lamports=100
    )

transaction = Transaction(instructions=[instruction], signers=[sender])

result = client.send_transaction(transaction)
print("Transaction response: ", result)
```
You can also check out the official documentation for more details: [Solathon docs for sending transaction](https://solathon.vercel.app/clients/client#send_transaction)

How easy it that? Hope so I was able to make things clear and the tutorial was helpful, if there is something which still confuses you or you have any query in general, feel free to ask any questions. See you soon!