Buying Bitcoin
The Breez SDK allows users to purchase Bitcoin through external providers, such as Moonpay. The user is directed to a provider URL in their browser to complete the purchase, and the funds are deposited directly into their wallet.
To initiate a Bitcoin purchase:
Rust
// Optionally, lock the purchase to a specific amount
let optional_locked_amount_sat = Some(100_000);
// Optionally, set a redirect URL for after the purchase is completed
let optional_redirect_url = Some("https://example.com/purchase-complete".to_string());
let request = BuyBitcoinRequest {
locked_amount_sat: optional_locked_amount_sat,
redirect_url: optional_redirect_url,
};
let response = sdk.buy_bitcoin(request).await?;
info!("Open this URL in a browser to complete the purchase:");
info!("{}", response.url);
Swift
// Optionally, lock the purchase to a specific amount
let optionalLockedAmountSat: UInt64 = 100_000
// Optionally, set a redirect URL for after the purchase is completed
let optionalRedirectUrl = "https://example.com/purchase-complete"
let request = BuyBitcoinRequest(
lockedAmountSat: optionalLockedAmountSat,
redirectUrl: optionalRedirectUrl
)
let response = try await sdk.buyBitcoin(request: request)
print("Open this URL in a browser to complete the purchase:")
print("\(response.url)")
Kotlin
// Optionally, lock the purchase to a specific amount
val optionalLockedAmountSat: ULong = 100_000u
// Optionally, set a redirect URL for after the purchase is completed
val optionalRedirectUrl = "https://example.com/purchase-complete"
val request = BuyBitcoinRequest(
lockedAmountSat = optionalLockedAmountSat,
redirectUrl = optionalRedirectUrl
)
val response = sdk.buyBitcoin(request)
// Log.v("Breez", "Open this URL in a browser to complete the purchase:")
// Log.v("Breez", "${response.url}")
C#
// Optionally, lock the purchase to a specific amount
var optionalLockedAmountSat = (ulong)100000;
// Optionally, set a redirect URL for after the purchase is completed
var optionalRedirectUrl = "https://example.com/purchase-complete";
var request = new BuyBitcoinRequest(
lockedAmountSat: optionalLockedAmountSat,
redirectUrl: optionalRedirectUrl
);
var response = await sdk.BuyBitcoin(request: request);
Console.WriteLine("Open this URL in a browser to complete the purchase:");
Console.WriteLine($"{response.url}");
Javascript
// Optionally, lock the purchase to a specific amount
const optionalLockedAmountSat = 100_000
// Optionally, set a redirect URL for after the purchase is completed
const optionalRedirectUrl = 'https://example.com/purchase-complete'
const request: BuyBitcoinRequest = {
lockedAmountSat: optionalLockedAmountSat,
redirectUrl: optionalRedirectUrl
}
const response = await sdk.buyBitcoin(request)
console.log('Open this URL in a browser to complete the purchase:')
console.log(response.url)
React Native
// Optionally, lock the purchase to a specific amount
const optionalLockedAmountSat = BigInt(100_000)
// Optionally, set a redirect URL for after the purchase is completed
const optionalRedirectUrl = 'https://example.com/purchase-complete'
const request: BuyBitcoinRequest = {
lockedAmountSat: optionalLockedAmountSat,
redirectUrl: optionalRedirectUrl
}
const response = await sdk.buyBitcoin(request)
console.log('Open this URL in a browser to complete the purchase:')
console.log(response.url)
Flutter
// Optionally, lock the purchase to a specific amount
final optionalLockedAmountSat = BigInt.from(100000);
// Optionally, set a redirect URL for after the purchase is completed
final optionalRedirectUrl = "https://example.com/purchase-complete";
final request = BuyBitcoinRequest(
lockedAmountSat: optionalLockedAmountSat,
redirectUrl: optionalRedirectUrl);
final response = await sdk.buyBitcoin(request: request);
print("Open this URL in a browser to complete the purchase:");
print(response.url);
Python
# Optionally, lock the purchase to a specific amount
optional_locked_amount_sat = 100_000
# Optionally, set a redirect URL for after the purchase is completed
optional_redirect_url = "https://example.com/purchase-complete"
try:
request = BuyBitcoinRequest(
locked_amount_sat=optional_locked_amount_sat,
redirect_url=optional_redirect_url,
)
response = await sdk.buy_bitcoin(request=request)
logging.debug("Open this URL in a browser to complete the purchase:")
logging.debug(response.url)
except Exception as error:
logging.error(error)
raise
Go
// Optionally, lock the purchase to a specific amount
optionalLockedAmountSat := uint64(100_000)
// Optionally, set a redirect URL for after the purchase is completed
optionalRedirectUrl := "https://example.com/purchase-complete"
request := breez_sdk_spark.BuyBitcoinRequest{
LockedAmountSat: &optionalLockedAmountSat,
RedirectUrl: &optionalRedirectUrl,
}
response, err := sdk.BuyBitcoin(request)
if err != nil {
return err
}
log.Printf("Open this URL in a browser to complete the purchase:")
log.Printf("%v", response.Url)
The returned URL should be opened in a browser for the user to complete the purchase.
Developer note
MoonPay supports Apple Pay and Google Pay, but these payment methods will not work inside an iframe or standard web view. To ensure compatibility:- iOS: Open the URL using
SFSafariViewController. - Android: Open the URL using Chrome Custom Tabs.
- Desktop: Apple Pay requires Safari; Google Pay requires Chrome.