To use the Stellar API, you need an Alchemy account. Create a free account to get started.
Stellar is an open-source, decentralized blockchain network designed for fast, low-cost financial transactions and asset tokenization. It features Soroban, a smart contract platform built on Rust and WebAssembly (WASM), offering 5-second finality and a developer-friendly environment for building decentralized applications.
The Stellar API gives you access to Stellar RPC over JSON-RPC 2.0 through Alchemy. The transport is familiar if you have used JSON-RPC before, but the methods and response shapes are Stellar-specific rather than Ethereum-specific.
Create a new directory for your example script:
mkdir stellar-api-quickstart
cd stellar-api-quickstartCreate an index.js file in your project directory and paste the following code. This example uses Node.js's built-in fetch API.
const apiKey = "your-api-key";
const url = `https://stellar-mainnet.g.alchemy.com/v2/${apiKey}`;
const payload = {
jsonrpc: "2.0",
id: 1,
method: "getNetwork",
};
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
})
.then((response) => response.json())
.then((data) => {
console.log(JSON.stringify(data, null, 2));
})
.catch((error) => {
console.error(error);
});Replace your-api-key with your actual Alchemy API key from the Alchemy Dashboard.
This request calls getNetwork, a Stellar RPC method that returns information such as the network passphrase and protocol version.
Run your script to make a request to the Stellar network:
node index.jsYou should see a JSON-RPC response with a result object containing network details such as passphrase and protocolVersion.
You've made your first request to the Stellar network. Next, you can:
- Call
getLatestLedgerto inspect the latest ledger metadata. - Call
getLedgerEntriesto read live ledger state such as contract data and other ledger entries. - Use the official
stellar-sdkpackage when you're ready to build a larger JavaScript application against Stellar RPC and Horizon.
When you move beyond zero-parameter methods like getNetwork, keep in mind that Stellar RPC methods use named parameters for requests instead of Ethereum-style positional arrays.