Stellar is a powerful, open-source blockchain network designed to enable fast, low-cost cross-asset transfers of value. Whether you're building financial applications, remittance platforms, or decentralized services, Stellar offers a robust infrastructure backed by a scalable consensus protocol and minimal transaction fees. This comprehensive guide walks you through the core aspects of Stellar blockchain development using JavaScript — from creating accounts and funding them on the testnet to executing payments and retrieving transaction history.
Understanding the Stellar Ecosystem
Before diving into code, it’s essential to understand the foundational components of the Stellar network that developers interact with.
Horizon API
Horizon is the primary interface for applications to communicate with the Stellar network. It's a RESTful HTTP API server that allows developers to submit transactions, query account balances, stream events in real time, and retrieve ledger data. While you can interact with Horizon using raw HTTP requests or tools like cURL
, the most efficient way is via the official Stellar SDKs.
The Stellar Development Foundation officially supports SDKs in JavaScript, Java, and Go, while community-driven libraries are available for Python, C#, and Ruby.
Stellar Core
At the heart of the network lies Stellar Core, the node software responsible for validating transactions and maintaining consensus across the decentralized network. Every Horizon instance connects to a Stellar Core node to ensure data consistency and network integrity.
Stellar Consensus Protocol (SCP)
Unlike proof-of-work or proof-of-stake models, Stellar uses its unique Stellar Consensus Protocol (SCP) — a federated Byzantine agreement system that prioritizes safety over liveness. In the event of network partitions or malicious behavior, SCP halts progress until full agreement is restored, ensuring no forks or double-spends occur.
Testnet for Developers
Stellar provides a fully functional test network (testnet) where developers can experiment without risking real funds. The native asset on the testnet is test Lumens (XLM), which can be obtained freely via Friendbot, a faucet service that funds developer accounts.
👉 Get started with blockchain development tools and resources today.
Setting Up Your Development Environment
To follow along, you should have basic knowledge of JavaScript, Node.js, and asynchronous programming using async/await
. We’ll use the official Stellar JavaScript SDK to interact with the testnet.
Install the Stellar SDK
First, initialize your project and install the required package:
npm init -y
npm install stellar-sdk request-promise
Now set up a connection to the Stellar testnet:
const Stellar = require('stellar-sdk');
const rp = require('request-promise');
// Connect to testnet
const server = new Stellar.Server('https://horizon-testnet.stellar.org');
Stellar.Network.useTestNetwork();
This configuration enables all subsequent operations to run on the sandbox environment.
Creating and Funding Stellar Accounts
Every Stellar account requires a public-private key pair. The SDK makes it easy to generate these securely.
Generate Key Pairs
let pairA = Stellar.Keypair.random();
let pairB = Stellar.Keypair.random();
console.log('Account A Public Key:', pairA.publicKey());
console.log('Account A Secret Key:', pairA.secret());
console.log('Account B Public Key:', pairB.publicKey());
🔐 Never expose secret keys in production or public repositories.
Fund Accounts Using Friendbot
On the testnet, use Friendbot to receive 10,000 test XLM per account:
await rp.get({
uri: 'https://horizon-testnet.stellar.org/friendbot',
qs: { addr: pairA.publicKey() },
json: true
});
await rp.get({
uri: 'https://horizon-testnet.stellar.org/friendbot',
qs: { addr: pairB.publicKey() },
json: true
});
Once funded, load the account object to access balance and sequence number:
let accountA = await server.loadAccount(pairA.publicKey());
let accountB = await server.loadAccount(pairB.publicKey());
Check Account Balances
accountA.balances.forEach(balance => {
console.log(`Asset: ${balance.asset_type}, Balance: ${balance.balance}`);
});
You should see a balance of approximately 10,000 XLM minus a small fee for the initial creation.
Executing a Payment Transaction
Transactions in Stellar consist of one or more operations. The most common is payment
.
Build and Sign the Transaction
const transaction = new Stellar.TransactionBuilder(accountA)
.addOperation(Stellar.Operation.payment({
destination: pairB.publicKey(),
asset: Stellar.Asset.native(), // Native XLM
amount: '30.0000001' // Must include 7 decimal places
}))
.build();
// Sign with sender's private key
transaction.sign(pairA);
Note: Amounts must be passed as strings with up to seven decimal places due to Stellar’s internal precision model (a stroop = 0.0000001 XLM).
Submit the Transaction
try {
const result = await server.submitTransaction(transaction);
console.log('Transaction successful:', result.hash);
} catch (error) {
console.error('Transaction failed:', error.response.data.extras.result_codes);
}
If successful, Account B will receive ~30 XLM (minus 100 stroops network fee).
👉 Explore advanced transaction features and smart contract patterns on modern blockchains.
Querying Transaction History
You can retrieve past transactions for any account using Horizon’s query methods.
Fetch Recent Transactions
let historyPage = await server.transactions()
.forAccount(accountA.accountId())
.limit(1)
.call();
console.log('Latest transaction ID:', historyPage.records[0].id);
Use .next()
to paginate forward:
historyPage = await historyPage.next();
Decode Transaction Details from XDR
All transactions are stored in XDR (External Data Representation) format. You can decode them to inspect operations:
const xdr = historyPage.records[0].envelope_xdr;
const txEnvelope = Stellar.xdr.TransactionEnvelope.fromXDR(xdr, 'base64');
const operations = txEnvelope.tx().operations();
operations.forEach(op => {
if (op.body().switch().name === 'payment') {
const amount = op.body().paymentOp().amount().toString();
console.log(`Transferred ${amount / 1e7} XLM`);
}
});
This gives full visibility into historical actions — ideal for audit trails or blockchain explorers.
Core Keywords for SEO Optimization
- Stellar blockchain
- XLM transaction
- Stellar JavaScript SDK
- Horizon API
- Stellar testnet
- Create Stellar account
- Blockchain development
- Send Lumens
These terms naturally appear throughout this guide, enhancing search visibility without keyword stuffing.
Frequently Asked Questions (FAQ)
Q: What is the difference between Stellar Mainnet and Testnet?
A: The mainnet handles real-value transactions using actual XLM. The testnet uses free test Lumens for development and testing purposes only.
Q: How much does a Stellar transaction cost?
A: Each operation costs 100 stroops (0.00001 XLM). A simple payment transaction costs one operation, totaling 0.00001 XLM.
Q: Can I build smart contracts on Stellar?
A: While not Turing-complete like Ethereum, Stellar supports programmable logic via multi-signature policies, timebounds, and automated payment paths using path payments.
Q: Is the Stellar SDK safe to use in browsers?
A: Yes, but never expose secret keys client-side. Use backend services to sign transactions securely.
Q: How do I recover a lost Stellar account?
A: Recovery depends entirely on your backup of the secret key. If lost, access cannot be restored — there is no central authority.
Q: Where can I view my transactions live?
A: Use the Stellar Laboratory or public explorers like Horizon Testnet Explorer to monitor activity.
Final Thoughts
This tutorial has equipped you with the foundational skills to begin building on the Stellar blockchain. From setting up your environment and creating accounts to sending payments and analyzing transaction data, you now have a working understanding of how applications interact with the network.
Whether you're building remittance apps, tokenized asset platforms, or cross-border payment gateways, Stellar’s efficiency and low cost make it an excellent choice for real-world financial solutions.
👉 Continue learning about blockchain innovation and digital assets with cutting-edge resources.