Stellar API Tutorial: Step-by-Step Guide for Developers
Build your first Stellar application from scratch with this hands-on tutorial.
This Stellar API for developers tutorial walks you through building a complete blockchain application using LumenQuery's infrastructure. You'll learn to query accounts, fetch transactions, read smart contract data, and build a real-time dashboard—all using the Stellar blockchain data API. By the end, you'll have a working application that demonstrates the power of the Stellar transaction query API and the broader Stellar Web3 developer platform ecosystem.
Prerequisites
- - Node.js 18+ installed
- - Basic JavaScript/TypeScript knowledge
- - A free LumenQuery account
Step 1: Set Up Your Stellar API Environment
First, sign up for a free LumenQuery account and get your API key from the dashboard. Then set up your project:
mkdir stellar-app && cd stellar-app
npm init -y
npm install @stellar/stellar-sdk node-fetch
# Set your API key
export LUMENQUERY_API_KEY="lq_your_key_here"Step 2: Query Account Balances with the Stellar API
The most common operation is querying account balances. The Stellar API for developers makes this straightforward through the Horizon REST API:
// query-account.js
const API_BASE = 'https://api.lumenquery.io';
async function getAccountBalances(accountId) {
const response = await fetch(
`${API_BASE}/accounts/${accountId}`,
{ headers: { 'X-API-Key': process.env.LUMENQUERY_API_KEY } }
);
const account = await response.json();
return account.balances.map(b => ({
asset: b.asset_type === 'native' ? 'XLM' : b.asset_code,
balance: parseFloat(b.balance).toLocaleString(),
type: b.asset_type,
}));
}
// Try it
getAccountBalances('GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN')
.then(b => console.table(b));You can also query accounts interactively using our Natural Language Query Interface—just type "account info for GXXX..." and get instant results.
Step 3: Fetch Transaction History Using the Stellar Transaction Query API
The Stellar transaction query API via Horizon lets you fetch recent transactions, filter by account, and stream updates in real time:
// fetch-transactions.js
async function getRecentTransactions(limit = 10) {
const response = await fetch(
`${API_BASE}/transactions?order=desc&limit=${limit}`,
{ headers: { 'X-API-Key': process.env.LUMENQUERY_API_KEY } }
);
const data = await response.json();
return data._embedded.records.map(tx => ({
hash: tx.hash.substring(0, 12) + '...',
source: tx.source_account.substring(0, 8) + '...',
operations: tx.operation_count,
fee: (parseInt(tx.fee_charged) / 10_000_000) + ' XLM',
status: tx.successful ? 'Success' : 'Failed',
time: new Date(tx.created_at).toLocaleString(),
}));
}
getRecentTransactions(5).then(txs => console.table(txs));See this data visualized on our Live Transaction Viewer, which decodes operations into human-readable descriptions.
Step 4: Read Smart Contract State with the Stellar Smart Contract API
The Stellar smart contract API (via Stellar RPC) lets you read Soroban contract storage. For a deeper dive into RPC methods, see our complete Stellar RPC guide.
// read-contract.js
async function getLatestLedger() {
const response = await fetch('https://rpc.lumenquery.io', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': process.env.LUMENQUERY_API_KEY,
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'getLatestLedger',
}),
});
const data = await response.json();
return data.result;
}
getLatestLedger().then(l =>
console.log('Ledger:', l.sequence, 'Protocol:', l.protocolVersion)
);Browse decoded contract storage visually using our Smart Contract Explorer. Read the Soroban contracts documentation for the full API reference.
Step 5: Build a Real-Time Dashboard
Now combine both APIs to build a dashboard that shows live network metrics. This is the same pattern used in our Stellar Network Analytics Dashboard:
// dashboard.js - Combine Horizon + RPC
async function getDashboardData() {
// Parallel API calls for speed
const [ledger, feeStats, txs] = await Promise.all([
getLatestLedger(), // Stellar RPC
fetch(`${API_BASE}/fee_stats`) // Horizon
.then(r => r.json()),
getRecentTransactions(5), // Horizon
]);
return {
network: {
ledger: ledger.sequence,
protocol: ledger.protocolVersion,
},
fees: {
min: feeStats.last_ledger_base_fee,
median: feeStats.fee_charged?.p50,
},
recentTransactions: txs,
};
}
// Refresh every 5 seconds
setInterval(async () => {
const data = await getDashboardData();
console.clear();
console.log('Ledger:', data.network.ledger);
console.table(data.recentTransactions);
}, 5000);Step 6: Explore Data with Natural Language
LumenQuery also provides a Natural Language Query Interface where you can explore data without writing code. Try queries like:
- "Show the top 10 XLM holders"
- "Recent payments larger than 100,000 XLM"
- "What assets are on Stellar?"
- "Latest ledger status"
Next Steps for Stellar API Developers
You now have a working Stellar API for developers foundation. Here's where to go next:
Continue Learning
- What is Stellar RPC? (Complete Guide) — Deep dive into all RPC methods
- Horizon vs RPC vs Indexers — Choose the right data access pattern
- LumenQuery Tutorial — Explore all platform features
- Build a Stellar Blockchain Explorer — Full Next.js project tutorial
- How Stellar Is Expanding the Developer Ecosystem
- API Documentation — Full endpoint reference
- Portfolio Intelligence Docs — Build portfolio tracking apps