Create and broadcast transactions to the Stacks blockchain network using Stacks.js.
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 learn how to:
Install the necessary packages for building and broadcasting transactions
Build different types of transactions
Sign and broadcast those transactions to the Stacks blockchain
Process and handle the results
The examples in this guide are designed to be minimalistic, allowing you to easily adapt and customize these examples to fit your application's specific needs.
To transfer STX, use the makeSTXTokenTransfer function provided by the @stacks/transactions package:
This builds a STX token transfer transaction. The makeSTXTokenTransfer function takes parameters like the recipient address, amount to transfer, and the sender's private key, ie senderKey.
Several parameters are available for calling makeSTXTokenTransfer. To learn more, check out our transactions reference page.
After building the transaction, broadcast it to the Stacks blockchain network using the broadcastTransaction function from the @stacks/transactions package.
Pass in the transaction object you created in the previous step to the broadcastTransaction function.
Handle the response by logging the response object.
This code sends the signed transaction to the Stacks blockchain. The broadcastTransaction function returns a response containing the transaction ID, which can be used to track the transaction on the blockchain.