{"id":5490,"library":"solana","title":"Solana Python API","description":"Solana.py is the official Python SDK for interacting with the Solana blockchain. It provides a robust API for building transactions, interacting with the Solana JSON RPC API, and working with the SPL Token Program. The library is actively maintained with frequent releases, often incorporating core types and high-performance components from the `solders` library.","status":"active","version":"0.36.11","language":"en","source_language":"en","source_url":"https://github.com/michaelhly/solanapy","tags":["blockchain","solana","web3","cryptocurrency","rpc"],"install":[{"cmd":"pip install solana solders","lang":"bash","label":"Install solana-py and solders"}],"dependencies":[{"reason":"Provides core Solana data structures (Pubkey, Keypair, Message, Transaction) and high-performance Rust bindings. Required for modern `solana-py` usage.","package":"solders","optional":false},{"reason":"Used for synchronous and asynchronous HTTP requests to the Solana RPC endpoint.","package":"httpx","optional":false},{"reason":"Used for WebSocket connections to the Solana RPC endpoint for subscriptions.","package":"websockets","optional":false},{"reason":"Required for cryptographic operations like message signing and verification.","package":"pynacl","optional":false}],"imports":[{"note":"AsyncClient is the recommended client for most modern applications due to Solana's high-throughput nature.","wrong":"from solana.rpc.api import Client","symbol":"AsyncClient","correct":"from solana.rpc.async_api import AsyncClient"},{"note":"Core types like Keypair have been moved from `solana.keypair` to `solders.keypair` for improved performance and consistency across Solana Python SDKs.","wrong":"from solana.keypair import Keypair","symbol":"Keypair","correct":"from solders.keypair import Keypair"},{"note":"Core types like PublicKey have been moved from `solana.publickey` to `solders.pubkey`.","wrong":"from solana.publickey import PublicKey","symbol":"Pubkey","correct":"from solders.pubkey import Pubkey"},{"note":"The `solana.transaction` module is deprecated. Use `solders.transaction` and `VersionedTransaction` for modern transaction formats.","wrong":"from solana.transaction import Transaction","symbol":"VersionedTransaction","correct":"from solders.transaction import VersionedTransaction"},{"note":"The `solana.transaction.Message` class is deprecated. `solders.message.MessageV0` (or `Message`) is the current standard.","wrong":"from solana.transaction import Message","symbol":"MessageV0","correct":"from solders.message import MessageV0"}],"quickstart":{"code":"import asyncio\nimport os\n\nfrom solders.pubkey import Pubkey\nfrom solana.rpc.async_api import AsyncClient\n\nasync def get_sol_balance(public_key_str: str):\n    \"\"\"Fetches the SOL balance of a given public key.\"\"\"\n    # Use a devnet RPC endpoint. For production, consider using a dedicated provider.\n    rpc_url = os.environ.get(\"SOLANA_RPC_URL\", \"https://api.devnet.solana.com\")\n    async with AsyncClient(rpc_url) as client:\n        pubkey = Pubkey.from_string(public_key_str)\n        try:\n            response = await client.get_balance(pubkey)\n            balance_lamports = response.value\n            balance_sol = balance_lamports / 1_000_000_000 # 1 SOL = 1,000,000,000 lamports\n            print(f\"Account: {public_key_str}\")\n            print(f\"Balance: {balance_sol} SOL ({balance_lamports} lamports)\")\n        except Exception as e:\n            print(f\"Error fetching balance: {e}\")\n\nif __name__ == \"__main__\":\n    # Replace with a real Solana public key (e.g., from a test wallet)\n    example_public_key = \"5Q544fFztbO2Drj9yQ9C7M2f8L2rW3e3H5D2n6H6f8f8\" # Example devnet address\n    asyncio.run(get_sol_balance(example_public_key))\n","lang":"python","description":"This quickstart demonstrates how to connect to the Solana Devnet using `AsyncClient` and fetch the SOL balance of a specified public key. It highlights the use of `solders.pubkey.Pubkey` and the asynchronous nature of `solana-py` interactions. Remember to replace the example public key with a valid one and set `SOLANA_RPC_URL` if not using the default devnet endpoint."},"warnings":[{"fix":"Rewrite transaction creation and signing logic to use `solders.transaction.VersionedTransaction` and `solders.message.MessageV0`. Refer to the latest Solana.py documentation or Cookbook for updated examples.","message":"Starting with v0.36.0, support for legacy transaction formats has been removed. Users should migrate to `VersionedTransaction` and `MessageV0` from `solders.transaction` and `solders.message` respectively.","severity":"breaking","affected_versions":">=0.36.0"},{"fix":"Update imports to use `solders` for core types (e.g., `from solders.keypair import Keypair`, `from solders.pubkey import Pubkey`, `from solders.message import MessageV0`). Ensure `solders` is installed alongside `solana`.","message":"Many core data types (e.g., `Keypair`, `Pubkey`, `Message`, `Transaction`) were migrated from `solana-py`'s internal modules to the `solders` library. Direct imports from `solana.keypair`, `solana.publickey`, or `solana.transaction` for these types are deprecated or will result in errors.","severity":"breaking","affected_versions":">=0.33.0"},{"fix":"Migrate to `solders.transaction.VersionedTransaction` and `solders.message.MessageV0` for transaction construction. Replace deprecated RPC methods with their modern equivalents or alternative approaches.","message":"The entire `solana.transaction` module and its classes (e.g., `Transaction`, `Message`) are deprecated. Additionally, methods like `get_stake_activation` have been deprecated.","severity":"deprecated","affected_versions":">=0.35.1"},{"fix":"Remove any reliance on `BlockhashCache`. Always fetch the latest blockhash directly using `client.get_latest_blockhash()` before constructing transactions.","message":"The `BlockhashCache`, an experimental feature, was removed in v0.33.0 due to it being 'experimental and flawed' and a 'footgun because it broke a lot'.","severity":"gotcha","affected_versions":">=0.33.0"},{"fix":"Carefully review the `solders` and `solana-py` documentation for the exact constructor signatures for `Transaction` and `VersionedTransaction`. Ensure all required arguments (`from_keypairs`, `message`, `recent_blockhash`) are provided with the correct `solders` types (e.g., `Pubkey` for payer, `MessageV0` for message, `Hash` for blockhash).","message":"When constructing transactions using `solders.transaction.Transaction` (especially older patterns) or `VersionedTransaction`, incorrect argument types or missing required arguments for the constructor are common, leading to `TypeError` or runtime errors like `argument 'payer': 'Hash' object cannot be converted to 'Pubkey'`.","severity":"gotcha","affected_versions":"All versions using `solders.transaction`"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}