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:
- Storage and Real-Time Storage to manage stored data
- Bitcoin Chain Service to provide network data
- LNURL Client to make REST requests
- Fiat Service to provide Fiat currencies and exchange rates
- Change the Key Set to alter the derivation path used
- Payment Observer to be notified before payments occur
See Customizing the SDK for examples of this advanced initialization pattern.
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()
}