Buying Bitcoin

The SDK also provides access to a Fiat on-ramp to purchase Bitcoin using Moonpay as a provider. It will generate a Bitcoin address and prepare a URL using the specified provider. The user then needs to open the URL and proceed with the provider flow to buy Bitcoin. Once the buy is completed, the provider will transfer the purchased amount to the Bitcoin address.

Checking the limits

Fetch the current onchain limits to check the minimum and maximum allowed to purchase.

Rust
let current_limits = sdk.fetch_onchain_limits().await?;

info!("Minimum amount: {} sats", current_limits.receive.min_sat);
info!("Maximum amount: {} sats", current_limits.receive.max_sat);
Swift
if let currentLimits = try? sdk.fetchOnchainLimits() {
    print("Minimum amount, in sats: \(currentLimits.receive.minSat)")
    print("Maximum amount, in sats: \(currentLimits.receive.maxSat)")
}
Kotlin
try {
    val currentLimits = sdk.fetchOnchainLimits()
    // Log.v("Breez", "Minimum amount, in sats: ${currentLimits.receive.minSat}")
    // Log.v("Breez", "Maximum amount, in sats: ${currentLimits.receive.maxSat}")
} catch (e: Exception) {
    // handle error
}
React Native
try {
  const currentLimits = await fetchOnchainLimits()

  console.log(`Minimum amount, in sats: ${currentLimits.receive.minSat}`)
  console.log(`Maximum amount, in sats: ${currentLimits.receive.maxSat}`)
} catch (err) {
  console.error(err)
}
Dart
OnchainPaymentLimitsResponse currentLimits = await breezSDKLiquid.instance!.fetchOnchainLimits();
print("Minimum amount: ${currentLimits.receive.minSat} sats");
print("Maximum amount: ${currentLimits.receive.maxSat} sats");
Python
try:
    current_limits = sdk.fetch_onchain_limits()
    logging.debug("Minimum amount, in sats ", current_limits.receive.min_sat)
    logging.debug("Maximum amount, in sats ", current_limits.receive.max_sat)
    return current_limits
except Exception as error:
    logging.error(error)
    raise
Go
if currentLimits, err := sdk.FetchOnchainLimits(); err == nil {
    log.Printf("Minimum amount, in sats: %v", currentLimits.Receive.MinSat)
    log.Printf("Maximum amount, in sats: %v", currentLimits.Receive.MaxSat)
}
C#
try
{
    var currentLimits = sdk.FetchOnchainLimits();
    Console.WriteLine($"Minimum amount, in sats: {currentLimits.receive.minSat}");
    Console.WriteLine($"Maximum amount, in sats: {currentLimits.receive.maxSat}");
}
catch (Exception)
{
    // Handle error
}

Preparing to buy, checking fees

Using the current onchain limits, select a provider and amount to purchase.

Rust
let prepare_response = sdk
    .prepare_buy_bitcoin(PrepareBuyBitcoinRequest {
        provider: BuyBitcoinProvider::Moonpay,
        amount_sat: current_limits.receive.min_sat,
    })
    .await?;

// Check the fees are acceptable before proceeding
let receive_fees_sat = prepare_response.fees_sat;
info!("Fees: {} sats", receive_fees_sat);
Swift
let req = PrepareBuyBitcoinRequest(
    provider: .moonpay,
    amountSat: currentLimits.receive.minSat)

