Quickstart
Learn how to transfer STX tokens using Stacks.js.
In this quickstart guide, you will learn how to build a transaction to transfer STX tokens using Stacks.js.
Check out the reference page for @stacks/transactions
to learn more about building different types of transactions.
Install packages
Add the @stacks/transactions
and @stacks/network
packages to your project using your preferred package manager.
Build the transaction for a STX transfer
To set up a STX token transfer transaction, use the makeSTXTokenTransfer
function.
This function requires a private key (senderKey
), a specified network
, and the details of the transfer.
1import { makeSTXTokenTransfer } from "@stacks/transactions";2import { STACKS_TESTNET } from "@stacks/network";34const transaction = await makeSTXTokenTransfer({5recipient: "ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5",6amount: 42000000,7senderKey:8"753b7cc01a1a2e86221266a154af739463fce51219d97e4f856cd7200c3bd2a601",9network: STACKS_TESTNET,10});
There are a few optional fields for a STX transfer transaction, including memo
, nonce
, and fee
.
Use memo
to add a message as part of the transaction. If you don't want the builder to fetch the nonce
and fee
from a Stacks node, you can manually set these fields.
Broadcast the transaction
Once you've constructed a valid transaction, you can broadcast it to the network using the broadcastTransaction
function.
1import { broadcastTransaction,makeSTXTokenTransfer } from "@stacks/transactions";2import { STACKS_TESTNET } from "@stacks/network";34const transaction = await makeSTXTokenTransfer({5recipient: "ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5",6amount: 42000000,7senderKey:8"753b7cc01a1a2e86221266a154af739463fce51219d97e4f856cd7200c3bd2a601",9network: STACKS_TESTNET,10});1112const broadcastResponse = await broadcastTransaction({ transaction });13const txId = broadcastResponse.txid;
Upon success, this will return a StacksTransaction
object that contains information about the transaction, including the txid
.