Transactions

Learn how to create and broadcast transactions with Stacks.js.


The following shows how to create a simple transaction (STX transfer) using Stacks.js in different environments.

Creating a transaction

Using Stacks Connect

1
import { openSTXTransfer } from '@stacks/connect';
2
3
openSTXTransfer({
4
network: 'testnet',
5
6
recipient: 'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK', // which address you are sending to
7
amount: 10000, // tokens, denominated in micro-STX
8
9
onFinish: response => console.log(response.txid),
10
onCancel: () => console.log('User canceled'),
11
});

Using a private key

For full manual transaction signing, you need to provide the sender's private key. Treat the private key as a secret and never expose it to the public.

1
import { makeSTXTokenTransfer } from '@stacks/transactions';
2
3
const privateKey = randomPrivateKey(); // see "Private Keys & Wallets" page
4
5
const tx = await makeSTXTokenTransfer({
6
recipient: 'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK', // which address you are sending to
7
amount: 10000, // tokens, denominated in micro-STX
8
senderKey: privateKey,
9
network: "testnet",
10
});

Different transaction types

In Stacks.js, we can create transactions for different purposes:

  • STX token transfers
  • Smart contract calls
  • Smart contract deployments