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 to
  amount: 10000, // tokens, denominated in micro-STX
  anchorMode: 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" page

const tx = await makeSTXTokenTransfer({
  recipient: 'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK', // which address you are sending to
  amount: 10000, // tokens, denominated in micro-STX
  anchorMode: '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