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.

npm install @stacks/transactions @stacks/network

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.

stx-transfer.ts
1
import { makeSTXTokenTransfer } from "@stacks/transactions";
2
import { STACKS_TESTNET } from "@stacks/network";
3
4
const transaction = await makeSTXTokenTransfer({
5
recipient: "ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5",
6
amount: 42000000,
7
senderKey:
8
"753b7cc01a1a2e86221266a154af739463fce51219d97e4f856cd7200c3bd2a601",
9
network: 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.

stx-transfer.ts
1
import { broadcastTransaction,makeSTXTokenTransfer } from "@stacks/transactions";
2
import { STACKS_TESTNET } from "@stacks/network";
3
4
const transaction = await makeSTXTokenTransfer({
5
recipient: "ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5",
6
amount: 42000000,
7
senderKey:
8
"753b7cc01a1a2e86221266a154af739463fce51219d97e4f856cd7200c3bd2a601",
9
network: STACKS_TESTNET,
10
});
11
12
const broadcastResponse = await broadcastTransaction({ transaction });
13
const txId = broadcastResponse.txid;

Upon success, this will return a StacksTransaction object that contains information about the transaction, including the txid.

Next steps