Solana.py
🐍 The Solana Python SDK 🐍
Solana.py is the base Python library for interacting with Solana. You can use it to build transactions and interact with the Solana JSON RPC API, much like you would do with solana-web3.js
It also covers the SPL Token Program.
Note: This library uses many core types from the Solders package which used to be provided by solana-py itself. If you are upgrading from an old version and you're looking for something that was deleted, it's probably in solders now.
⚓︎ See also: AnchorPy, a Python client for Anchor-based programs on Solana. ⚓︎
⚡ Quickstart
Installation
-
Install Python bindings for the solana-sdk.
pip install solders -
Install this package to interact with the Solana JSON RPC API.
pip install solana
General Usage
Async API Client
import asyncio
from solana.rpc.async_api import AsyncClient
async def main():
async with AsyncClient("https://api.devnet.solana.com") as client:
res = await client.is_connected()
print(res) # True
# Alternatively, close the client explicitly instead of using a context manager:
client = AsyncClient("https://api.devnet.solana.com")
res = await client.is_connected()
print(res) # True
await client.close()
asyncio.run(main())
Websockets Client
import asyncio
from asyncstdlib import enumerate
from solana.rpc.websocket_api import SolanaWsClient
async def main():
async with SolanaWsClient("wss://api.devnet.solana.com") as websocket:
# Returns once the server has confirmed the subscription.
subscription = await websocket.logs_subscribe()
msg = await websocket.recv()
print(msg)
await websocket.unsubscribe(subscription)
# Alternatively, use the client as an infinite asynchronous iterator:
async with SolanaWsClient("wss://api.devnet.solana.com") as websocket:
subscription = await websocket.logs_subscribe()
async for idx, msg in enumerate(websocket):
if idx == 3:
break
print(msg)
await websocket.unsubscribe(subscription)
asyncio.run(main())
Each *_subscribe() helper awaits the server confirmation and returns a Subscription
handle that carries the server-assigned subscription ID and its kind. Pass that handle to
unsubscribe(); there are no per-method *_unsubscribe() helpers and no raw subscription
IDs in the public API. A handle belongs to the connection that created it, and
recv() yields notifications only — subscription confirmations never appear in the stream.
signature_subscribe() is one-shot: the server cancels it after the notification, so the
client drops its local handle once the SignatureNotification arrives. Calling
unsubscribe() for it afterwards raises ValueError.
🔨 Development
Setup
- Install uv
- Install dev dependencies:
uv sync
- Activate the virtual environment.
.venv\Scripts\activate # Windows
source .venv/bin/activate # Linux/macOS
Lint
make lint
Tests
# All tests
make tests
# Unit tests only
make unit-tests
# Integration tests only
make int-tests
Documentation
To build documentation from source files run:
make build-docs
To serve documentation locally run:
make serve