Buying Bitcoin

This section of the Breez SDK documentation provides an example on purchasing Bitcoin using Moonpay as the provider. The example code snippet demonstrates how to initiate a Bitcoin purchase transaction using the Breez SDK.

The SDK will generate a Bitcoin address and prepare a URL using the specified provider. The user needs to open the URL and proceed with the provider flow to buy the Bitcoin.

Once the buy is completed, the provider will transfer the Bitcoin to the generated address and Breez SDK will add the received Bitcoin to the Lightning balance.

Rust
let res = sdk
    .buy_bitcoin(BuyBitcoinRequest {
        provider: BuyBitcoinProvider::Moonpay,
        opening_fee_params: None,
    })
    .await?;
Swift
let buyBitcoinResponse = try? sdk.buyBitcoin(
    req: BuyBitcoinRequest(provider: .moonpay))
Kotlin
try {
    // Choose your provider
    val provider = BuyBitcoinProvider.MOONPAY
    // request the url to proceed with the Bitcoin acquisition
    val url = sdk.buyBitcoin(BuyBitcoinRequest(provider)).url
} catch (e: Exception) {
    // Handle error
}
React Native
try {
  const buyBitcoinResponse = await buyBitcoin({
    provider: BuyBitcoinProvider.MOONPAY
  })
} catch (err) {
  console.error(err)
}
Dart
BuyBitcoinRequest req = const BuyBitcoinRequest(provider: BuyBitcoinProvider.Moonpay);
BuyBitcoinResponse resp = await BreezSDK().buyBitcoin(req: req);
Python
req =  breez_sdk.BuyBitcoinRequest(breez_sdk.BuyBitcoinProvider.MOONPAY)
buy_bitcoin_resp = sdk_services.buy_bitcoin(req=req)
Go
buyBitcoinRequest := breez_sdk.BuyBitcoinRequest{
    Provider: breez_sdk.BuyBitcoinProviderMoonpay,
}
if buyBitcoinResponse, err := sdk.BuyBitcoin(buyBitcoinRequest); err == nil {
    log.Printf("%#v", buyBitcoinResponse)
}
C#
try
{
    var buyBitcoinResponse = sdk.BuyBitcoin(
        new BuyBitcoinRequest(BuyBitcoinProvider.MOONPAY));
}
catch (Exception)
{
    // Handle error
}