Skip to main content

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 error

Example

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 error

Example

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 error

Example


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 error
string
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 error

Price Fetching Strategy

1

Check Cache

First checks localStorage for recently cached price (shared with PriceTicker component)Valid for: 1 hour
2

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
Complete portfolio data object
string
STX balance (4 decimals)
string
sBTC balance (8 decimals)
array
Recent transaction history (max 10)
number
Current STX price in USD
string
Total portfolio value in USD (2 decimals)

Example

This function parallelizes all API calls using Promise.all for optimal performance.

Network Detection

All functions automatically detect the correct API endpoint:
Address prefix: SP
API base: https://api.hiro.so
Explorer: https://explorer.hiro.so

Error 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.85 fallback
  • Token balances returns empty object {}
  • Errors are logged to console for debugging
No exceptions are thrown - all functions return safe defaults on error.

Performance Optimization

Parallel Requests

getFullPortfolio uses Promise.all to fetch data in parallel:

Price Caching

STX price is cached in localStorage for 1 hour:

Complete Example

API Rate Limits

Hiro API has rate limits:
  • Free tier: 50 requests/minute
  • Authenticated: Higher limits with API key
Implement caching and debouncing for production apps: