This guide will walk you through creating, signing, and broadcasting your first Hive blockchain transaction using hive-tx.
Prerequisites
Before you begin, make sure you have:
Node.js 20 or higher installed
hive-tx package installed (see installation )
A Hive account with a private key (posting key for voting, active key for transfers)
Never share your private keys or commit them to version control. Use environment variables or secure key management solutions in production.
Your first transaction
Import the required modules
Start by importing the Transaction and PrivateKey classes from hive-tx: import { Transaction , PrivateKey } from 'hive-tx'
Create a transaction and add an operation
Create a new transaction instance and add a vote operation: const tx = new Transaction ()
await tx . addOperation ( 'vote' , {
voter: 'your-username' ,
author: 'post-author' ,
permlink: 'post-permlink' ,
weight: 10000 // 100% upvote
})
The addOperation method is asynchronous because it fetches blockchain data to set up the transaction properly.
Sign the transaction
Sign the transaction with your private posting key: const key = PrivateKey . from ( '5JYourPrivatePostingKeyHere...' )
tx . sign ( key )
For voting operations, you need your posting key . For transfers and financial operations, you’ll need your active key .
Broadcast to the network
Broadcast the signed transaction to the Hive blockchain: const result = await tx . broadcast ()
console . log ( 'Transaction ID:' , result . tx_id )
console . log ( 'Status:' , result . status )
{
"tx_id" : "a1b2c3d4e5f6..." ,
"status" : "unknown"
}
The status will be "unknown" by default. Pass true to broadcast() to wait for confirmation: const result = await tx . broadcast ( true )
// status will be "within_irreversible_block" when confirmed
Complete example
Here’s the complete code in one place:
import { Transaction , PrivateKey } from 'hive-tx'
async function voteOnPost () {
// Create transaction
const tx = new Transaction ()
await tx . addOperation ( 'vote' , {
voter: 'your-username' ,
author: 'post-author' ,
permlink: 'post-permlink' ,
weight: 10000
})
// Sign transaction
const key = PrivateKey . from ( '5JYourPrivatePostingKeyHere...' )
tx . sign ( key )
// Broadcast
const result = await tx . broadcast ()
console . log ( 'Vote successful!' , result . tx_id )
}
voteOnPost (). catch ( console . error )
Transfer example
Here’s how to send HIVE or HBD tokens:
import { Transaction , PrivateKey } from 'hive-tx'
async function sendHive () {
const tx = new Transaction ()
await tx . addOperation ( 'transfer' , {
from: 'sender-username' ,
to: 'receiver-username' ,
amount: '1.000 HIVE' ,
memo: 'Thanks for your help!'
})
// Use active key for transfers
const key = PrivateKey . from ( '5JYourPrivateActiveKeyHere...' )
tx . sign ( key )
const result = await tx . broadcast ()
console . log ( 'Transfer successful!' , result . tx_id )
}
sendHive (). catch ( console . error )
Amount must be formatted as a string with 3 decimal places: "1.000 HIVE" or "5.123 HBD"
Making API calls
Query blockchain data without broadcasting transactions:
import { callRPC } from 'hive-tx'
// Get account information
const accounts = await callRPC ( 'condenser_api.get_accounts' , [[ 'username' ]])
console . log ( 'Account:' , accounts [ 0 ])
// Get blockchain properties
const props = await callRPC ( 'condenser_api.get_dynamic_global_properties' )
console . log ( 'Head block:' , props . head_block_number )
// Get content (posts/comments)
const content = await callRPC ( 'condenser_api.get_content' , [ 'author' , 'permlink' ])
console . log ( 'Post title:' , content . title )
Error handling
Always wrap your code in try-catch blocks to handle errors:
import { Transaction , PrivateKey , RPCError } from 'hive-tx'
async function safeVote () {
try {
const tx = new Transaction ()
await tx . addOperation ( 'vote' , {
voter: 'your-username' ,
author: 'post-author' ,
permlink: 'post-permlink' ,
weight: 10000
})
const key = PrivateKey . from ( 'your-private-key' )
tx . sign ( key )
const result = await tx . broadcast ()
console . log ( 'Success:' , result . tx_id )
} catch ( error ) {
if ( error instanceof RPCError ) {
console . error ( 'RPC Error:' , error . message )
console . error ( 'Error code:' , error . code )
} else {
console . error ( 'Error:' , error . message )
}
}
}
Next steps
Now that you’ve created your first transaction, explore more features:
Transactions Learn about transaction structure and lifecycle
Operations Explore all available Hive operations
Key Management Generate and manage keys securely
Examples See more real-world examples