if let prepareResponse = try? sdk.prepareBuyBitcoin(req: req) {
    // Check the fees are acceptable before proceeding
    let receiveFeesSat = prepareResponse.feesSat;
    print("Fees: \(receiveFeesSat) sats")
}
Kotlin
try {
    val req = PrepareBuyBitcoinRequest(BuyBitcoinProvider.MOONPAY, currentLimits.receive.minSat)
    val prepareResponse = sdk.prepareBuyBitcoin(req)

    // Check the fees are acceptable before proceeding
    val receiveFeesSat = prepareResponse.feesSat;
    // Log.v("Breez", "Fees: ${receiveFeesSat} sats")
} catch (e: Exception) {
    // Handle error
}
React Native
try {
  const prepareRes = await prepareBuyBitcoin({
    provider: BuyBitcoinProvider.MOONPAY,
    amountSat: currentLimits.receive.minSat
  })

  // Check the fees are acceptable before proceeding
  const receiveFeesSat = prepareRes.feesSat
  console.log(`Fees: ${receiveFeesSat} sats`)
} catch (err) {
  console.error(err)
}
Dart
PrepareBuyBitcoinRequest req =
    PrepareBuyBitcoinRequest(provider: BuyBitcoinProvider.moonpay, amountSat: currentLimits.receive.minSat);
PrepareBuyBitcoinResponse prepareRes = await breezSDKLiquid.instance!.prepareBuyBitcoin(req: req);

// Check the fees are acceptable before proceeding
BigInt receiveFeesSat = prepareRes.feesSat;
print("Fees: $receiveFeesSat sats");
Python
try:
    req = PrepareBuyBitcoinRequest(BuyBitcoinProvider.MOONPAY, current_limits.receive.min_sat)
    prepare_response = sdk.prepare_buy_bitcoin(req)

    # Check the fees are acceptable before proceeding
    receive_fees_sat = prepare_response.fees_sat
    logging.debug("Fees: ", receive_fees_sat, " sats")
    return prepare_response
except Exception as error:
    logging.error(error)
    raise
Go
req := breez_sdk_liquid.PrepareBuyBitcoinRequest{
    Provider:  breez_sdk_liquid.BuyBitcoinProviderMoonpay,
    AmountSat: currentLimits.Receive.MinSat,
}

// Check the fees are acceptable before proceeding
if prepareResponse, err := sdk.PrepareBuyBitcoin(req); err == nil {
    receiveFeesSat := prepareResponse.FeesSat
    log.Printf("Fees: %v sats", receiveFeesSat)
}
C#
try
{
    var req = new PrepareBuyBitcoinRequest(BuyBitcoinProvider.Moonpay, currentLimits.receive.minSat);
    var prepareResponse = sdk.PrepareBuyBitcoin(req);

    // Check the fees are acceptable before proceeding
    var receiveFeesSat = prepareResponse.feesSat;
    Console.WriteLine($"Fees: {receiveFeesSat} sats");
}
catch (Exception)
{
    // Handle error
}

Generate the URL

Generate a URL to the provider with a Bitcoin address to receive the purchase to. You can also pass in an optional redirect URL here that the provider redirects to after a successful purchase.

Rust
let url = sdk.buy_bitcoin(BuyBitcoinRequest {
    prepare_response,
    redirect_url: None,
})
.await?;
Swift
let req = BuyBitcoinRequest(prepareResponse: prepareResponse)
let url = try? sdk.buyBitcoin(req: req)
Kotlin
try {
    val req = BuyBitcoinRequest(prepareResponse)
    val url = sdk.buyBitcoin(req)
} catch (e: Exception) {
    // Handle error
}
React Native
try {
  const url = await buyBitcoin({
    prepareResponse
  })
} catch (err) {
  console.error(err)
}
Dart
BuyBitcoinRequest req = BuyBitcoinRequest(prepareResponse: prepareResponse);
String url = await breezSDKLiquid.instance!.buyBitcoin(req: req);
Python
try:
    req = BuyBitcoinRequest(prepareResponse)
    url = sdk.buy_bitcoin(req)
except Exception as error:
    logging.error(error)
    raise
Go
req := breez_sdk_liquid.BuyBitcoinRequest{
    PrepareResponse: prepareResponse,
}

if url, err := sdk.BuyBitcoin(req); err == nil {
    log.Printf("Url: %v", url)
}
C#
try
{
    var req = new BuyBitcoinRequest(prepareResponse);
    var url = sdk.BuyBitcoin(req);
}
catch (Exception)
{
    // Handle error
}