Cross Chain Swaps with Bitcoin
Use the /quotes and /status endpoints on the 0x Cross Chain API to swap native BTC across chains:
- Buy BTC: sell a token on another chain and receive native BTC at a mainnet Bitcoin address.
- Sell BTC: sell native BTC from a mainnet Bitcoin address and receive a token on another chain.
The examples below use USDC on Base as the other side of the swap.
Prerequisites
Before you start, make sure you have:
- A 0x API key.
- A mainnet Bitcoin address. Use a native SegWit address (
bc1q, 42 characters) when you can. - To buy BTC: a funded EVM wallet set up as shown in Get started.
- To sell BTC: confirmed BTC UTXOs at one mainnet Bitcoin address that cover both the sell amount and the miner fee.
- To sell BTC: a private key in WIF format stored in
BITCOIN_WIF, plus bitcoinjs-lib,ecpair, andtiny-secp256k1to run the signing example.
Bitcoin request constants
Use these values in every Bitcoin request:
Pass BTC amounts as base-unit integers, the same as EVM token amounts. The base unit of BTC is the satoshi, so 1 BTC = 100000000 satoshis.
Bitcoin terms
Supported Bitcoin addresses
When destinationChain is bitcoin, destinationAddress is required and accepts any mainnet Bitcoin address. When originChain is bitcoin, 0x funds the swap from confirmed UTXOs at one originAddress using one of the supported single-key types below.
Keep these funding limits in mind when selling BTC:
- 0x selects confirmed UTXOs from a single
originAddressand cannot combine UTXOs across multiple addresses. - Every sell transaction must fit within Bitcoin’s standard 100,000-vbyte (400,000-weight-unit) limit. Legacy (
1...) origins have an additional cap of 50 selected UTXOs per quote because each input embeds its full previous transaction. If your address holds many small UTXOs, consolidate them or reducesellAmount. - Receiving BTC at an address does not guarantee that your wallet can sign PSBTs for that address type. Confirm that your wallet signs the returned PSBT with
SIGHASH_ALLbefore requesting a sell quote.
Buy BTC with a token on another chain
1. Fetch a quote
Set destinationChain to bitcoin and buyToken to bc1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqmql8k8.
If you omit destinationAddress, the API returns HTTP 400 INPUT_INVALID with the reason Bitcoin destinationAddress is required.
2. Approve and send the transaction
Check quote.issues.allowance first. If an allowance is needed, approve the spender and wait for confirmation before sending the swap transaction.
3. Monitor execution
Poll /status with the origin transaction hash and the quoteId from the quote you executed.
When status reaches bridge_filled, the BTC has been sent to destinationAddress.
Sell BTC for a token on another chain
1. Fetch a quote
Set originChain to bitcoin and sellToken to bc1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqmql8k8. If originAddress is a wrapped SegWit (3...) address, also pass its 33-byte compressed public key in bitcoinOriginPublicKey.
Review these response details before signing:
allowanceTargetis alwaysnull. Bitcoin swaps need no token approval and no wrap transaction.quotes[i].sellAmountandsteps[0].sellAmountreport the bridge deposit plus payable service fees, before the miner fee. If wallet change is too small to create a valid change output, 0x adds that remainder to the bridge deposit instead of paying it to miners. That remainder can makesellAmountslightly higher than requested.gasCosts.totalreports the Bitcoin miner fee in satoshis (sum(inputs) - sum(outputs)).
Example sell quote response
A Bitcoin block confirmation takes about 10 minutes.
2. Sign the PSBT
Decode the hex PSBT from quote.transaction.details.psbt, sign all inputs, finalize them, and extract the raw transaction hex.
Signing details by address type
- Sign every input with
SIGHASH_ALL. Returned PSBTs set each input’ssequenceto0xfffffffd, which signals opt-in Replace-By-Fee (BIP 125) with relative locktime disabled (BIP 68). 0x does not build or broadcast replacement transactions. - Legacy (
1...): 0x attaches the verified full previous transaction innon_witness_utxofor every legacy input. Your wallet must use a compressed public key, and a single quote selects at most 50 legacy inputs. - Wrapped SegWit (
3...): 0x derives the P2SH-P2WPKHredeemScriptfrombitcoinOriginPublicKeyand attaches it to every input and any change output in the PSBT. - Taproot (
bc1p...): Key-path spends require a BIP 86 tweaked Schnorr signer and the x-onlytapInternalKeyon each input before callingsignAllInputs.
3. Broadcast the transaction
Submit the finalized raw transaction hex to a Bitcoin broadcast endpoint. The example below uses mempool.space, which returns the transaction ID (txid) as plain text.
4. Monitor execution
Poll /status with originChain set to bitcoin, originTxHash set to the txid, and the quoteId from the quote you executed.
For the full status lifecycle and failure handling, see Tracking Transaction Status.
Integrator fees
Pass feeBps or feePpm, and one or more mainnet Bitcoin addresses in feeRecipient, to collect integrator fees in native BTC. Omit feeToken, or set it to the native BTC token (bc1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqmql8k8). Parameter format matches Monetize Your App.
Each payable fee is a separate output in the Bitcoin transaction. The fee has to meet the dust limit for that recipient address type on its own. If a fee is below the limit, 0x waives it, leaves those satoshis in the bridge deposit, and omits it from fees.integratorFees.