broadcastTransaction

Broadcasts a serialized transaction to a Stacks node, which will validate and forward it to the network.


Usage

import {
  makeSTXTokenTransfer,
  broadcastTransaction,
} from '@stacks/transactions';

const transaction = await makeSTXTokenTransfer({
  recipient: 'ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5',
  amount: 1000000n,
  senderKey: 'b244296d5907de9864c0b0d51f98a13c52890be0404e83f273144004b81874603',
  network: 'testnet',
});

const result = await broadcastTransaction({
  transaction,
  network: 'testnet',
});

if ('txid' in result) {
  console.log('Success:', result.txid);
} else {
  console.log('Rejected:', result.reason);
}

Notes

  • Returns a TxBroadcastResult which is either a success (contains txid) or a rejection (contains reason).

  • You can provide an optional attachment for contract deploys or other uses.

  • The network is inferred from the transaction if not specified.

Reference Linkarrow-up-right


Signature


Returns

Promise<TxBroadcastResult>


Parameters

opts.transaction (required)

  • Type: StacksTransactionWire

The signed transaction to broadcast.

opts.attachment (optional)

  • Type: Uint8Array | string

An optional attachment in bytes or as a hex string.

opts.network (optional)

  • Type: StacksNetworkName | StacksNetwork

The network to broadcast to. Inferred from the transaction if not provided.

opts.client (optional)

  • Type: NetworkClient

Custom client configuration (base URL, fetch function) for the API call.

Last updated

Was this helpful?