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
1import { openSTXTransfer } from '@stacks/connect';23openSTXTransfer({4network: 'testnet',56recipient: 'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK', // which address you are sending to7amount: 10000, // tokens, denominated in micro-STX89onFinish: response => console.log(response.txid),10onCancel: () => 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.
1import { makeSTXTokenTransfer } from '@stacks/transactions';23const privateKey = randomPrivateKey(); // see "Private Keys & Wallets" page45const tx = await makeSTXTokenTransfer({6recipient: 'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK', // which address you are sending to7amount: 10000, // tokens, denominated in micro-STX8senderKey: privateKey,9network: "testnet",10});
Different transaction types
In Stacks.js, we can create transactions for different purposes:
- STX token transfers
- Smart contract calls
- Smart contract deployments