Overview
The Stacks API service provides functions to interact with the Hiro Stacks API for fetching real-time blockchain data. All functions automatically detect network (testnet/mainnet) based on address prefix. Source:src/services/stacksApi.js
Functions
getSTXBalance
Fetch a user’s STX token balance.string
required
Stacks wallet address (mainnet starts with
SP, testnet starts with ST)string
STX balance formatted to 4 decimal places (e.g., “100.0000”)
Returns
"0.0000" on errorExample
Balance is returned in STX units (converted from microstacks). 1 STX = 1,000,000 microstacks.
getSBTCBalance
Fetch a user’s sBTC (synthetic Bitcoin) balance.string
required
Stacks wallet address
string
sBTC balance formatted to 8 decimal places (e.g., “0.00500000”)
Returns
"0.00000000" on errorExample
Supported Contracts
The function checks multiple sBTC contract addresses:getTokenBalances
Fetch all fungible token balances for an address.string
required
Stacks wallet address
object
Object mapping token contract IDs to balance info
Returns
{} (empty object) on errorExample
getTransactionHistory
Fetch recent transaction history for an address.string
required
Stacks wallet address
array
Array of transaction objects (max 10, most recent first)
Returns
[] on errorstring
Full transaction ID
string
Shortened ID for display (e.g., “0x1234ab…89ef”)
string
Direct link to Hiro Explorer for this transaction
string
Transaction type (e.g., “token_transfer”, “contract_call”)
string
Transaction status (“success”, “pending”, “failed”)
string
Amount transferred (e.g., “10.0000 STX”) or ”—” if not applicable
string
Formatted date string (e.g., “1/15/2024”)
Example
getSTXPrice
Fetch current STX price in USD with multiple fallback sources.number
Current STX price in USD (e.g.,
2.85)
Returns cached value or fallback (2.85) on errorPrice Fetching Strategy
1
Check Cache
First checks
localStorage for recently cached price (shared with PriceTicker component)Valid for: 1 hour2
CryptoCompare API
If cache miss, tries CryptoCompare API (better CORS support)
3
CoinGecko API
Falls back to CoinGecko if CryptoCompare fails
4
Hard Default
Returns
2.85 if all APIs fail (reasonable fallback)Example
getFullPortfolio
Fetch complete portfolio data in a single call (parallelized).string
required
Stacks wallet address
object
Example
This function parallelizes all API calls using
Promise.all for optimal performance.Network Detection
All functions automatically detect the correct API endpoint:- Mainnet
- Testnet
Address prefix:
API base:
Explorer:
SPAPI base:
https://api.hiro.soExplorer:
https://explorer.hiro.soError Handling
All functions gracefully handle errors:- Balance functions return
"0"with appropriate decimal places - Transaction history returns empty array
[] - Price function returns cached value or
2.85fallback - Token balances returns empty object
{} - Errors are logged to console for debugging
Performance Optimization
Parallel Requests
getFullPortfolio uses Promise.all to fetch data in parallel:
Price Caching
STX price is cached inlocalStorage for 1 hour:
Complete Example
API Rate Limits
Hiro API has rate limits:- Free tier: 50 requests/minute
- Authenticated: Higher limits with API key
Related Resources
- usePortfolio Hook - React hook wrapper with auto-refresh
- Contract Service - Write data to blockchain
- Hiro API Docs