Connecting to Solana
This example shows how to connect to a Solana environment and check the connection status.
Code
#!/usr/bin/env python3
"""
Solana Cookbook - Connecting to a Solana Environment
"""
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
asyncio.run(main())
Explanation
- Import required modules: We import
asyncio
for async operations andAsyncClient
from solana.rpc.async_api - Create async client: Initialize an async client with the Solana devnet RPC endpoint
- Check connection: Use
is_connected()
to verify the connection status - Print result: The result should be
True
if connected successfully
Usage
Run this script to test your connection to the Solana devnet:
python connecting_to_solana.py
Network Endpoints
- Devnet: https://api.devnet.solana.com
- Testnet: https://api.testnet.solana.com
- Mainnet: https://api.mainnet-beta.solana.com