Bitcoin - Getting Started (SDK)

Use the Tatum SDK with the UTXO Wallet Provider to generate a Bitcoin mnemonic, xpub, private key and address, and to sign and broadcast BTC transactions.

Steps

Step_1: Get Started

Install the UTXO wallet provider for Bitcoin wallets (@tatumio/utxo-wallet-provider) together with the Tatum SDK (@tatumio/tatum).

npm install @tatumio/tatum @tatumio/utxo-wallet-provider

Then initialize the SDK for Bitcoin with the UTXO Wallet Provider. Replace {YOUR_API_KEY} with your API key from the Tatum Dashboard. Call destroy() when you are done: it stops the SDK's background jobs so the script can exit.

import { TatumSDK, Network, Bitcoin } from '@tatumio/tatum';
import { UtxoWalletProvider } from '@tatumio/utxo-wallet-provider';

const tatum = await TatumSDK.init<Bitcoin>({
  network: Network.BITCOIN,
  apiKey: '{YOUR_API_KEY}',
  configureWalletProviders: [UtxoWalletProvider],
});

// Use tatum.walletProvider.use(UtxoWalletProvider) as shown in the steps below

await tatum.destroy();

Each example below is a complete script. The examples are ES modules that use top-level await: run the JavaScript ones as .mjs files or set "type": "module" in your package.json.

Step_2: Generate a Mnemonic

Generating a mnemonic means creating a 24-word phrase that will be the foundation for your wallet. This phrase is like a master key from which all your wallet addresses and their private keys can be generated.

  • Tatum does not store Mnemonics and or Private Keys.
  • The responsibility of keeping your Mnemonics and Private Keys secure rests solely with you, the User.
  • Additional information about Mnemonics and Private Keys is available in Managing Mnemonics and PrivateKeys.
// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network, Bitcoin } from '@tatumio/tatum';
import { UtxoWalletProvider } from '@tatumio/utxo-wallet-provider';

const tatum = await TatumSDK.init<Bitcoin>({
  network: Network.BITCOIN,
  apiKey: '{YOUR_API_KEY}',
  configureWalletProviders: [UtxoWalletProvider],
});

// Generate a mnemonic using the UTXO Wallet Provider submodule
const mnemonic = tatum.walletProvider.use(UtxoWalletProvider).generateMnemonic();

console.log(mnemonic);  // This will print the generated mnemonic

await tatum.destroy();
// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network } from '@tatumio/tatum';
import { UtxoWalletProvider } from '@tatumio/utxo-wallet-provider';

const tatum = await TatumSDK.init({
  network: Network.BITCOIN,
  apiKey: '{YOUR_API_KEY}',
  configureWalletProviders: [UtxoWalletProvider],
});

// Generate a mnemonic using the UTXO Wallet Provider submodule
const mnemonic = tatum.walletProvider.use(UtxoWalletProvider).generateMnemonic();

console.log(mnemonic);  // This will print the generated mnemonic

await tatum.destroy();

Step_3: Generate XPUB

Generating an extended public key (xpub) is a way to allow the creation of public addresses without exposing the corresponding private keys. It's an important function for maintaining security while still being able to receive funds.

You can generate an xpub with or without a mnemonic. If you call generateXpub() without a mnemonic, the SDK generates a new one and returns it in xpubDetails.mnemonic.

// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network, Bitcoin } from '@tatumio/tatum';
import { UtxoWalletProvider } from '@tatumio/utxo-wallet-provider';

const tatum = await TatumSDK.init<Bitcoin>({
  network: Network.BITCOIN,
  apiKey: '{YOUR_API_KEY}',
  configureWalletProviders: [UtxoWalletProvider],
});

const mnemonic = '{YOUR_MNEMONIC}'; // The mnemonic from Step 2

// Generate an xpub from the mnemonic
const xpubDetails = await tatum.walletProvider.use(UtxoWalletProvider)
  .generateXpub(mnemonic);

console.log(xpubDetails.xpub);  // This will print the generated xpub

