Broadcast Transactions
The process of broadcasting transactions is fundamental for interacting with blockchains, whether you're transferring tokens, deploying contracts, or executing contract functions.
In this guide you will:
Install the required packages
Connect to a user's wallet
Sign and broadcast different transaction types
Handle transaction results
Setup and installation
Install the required packages to start building and broadcasting transactions.
npm install @stacks/connect @stacks/transactionsyarn add @stacks/connect @stacks/transactionspnpm add @stacks/connect @stacks/transactionsConnect to a user's wallet
Before signing transactions, users need to connect their wallet to your application. Use the connect function to initiate a wallet connection:
import { connect, isConnected } from '@stacks/connect';
async function connectWallet() {
if (!isConnected()) {
const response = await connect();
console.log('Connected with addresses:', response);
}
}Sign and broadcast transactions
There are three common transaction flows you can build:
STX transfer
Use stx_transferStx to send tokens:
Contract deployment
Deploy a contract with stx_deployContract:
Contract execution
Call contract functions with stx_callContract:
When passing arguments, construct Clarity values via Cl:
Handle transaction results
When a transaction is signed and broadcast, the request method returns a response object containing information about the transaction:
You can use the transaction ID to create a link to view the transaction in the explorer:
Last updated
Was this helpful?
