Initializing the SDK API docs
Basic Initialization
The easiest way to initialize the SDK is with the connect method. This method requires:
- The network, mnemonic, and Breez API key you intend to use
- A storage directory path where the SDK can manage its data
Developer note
For WASM Web, SDK storage is managed using IndexedDB.The storage is used to persist the SDK’s state. If you run multiple SDK instances, each must have its own unique storage directory.
Once connected, you’re ready to start interacting with the SDK.
// Construct the seed using mnemonic words or entropy bytes
let mnemonic = "<mnemonic words>".to_string();
let seed = Seed::Mnemonic {
mnemonic,
passphrase: None,
};
// Create the default config
let mut config = default_config(Network::Mainnet);
config.api_key = Some("<breez api key>".to_string());
// Connect to the SDK using the simplified connect method
let sdk = connect(ConnectRequest {
config,
seed,
storage_dir: "./.data".to_string(),
})
.await?;
// Construct the seed using mnemonic words or entropy bytes
let mnemonic = "<mnemonic words>"
let seed = Seed.mnemonic(mnemonic: mnemonic, passphrase: nil)
// Create the default config
var config = defaultConfig(network: Network.mainnet)
config.apiKey = "<breez api key>"
// Connect to the SDK using the simplified connect method
let sdk = try await connect(request: ConnectRequest(
config: config,
seed: seed,
storageDir: "./.data"
))
// Construct the seed using mnemonic words or entropy bytes
val mnemonic = "<mnemonic words>"
val seed = Seed.Mnemonic(mnemonic, null)
// Create the default config
val config = defaultConfig(Network.MAINNET)
config.apiKey = "<breez api key>"
try {
// Connect to the SDK using the simplified connect method
val sdk = connect(ConnectRequest(
config = config,
seed = seed,
storageDir = "./.data"
))
} catch (e: Exception) {
// handle error
}
// Construct the seed using mnemonic words or entropy bytes
var mnemonic = "<mnemonic words>";
var seed = new Seed.Mnemonic(mnemonic: mnemonic, passphrase: null);
// Create the default config
var config = BreezSdkSparkMethods.DefaultConfig(Network.Mainnet) with
{
apiKey = "<breez api key>"
};
// Connect to the SDK using the simplified connect method
var sdk = await BreezSdkSparkMethods.Connect(
request: new ConnectRequest(
config: config,
seed: seed,
storageDir: "./.data"
)
);
// Call init when using the SDK in a web environment before calling any other SDK
// methods. This is not needed when using the SDK in a Node.js/Deno environment.
//
// import init, { BreezSdk, defaultConfig } from '@breeztech/breez-sdk-spark'
await init()
// Construct the seed using mnemonic words or entropy bytes
const mnemonic = '<mnemonic words>'
const seed: Seed = { type: 'mnemonic', mnemonic, passphrase: undefined }
// Create the default config
const config = defaultConfig('mainnet')
config.apiKey = '<breez api key>'
// Connect to the SDK using the simplified connect method
const sdk = await connect({
config,
seed,
storageDir: './.data'
})
// Construct the seed using mnemonic words or entropy bytes
const mnemonic = '<mnemonics words>'
const seed = new Seed.Mnemonic({ mnemonic, passphrase: undefined })
// Create the default config
const config = defaultConfig(Network.Mainnet)
config.apiKey = '<breez api key>'
const sdk = await connect({
config,
seed,
storageDir: `${RNFS.DocumentDirectoryPath}/data`
})
// Call once on your Dart entrypoint file, e.g.; `lib/main.dart`
// or singleton SDK service. It is recommended to use a single instance
// of the SDK across your Flutter app.
await BreezSdkSparkLib.init();
// Construct the seed using mnemonic words or entropy bytes
String mnemonic = "<mnemonic words>";
final seed = Seed.mnemonic(mnemonic: mnemonic, passphrase: null);
// Create the default config
final config = defaultConfig(network: Network.mainnet)
.copyWith(apiKey: "<breez api key>");
final connectRequest =
ConnectRequest(config: config, seed: seed, storageDir: "./.data");
final sdk = await connect(request: connectRequest);
# Construct the seed using mnemonic words or entropy bytes
mnemonic = "<mnemonic words>"
seed = Seed.MNEMONIC(mnemonic=mnemonic, passphrase=None)
# Create the default config
config = default_config(network=Network.MAINNET)
config.api_key = "<breez api key>"
try:
# Connect to the SDK using the simplified connect method
sdk = await connect(
request=ConnectRequest(config=config, seed=seed, storage_dir="./.data")
)
return sdk
except Exception as error:
logging.error(error)
raise
// Construct the seed using mnemonic words or entropy bytes
mnemonic := "<mnemonic words>"
var seed breez_sdk_spark.Seed = breez_sdk_spark.SeedMnemonic{
Mnemonic: mnemonic,
Passphrase: nil,
}
// Create the default config
apiKey := "<breez api key>"
config := breez_sdk_spark.DefaultConfig(breez_sdk_spark.NetworkMainnet)
config.ApiKey = &apiKey
connectRequest := breez_sdk_spark.ConnectRequest{
Config: config,
Seed: seed,
StorageDir: "./.data",
}
// Connect to the SDK using the simplified connect method
sdk, err := breez_sdk_spark.Connect(connectRequest)
return sdk, err
Developer note
On some platforms (e.g., Android, iOS), you must use an application-specific writable directory within the app’s sandbox for SDK storage.
Advanced Initialization
For advanced use cases where you need more control, you can configure the SDK using the Builder pattern. With the SDK Builder you can define:
- Custom storage management (bring your own implementation)
- Which chain service to use (custom or the SDK’s default)
- Which REST client to use for LNURL requests (custom or the SDK’s default)
- Which keyset to use for the signer (custom or the SDK’s default)
// Construct the seed using mnemonic words or entropy bytes
let mnemonic = "<mnemonic words>".to_string();
let seed = Seed::Mnemonic {
mnemonic,
passphrase: None,
};
// Create the default config
let mut config = default_config(Network::Mainnet);
config.api_key = Some("<breez api key>".to_string());
// Build the SDK using the config, seed and default storage
let builder = SdkBuilder::new(config, seed)
.with_default_storage("./.data".to_string());
// You can also pass your custom implementations:
// let builder = builder.with_storage(<your storage implementation>)
// let builder = builder.with_real_time_sync_storage(<your real-time sync storage implementation>)
// let builder = builder.with_chain_service(<your chain service implementation>)
// let builder = builder.with_rest_client(<your rest client implementation>)
// let builder = builder.with_key_set(<your key set type>, <use address index>, <account number>)
// let builder = builder.with_payment_observer(<your payment observer implementation>)
let sdk = builder.build().await?;
// Construct the seed using mnemonic words or entropy bytes
let mnemonic = "<mnemonic words>"
let seed = Seed.mnemonic(mnemonic: mnemonic, passphrase: nil)
// Create the default config
var config = defaultConfig(network: Network.mainnet)
config.apiKey = "<breez api key>"
// Build the SDK using the config, seed and default storage
let builder = SdkBuilder(config: config, seed: seed)
await builder.withDefaultStorage(storageDir: "./.data")
// You can also pass your custom implementations:
// await builder.withStorage(<your storage implementation>)
// await builder.withRealTimeSyncStorage(<your real-time sync storage implementation>)
// await builder.withChainService(<your chain service implementation>)
// await builder.withRestClient(<your rest client implementation>)
// await builder.withKeySet(<your key set type>, <use address index>, <account number>)
// await builder.withPaymentObserver(<your payment observer implementation>)
let sdk = try await builder.build()
// Construct the seed using mnemonic words or entropy bytes
val mnemonic = "<mnemonic words>"
val seed = Seed.Mnemonic(mnemonic, null)
// Create the default config
val config = defaultConfig(Network.MAINNET)
config.apiKey = "<breez api key>"
try {
// Build the SDK using the config, seed and default storage
val builder = SdkBuilder(config, seed)
builder.withDefaultStorage("./.data")
// You can also pass your custom implementations:
// builder.withStorage(<your storage implementation>)
// builder.withRealTimeSyncStorage(<your real-time sync storage implementation>)
// builder.withChainService(<your chain service implementation>)
// builder.withRestClient(<your rest client implementation>)
// builder.withKeySet(<your key set type>, <use address index>, <account number>)
// builder.withPaymentObserver(<your payment observer implementation>)
val sdk = builder.build()
} catch (e: Exception) {
// handle error
}
// Construct the seed using mnemonic words or entropy bytes
var mnemonic = "<mnemonic words>";
var seed = new Seed.Mnemonic(mnemonic: mnemonic, passphrase: null);
// Create the default config
var config = BreezSdkSparkMethods.DefaultConfig(Network.Mainnet) with
{
apiKey = "<breez api key>"
};
// Build the SDK using the config, seed and default storage
var builder = new SdkBuilder(config: config, seed: seed);
await builder.WithDefaultStorage(storageDir: "./.data");
// You can also pass your custom implementations:
// await builder.WithStorage(<your storage implementation>)
// await builder.WithRealTimeSyncStorage(<your real-time sync storage implementation>)
// await builder.WithChainService(<your chain service implementation>)
// await builder.WithRestClient(<your rest client implementation>)
// await builder.WithKeySet(<your key set type>, <use address index>, <account number>)
// await builder.WithPaymentObserver(<your payment observer implementation>)
var sdk = await builder.Build();
// Call init when using the SDK in a web environment before calling any other SDK
// methods. This is not needed when using the SDK in a Node.js/Deno environment.
await init()
// Construct the seed using mnemonic words or entropy bytes
const mnemonic = '<mnemonic words>'
const seed: Seed = { type: 'mnemonic', mnemonic, passphrase: undefined }
// Create the default config
const config = defaultConfig('mainnet')
config.apiKey = '<breez api key>'
// Build the SDK using the config, seed and default storage
let builder = SdkBuilder.new(config, seed)
builder = await builder.withDefaultStorage('./.data')
// You can also pass your custom implementations:
// builder = builder.withStorage(<your storage implementation>)
// builder = builder.withRealTimeSyncStorage(<your real-time sync storage implementation>)
// builder = builder.withChainService(<your chain service implementation>)
// builder = builder.withRestClient(<your rest client implementation>)
// builder = builder.withKeySet(<your key set type>, <use address index>, <account number>)
// builder = builder.withPaymentObserver(<your payment observer implementation>)
const sdk = await builder.build()
// Construct the seed using mnemonic words or entropy bytes
const mnemonic = '<mnemonics words>'
const seed = new Seed.Mnemonic({ mnemonic, passphrase: undefined })
// Create the default config
const config = defaultConfig(Network.Mainnet)
config.apiKey = '<breez api key>'
// Build the SDK using the config, seed and default storage
const builder = new SdkBuilder(config, seed)
await builder.withDefaultStorage(`${RNFS.DocumentDirectoryPath}/data`)
// You can also pass your custom implementations:
// await builder.withStorage(<your storage implementation>)
// await builder.withRealTimeSyncStorage(<your real-time sync storage implementation>)
// await builder.withChainService(<your chain service implementation>)
// await builder.withRestClient(<your rest client implementation>)
// await builder.withKeySet(<your key set type>, <use address index>, <account number>)
// await builder.withPaymentObserver(<your payment observer implementation>)
const sdk = await builder.build()
// Construct the seed using mnemonic words or entropy bytes
String mnemonic = "<mnemonic words>";
final seed = Seed.mnemonic(mnemonic: mnemonic, passphrase: null);
// Create the default config
final config = defaultConfig(network: Network.mainnet)
.copyWith(apiKey: "<breez api key>");
// Build the SDK using the config, seed and default storage
final builder = SdkBuilder(config: config, seed: seed);
builder.withDefaultStorage(storageDir: "./.data");
// You can also pass your custom implementations:
// builder.withRestChainService(
// url: "https://custom.chain.service",
// credentials: Credentials(
// username: "service-username", password: "service-password"));
// builder.withKeySet(keySetType: <your key set type>, useAddressIndex: <use address index>, accountNumber: <account number>);
final sdk = await builder.build();
# Construct the seed using mnemonic words or entropy bytes
mnemonic = "<mnemonic words>"
seed = Seed.MNEMONIC(mnemonic=mnemonic, passphrase=None)
# Create the default config
config = default_config(network=Network.MAINNET)
config.api_key = "<breez api key>"
try:
# Build the SDK using the config, seed and default storage
builder = SdkBuilder(config=config, seed=seed)
await builder.with_default_storage(storage_dir="./.data")
# You can also pass your custom implementations:
# await builder.with_storage(<your storage implementation>)
# await builder.with_real_time_sync_storage(<your real-time sync storage implementation>)
# await builder.with_chain_service(<your chain service implementation>)
# await builder.with_rest_client(<your rest client implementation>)
# await builder.with_key_set(<your key set type>, <use address index>, <account number>)
# await builder.with_payment_observer(<your payment observer implementation>)
sdk = await builder.build()
return sdk
except Exception as error:
logging.error(error)
raise
// Construct the seed using mnemonic words or entropy bytes
mnemonic := "<mnemonic words>"
var seed breez_sdk_spark.Seed = breez_sdk_spark.SeedMnemonic{
Mnemonic: mnemonic,
Passphrase: nil,
}
// Create the default config
apiKey := "<breez api key>"
config := breez_sdk_spark.DefaultConfig(breez_sdk_spark.NetworkMainnet)
config.ApiKey = &apiKey
// Build the SDK using the config, seed and default storage
builder := breez_sdk_spark.NewSdkBuilder(config, seed)
builder.WithDefaultStorage("./.data")
// You can also pass your custom implementations:
// builder.WithStorage(<your storage implementation>)
// builder.WithRealTimeSyncStorage(<your real-time sync storage implementation>)
// builder.WithChainService(<your chain service implementation>)
// builder.WithRestClient(<your rest client implementation>)
// builder.WithKeySet(<your key set type>, <use address index>, <account number>)
// builder.WithPaymentObserver(<your payment observer implementation>)
sdk, err := builder.Build()
return sdk, err
Disconnecting API docs
When you’re done using the SDK, call the disconnect method to release any resources in use.
This is particularly useful if you need to re-instantiate the SDK, such as when changing the mnemonic or updating configuration.
pub(crate) async fn disconnect(sdk: &BreezSdk) -> Result<()> {
sdk.disconnect().await?;
Ok(())
}
func disconnect(sdk: BreezSdk) async throws {
try await sdk.disconnect()
}
suspend fun disconnect(sdk: BreezSdk) {
try {
sdk.disconnect()
} catch (e: Exception) {
// handle error
}
}
async Task Disconnect(BreezSdk sdk)
{
await sdk.Disconnect();
}
await sdk.disconnect()
await sdk.disconnect()
Future<void> disconnect(BreezSdk sdk) async {
await sdk.disconnect();
}
async def disconnect(sdk: BreezSdk):
try:
await sdk.disconnect()
except Exception as error:
logging.error(error)
raise
func Disconnect(sdk *breez_sdk_spark.BreezSdk) {
sdk.Disconnect()
}