await tatum.destroy();
// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network } from '@tatumio/tatum';
import { UtxoWalletProvider } from '@tatumio/utxo-wallet-provider';

const tatum = await TatumSDK.init({
  network: Network.BITCOIN,
  apiKey: '{YOUR_API_KEY}',
  configureWalletProviders: [UtxoWalletProvider],
});

const mnemonic = '{YOUR_MNEMONIC}'; // The mnemonic from Step 2

// Generate an xpub from the mnemonic
const xpubDetails = await tatum.walletProvider.use(UtxoWalletProvider)
  .generateXpub(mnemonic);

console.log(xpubDetails.xpub);  // This will print the generated xpub

await tatum.destroy();

Step_4: Generate Private Key

Creating a private key is creating a unique and secret key that belongs to you. This key is crucial for accessing and controlling your funds. The second argument is the derivation index: 0 returns the first private key derived from the mnemonic.

// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network, Bitcoin } from '@tatumio/tatum';
import { UtxoWalletProvider } from '@tatumio/utxo-wallet-provider';

const tatum = await TatumSDK.init<Bitcoin>({
  network: Network.BITCOIN,
  apiKey: '{YOUR_API_KEY}',
  configureWalletProviders: [UtxoWalletProvider],
});

const mnemonic = '{YOUR_MNEMONIC}'; // The mnemonic from Step 2

// Generate the private key at index 0 from the mnemonic
const privateKey = await tatum.walletProvider.use(UtxoWalletProvider)
  .generatePrivateKeyFromMnemonic(mnemonic, 0);

console.log(privateKey);  // This will print the generated private key

await tatum.destroy();
// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network } from '@tatumio/tatum';
import { UtxoWalletProvider } from '@tatumio/utxo-wallet-provider';

const tatum = await TatumSDK.init({
  network: Network.BITCOIN,
  apiKey: '{YOUR_API_KEY}',
  configureWalletProviders: [UtxoWalletProvider],
});

const mnemonic = '{YOUR_MNEMONIC}'; // The mnemonic from Step 2

// Generate the private key at index 0 from the mnemonic
const privateKey = await tatum.walletProvider.use(UtxoWalletProvider)
  .generatePrivateKeyFromMnemonic(mnemonic, 0);

console.log(privateKey);  // This will print the generated private key

await tatum.destroy();

Step_5: Generate Address

Generating an address is about creating a public address where others can send you funds. It's like an account number in the blockchain world.

You can generate an address from either a mnemonic or an xpub. When the xpub comes from the same mnemonic, both return the same address for the same index.

// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network, Bitcoin } from '@tatumio/tatum';
import { UtxoWalletProvider } from '@tatumio/utxo-wallet-provider';

const tatum = await TatumSDK.init<Bitcoin>({
  network: Network.BITCOIN,
  apiKey: '{YOUR_API_KEY}',
  configureWalletProviders: [UtxoWalletProvider],
});

const mnemonic = '{YOUR_MNEMONIC}'; // The mnemonic from Step 2
const xpub = '{YOUR_XPUB}'; // The xpub from Step 3

// Generate the address at index 0 from the mnemonic or from the xpub
const addressFromMnemonic = await tatum.walletProvider
  .use(UtxoWalletProvider).generateAddressFromMnemonic(mnemonic, 0);
const addressFromXpub = tatum.walletProvider
  .use(UtxoWalletProvider).generateAddressFromXpub(xpub, 0);

// This will print the generated address from mnemonic
console.log(addressFromMnemonic);
// This will print the generated address from xpub
console.log(addressFromXpub);

await tatum.destroy();
// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network } from '@tatumio/tatum';
import { UtxoWalletProvider } from '@tatumio/utxo-wallet-provider';

const tatum = await TatumSDK.init({
  network: Network.BITCOIN,
  apiKey: '{YOUR_API_KEY}',
  configureWalletProviders: [UtxoWalletProvider],
});

const mnemonic = '{YOUR_MNEMONIC}'; // The mnemonic from Step 2
const xpub = '{YOUR_XPUB}'; // The xpub from Step 3

