Fetching the balance API docs
Once connected, the balance can be retrieved at any time.
Rust
let info = sdk.get_info(GetInfoRequest {
// ensure_synced: true will ensure the SDK is synced with the Spark network
// before returning the balance
ensure_synced: Some(false),
}).await?;
let balance_sats = info.balance_sats;
Swift
// ensureSynced: true will ensure the SDK is synced with the Spark network
// before returning the balance
let info = try await sdk.getInfo(request: GetInfoRequest(
ensureSynced: false
))
let balanceSats = info.balanceSats
Kotlin
try {
// ensureSynced: true will ensure the SDK is synced with the Spark network
// before returning the balance
val info = sdk.getInfo(GetInfoRequest(false))
val balanceSats = info.balanceSats
} catch (e: Exception) {
// handle error
}
Javascript
const info = await sdk.getInfo({
// ensureSynced: true will ensure the SDK is synced with the Spark network
// before returning the balance
ensureSynced: false
})
const balanceSats = info.balanceSats
React Native
// ensureSynced: true will ensure the SDK is synced with the Spark network
// before returning the balance
const info = await sdk.getInfo({
ensureSynced: false
})
const balanceSats = info.balanceSats
Flutter
// ensureSynced: true will ensure the SDK is synced with the Spark network
// before returning the balance
final info = await sdk.getInfo(request: GetInfoRequest(ensureSynced: false));
final balanceSats = info.balanceSats;
Python
try:
# ensure_synced: True will ensure the SDK is synced with the Spark network
# before returning the balance
info = await sdk.get_info(request=GetInfoRequest(ensure_synced=False))
balance_sats = info.balance_sats
except Exception as error:
logging.error(error)
raise
Go
ensureSynced := false
info, err := sdk.GetInfo(breez_sdk_spark.GetInfoRequest{
// EnsureSynced: true will ensure the SDK is synced with the Spark network
// before returning the balance
EnsureSynced: &ensureSynced,
})
if sdkErr := err.(*breez_sdk_spark.SdkError); sdkErr != nil {
return err
}
balanceSats := info.BalanceSats
log.Printf("Balance: %v sats", balanceSats)
Developer note
The SDK maintains a cached balance for fast responses and updates it on every change. The `get_info` call returns the value from this cache to provide a low-latency user experience.Right after startup, the cache may not yet reflect the latest state from the network. Depends on your use case you can use one of the following options to get the fully up to date balance:
- If your application runs continuously in the background, call
get_infoafter eachSdkEvent::Syncedevent. - If you're only briefly using the SDK to fetch the balance, call
get_infowithensure_synced = truebefore disconnecting.