?

Solana RPC URLs: Guide for Developers and Users + Solana Chain IDs

Kevin Dwyer

Kevin Dwyer

August 30, 2024

3 min read

all-solana-rpc-urls-and-chain-id.jpg

As a developer diving into the Solana ecosystem, understanding Remote Procedure Call (RPC) URLs is crucial. These URLs serve as your gateway to interact with the Solana blockchain, enabling you to fetch data, send transactions, and build decentralized applications (dApps).

This guide will break down Solana RPC URLs, Solana Chain IDs, and provide basic code examples for seamless integration.

Use Ankr’s Solana RPC Now!

List of Solana RPC URLs and Which to Choose

Solana offers several types of RPC URLs, each tailored for different development stages and environments:

  • Solana Mainnet RPC URL:
    • This URL connects you to the live, production Solana blockchain. Use it when your dApp is ready for deployment to the real world.
    • Common Mainnet URLs:
      • https://api.mainnet-beta.solana.com

Solana Labs hosts a rate-limited API node cluster with the endpoint below.

For a scalable and high-performance Solana RPC endpoint, you can use Ankr’s Premium RPC Service.

  • Solana Devnet RPC URL:

    • The devnet is a testing ground for your applications before you go live. It replicates the mainnet but uses "play" tokens (not real SOL) to prevent costly mistakes.
    • Common Devnet URL:
      • https://rpc.ankr.com/solana_devnet
  • Solana Testnet RPC URL:

    • Similar to the devnet, the testnet is another environment for testing your dApps.
    • Common Testnet URL:
      • https://api.testnet.solana.com
  • Custom Solana RPC URLs:

    • For more control and flexibility (e.g., higher throughput), you can set up your own Solana RPC node.

Choosing the Right URL

  • Production (mainnet): Use a reputable public RPC provider or your own node to ensure high reliability and uptime.
  • Development: The devnet or testnet URLs are ideal for testing.

Solana Chain IDs

Each Solana network has a unique chain ID. These IDs are used by developers to ensure they are interacting with the correct blockchain.

|Network | Chain ID | |--|--| | Mainnet Beta | 101 | | Testnet | 102 | | Devnet | 103 |

How to Use Solana RPC URLs: Code Examples

Here's how you can interact with Solana using RPC URLs in popular programming languages:

Learn more in the Solana Docs

JavaScript (Solana-Web3.js):

JavaScript


async function getAccountBalance(publicKeyString) {
  const connection = new Connection('https://api.devnet.solana.com'); // Replace with your desired cluster's RPC endpoint

  try {
    const publicKey = new PublicKey(publicKeyString);
    const accountInfo = await connection.getAccountInfo(publicKey);

    if (accountInfo) {
      const balanceInLamports = accountInfo.lamports;
      const balanceInSOL = balanceInLamports / 1000000000;
      console.log(`Balance: ${balanceInSOL} SOL`);
    } else {
      co
nsole.error('Account not found.');
    }
  } catch (error) {
    console.error('Error fetching account balance:', error);
  }
}

// Replace 'your_public_key' with the actual public key of the account you want to check
getAccountBalance('your_public_key');

Python (Solana Py):

Python


async def get_block_height():
    client = Client("https://api.devnet.solana.com")  # Replace with your desired cluster's RPC endpoint

    try:
        block_height = await client.get_block_height()
        print("Block Height:", block_height)
    except Exception as e:
        print("Error fetching block height:", e)

# Example usage:
get_block_height()

Rust (Solana Rust):

Rust


async fn get_fee_rate() -> Result<(), Box<dyn std::error::Error>> {
    let client = RpcClient::new("https://api.devnet.solana.com"); // Replace with your desired cluster's RPC endpoint

    let fee_rate = client.get_fee_rate().await?;
    println!("Fee Rate: {}", fee_rate);

    Ok(())
}

// Example usage:
get_fee_rate().await?;

Important Considerations Solana RPC URLs:

  • Rate Limits: Be mindful of rate limits imposed by public RPC providers.
  • Security: Always double-check the URLs you use to prevent phishing attacks.

Join the Conversation on Our Channels!

Twitter | Telegram | Discord | YouTube | LinkedIn | Instagram | Reddit | All Links

Similar articles.

What Are RPC Nodes and Endpoints? The Complete Guide [2024]

What Are RPC Nodes and Endpoints? The Complete Guide [2024]

Kevin Dwyer

Kevin Dwyer

December 6, 2023

rpc node.jpg

When diving into the world of blockchain development, you’ll soon come across a variety of terms and acronyms that can get confusing! Let’s tackle...

gRPC vs RPC in Blockchain Infrastructure

gRPC vs RPC in Blockchain Infrastructure

Kevin Dwyer

Kevin Dwyer

April 29, 2024

grpc-vs-rpc.jpg

As blockchain technology continues to evolve, the choice of communication protocols plays a crucial role in determining the efficiency, security, and scalability of blockchain infrastructure....

What is gRPC: A Comprehensive Guide [2024]

What is gRPC: A Comprehensive Guide [2024]

Kevin Dwyer

Kevin Dwyer

April 30, 2024

gRPC, or Google Remote Procedure Call, is an open-source RPC framework initially developed by Google. It allows efficient communication between distributed systems using a variety...