// Generate the address at index 0 from the mnemonic or from the xpub
const addressFromMnemonic = await tatum.walletProvider
  .use(UtxoWalletProvider).generateAddressFromMnemonic(mnemonic, 0);
const addressFromXpub = tatum.walletProvider
  .use(UtxoWalletProvider).generateAddressFromXpub(xpub, 0);

// This will print the generated address from mnemonic
console.log(addressFromMnemonic);
// This will print the generated address from xpub
console.log(addressFromXpub);

await tatum.destroy();

Step_6: Sign and Broadcast a Transaction

Signing and broadcasting a transaction is about authorising a transfer of funds or interaction with a smart contract and then sending that authorisation to the blockchain to be processed.

The payload follows the UtxoTxPayload type. fromAddress lists the addresses to spend from with their private keys, and to lists the recipients with the amount (value) in BTC. fee (in BTC) and changeAddress only work together: set both, or leave both out and the difference between the inputs and outputs is paid as the fee. To spend specific UTXOs instead of scanning addresses, replace fromAddress with fromUTXO (a list of txHash, index and privateKey).

// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network, Bitcoin } from '@tatumio/tatum';
import { UtxoWalletProvider } from '@tatumio/utxo-wallet-provider';

const tatum = await TatumSDK.init<Bitcoin>({
  network: Network.BITCOIN,
  apiKey: '{YOUR_API_KEY}',
  configureWalletProviders: [UtxoWalletProvider],
});

// Define your transaction details
const payloadUtxo = {
  fromAddress: [{ address: '{YOUR_WALLET_ADDRESS}', privateKey: '{YOUR_PRIVATE_KEY}' }],
  to: [{ address: '{TARGET_WALLET_ADDRESS}', value: 0.0001 }], // Amount in BTC
  fee: '0.00001', // Fee in BTC
  changeAddress: '{CHANGE_WALLET_ADDRESS}',
};

// Sign and broadcast the transaction using the UTXO Wallet Provider submodule
const txHash = await tatum.walletProvider.use(UtxoWalletProvider)
  .signAndBroadcast(payloadUtxo);

// This will print the transaction hash of the broadcasted transaction
console.log(txHash);

await tatum.destroy();
// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network } from '@tatumio/tatum';
import { UtxoWalletProvider } from '@tatumio/utxo-wallet-provider';

const tatum = await TatumSDK.init({
  network: Network.BITCOIN,
  apiKey: '{YOUR_API_KEY}',
  configureWalletProviders: [UtxoWalletProvider],
});

// Define your transaction details
const payloadUtxo = {
  fromAddress: [{ address: '{YOUR_WALLET_ADDRESS}', privateKey: '{YOUR_PRIVATE_KEY}' }],
  to: [{ address: '{TARGET_WALLET_ADDRESS}', value: 0.0001 }], // Amount in BTC
  fee: '0.00001', // Fee in BTC
  changeAddress: '{CHANGE_WALLET_ADDRESS}',
};

// Sign and broadcast the transaction using the UTXO Wallet Provider submodule
const txHash = await tatum.walletProvider.use(UtxoWalletProvider)
  .signAndBroadcast(payloadUtxo);

// This will print the transaction hash of the broadcasted transaction
console.log(txHash);

await tatum.destroy();

Good to Know

  • You can find the official guide on how to build a RAW BTC Transaction in the following article.
  • If your transaction has low fees, it may get stuck in the mempool for two (2) weeks. To speed it up, you may use Child Pays for Parent - Replace By Fee is unavailable for transactions broadcasted via core API.
  • Fee Estimates may fluctuate one way or the other, within seconds.
    • Tatum Mainnet Fee Estimates are generally in line with other providers.
    • The Testnet Fee Estimate is generally unreliable. Since testnet coins have "no value", users tend to set random amounts greatly skewing the estimates.
  • Be mindful of Tatum's derivation path. Find additional information in Tatum's Derivation Path.
  • Familiarize yourself with the Safety & Security Basics in Best Practices for Security.

Did this page help you?