This API provides an interface to transfer SPL tokens on the Solana blockchain. It allows users to specify a destination wallet and the amount of tokens to transfer. The API is built using Flask and communicates with the Solana blockchain via the solana-py
and spl-token
libraries.
http://localhost:5000/
Before running the API, ensure the following environment variables are set:
SOLANA_PRIVATE_KEY
: The private key of the sender in JSON format.TOKEN_MINT_ADDRESS
: The address of the SPL token mint.SENDER_TOKEN_ACCOUNT
: The sender's associated token account.DEVNET_URL
: The Solana cluster endpoint (e.g., "https://api.devnet.solana.com").
- Endpoint:
/transfer
- Method:
POST
- Description: Transfers a specified amount of SPL tokens from the sender's account to the recipient's wallet.
destination_wallet
(string, required): The Solana wallet address of the recipient.amount
(float, required): The amount of tokens to transfer. This value should be in the token's base unit (e.g., for a token with 9 decimals,1.23
represents1.23 tokens
).
{
"destination_wallet": "recipient_wallet_address",
"amount": 1.23
}
-
Success (200 OK):
- Returns a JSON object containing the result and transaction ID if the transfer is successful.
{ "result": "Transaction Successful", "transaction_id": "some_txn_id" }
-
Error (500 Internal Server Error):
- Returns a JSON object with an error message if the transfer fails.
{ "error": "Invalid destination wallet" }
-
Successful Transfer:
{ "result": "Transaction Successful", "transaction_id": "3x2zPcsG4r6W1Fuy1tW2QsY4pCm9v1Edgcz6rKz4qv8Z" }
-
Failed Transfer:
{ "error": "Insufficient funds" }
- 400 Bad Request: Missing or invalid parameters (e.g., missing
destination_wallet
oramount
). - 500 Internal Server Error: General errors during the transfer process (e.g., invalid wallet, insufficient funds).
To start the API:
-
Set the required environment variables.
-
Run the Flask app:
python solana-airdrop-api.py
-
The API will be available at
http://localhost:5000
.
flask
: Web framework used to create the API.solana-py
: Python client library for interacting with the Solana blockchain.spl-token
: Python library for working with Solana SPL tokens.
- Automated Token Transfers: Use this API to automate the distribution of tokens to multiple recipients, such as airdrops or reward distributions.
- Payment Processing: Integrate this API into a payment gateway that processes SPL token payments.
- Private Key Management: Ensure the
SOLANA_PRIVATE_KEY
is securely managed and not exposed in the source code or logs. - API Rate Limiting: Consider implementing rate limiting to prevent abuse.