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
import { openSTXTransfer } from '@stacks/connect';import { AnchorMode } from '@stacks/transactions';openSTXTransfer({network: 'testnet',recipient: 'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK', // which address you are sending toamount: 10000, // tokens, denominated in micro-STXanchorMode: AnchorMode.Any,onFinish: response => console.log(response.txid),onCancel: () => console.log('User canceled'),});
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.
import { makeSTXTokenTransfer } from '@stacks/transactions';const privateKey = randomPrivateKey(); // see "Private Keys & Wallets" pageconst tx = await makeSTXTokenTransfer({recipient: 'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK', // which address you are sending toamount: 10000, // tokens, denominated in micro-STXanchorMode: 'any',senderKey: privateKey,});
Different transaction types
In Stacks.js, we can create transactions for different purposes:
- STX token transfers
- Smart contract calls
- Smart contract deployments