Create a Post
To create a new post, use thecomment operation with an empty parent_author.
import { Transaction, PrivateKey } from 'hive-tx'
async function createPost() {
const tx = new Transaction()
await tx.addOperation('comment', {
parent_author: '', // Empty for root posts
parent_permlink: 'technology', // Category/tag
author: 'your-username',
permlink: 'my-first-post-123', // Unique permlink for your post
title: 'My First Post on Hive',
body: 'This is the content of my first post. It supports **markdown** formatting!',
json_metadata: JSON.stringify({
tags: ['technology', 'hive', 'blockchain'],
app: 'my-app/1.0.0',
format: 'markdown'
})
})
const key = PrivateKey.from('your-posting-key')
tx.sign(key)
const result = await tx.broadcast()
console.log('Post created!', result.result.tx_id)
console.log('View at: https://peakd.com/@your-username/my-first-post-123')
}
CommentOperation Type
interface CommentOperation {
parent_author: string // Empty '' for posts, author name for comments
parent_permlink: string // Category for posts, post permlink for comments
author: string // Your account name
permlink: string // Unique identifier for this post/comment
title: string // Post title (empty for comments)
body: string // Post/comment content (supports markdown)
json_metadata: string // JSON string with metadata
}
Create a Comment (Reply to Post)
To reply to a post, set theparent_author and parent_permlink to the post you’re replying to.
import { Transaction, PrivateKey } from 'hive-tx'
async function createComment() {
const tx = new Transaction()
await tx.addOperation('comment', {
parent_author: 'original-author', // Author of the post you're replying to
parent_permlink: 'original-permlink', // Permlink of the post you're replying to
author: 'your-username',
permlink: 're-original-permlink-20260304t120000', // Unique permlink for your comment
title: '', // Comments don't have titles
body: 'Great post! Thanks for sharing this information.',
json_metadata: JSON.stringify({
tags: ['technology'],
app: 'my-app/1.0.0'
})
})
const key = PrivateKey.from('your-posting-key')
tx.sign(key)
const result = await tx.broadcast()
console.log('Comment posted!', result.result.tx_id)
}
Reply to a Comment
Replying to a comment is the same as replying to a post - just use the comment’s author and permlink.import { Transaction, PrivateKey } from 'hive-tx'
async function replyToComment() {
const tx = new Transaction()
await tx.addOperation('comment', {
parent_author: 'comment-author', // Author of the comment
parent_permlink: 'comment-permlink', // Permlink of the comment
author: 'your-username',
permlink: 're-comment-permlink-20260304t120500',
title: '',
body: 'Thanks for your comment! I agree with your point.',
json_metadata: JSON.stringify({
app: 'my-app/1.0.0'
})
})
const key = PrivateKey.from('your-posting-key')
tx.sign(key)
const result = await tx.broadcast()
console.log('Reply posted!', result.result.tx_id)
}
Update a Post
To update an existing post, use the samecomment operation with the same permlink.
import { Transaction, PrivateKey } from 'hive-tx'
async function updatePost() {
const tx = new Transaction()
await tx.addOperation('comment', {
parent_author: '',
parent_permlink: 'technology',
author: 'your-username',
permlink: 'my-first-post-123', // Same permlink as original post
title: 'My Updated Post Title',
body: 'This is the **updated** content of my post with additional information.',
json_metadata: JSON.stringify({
tags: ['technology', 'hive', 'blockchain', 'updated'],
app: 'my-app/1.0.0',
format: 'markdown',
updated: new Date().toISOString()
})
})
const key = PrivateKey.from('your-posting-key')
tx.sign(key)
const result = await tx.broadcast()
console.log('Post updated!', result.result.tx_id)
}
Update a Comment
Updating a comment works the same way as updating a post.import { Transaction, PrivateKey } from 'hive-tx'
async function updateComment() {
const tx = new Transaction()
await tx.addOperation('comment', {
parent_author: 'original-author',
parent_permlink: 'original-permlink',
author: 'your-username',
permlink: 're-original-permlink-20260304t120000', // Same permlink as original comment
title: '',
body: 'Updated comment: Great post! Thanks for sharing. **Edit**: Added more thoughts.',
json_metadata: JSON.stringify({
tags: ['technology'],
app: 'my-app/1.0.0',
edited: true
})
})
const key = PrivateKey.from('your-posting-key')
tx.sign(key)
const result = await tx.broadcast()
console.log('Comment updated!', result.result.tx_id)
}
Delete a Comment
You can only delete comments that have no replies and no votes.import { Transaction, PrivateKey } from 'hive-tx'
async function deleteComment() {
const tx = new Transaction()
await tx.addOperation('delete_comment', {
author: 'your-username',
permlink: 're-original-permlink-20260304t120000'
})
const key = PrivateKey.from('your-posting-key')
tx.sign(key)
const result = await tx.broadcast()
console.log('Comment deleted!', result.result.tx_id)
}
DeleteCommentOperation Type
interface DeleteCommentOperation {
author: string // Your account name
permlink: string // Permlink of the comment to delete
}
You can only delete comments that have:
- No replies
- No votes
- Not been paid out
Set Comment Options with Beneficiaries
Usecomment_options to configure payout settings and add beneficiaries who will receive a portion of rewards.
import { Transaction, PrivateKey } from 'hive-tx'
async function createPostWithBeneficiaries() {
const tx = new Transaction()
// First, create the post
await tx.addOperation('comment', {
parent_author: '',
parent_permlink: 'technology',
author: 'your-username',
permlink: 'post-with-beneficiaries',
title: 'Post with Reward Sharing',
body: 'This post shares rewards with other users.',
json_metadata: JSON.stringify({
tags: ['technology', 'hive'],
app: 'my-app/1.0.0'
})
})
// Then, set comment options
await tx.addOperation('comment_options', {
author: 'your-username',
permlink: 'post-with-beneficiaries',
max_accepted_payout: '1000.000 HBD', // Maximum payout
percent_hbd: 10000, // 10000 = 100% HBD, 0 = 100% HP
allow_votes: true,
allow_curation_rewards: true,
extensions: [
[
0,
{
beneficiaries: [
{ account: 'alice', weight: 500 }, // 5% to alice
{ account: 'bob', weight: 1000 }, // 10% to bob
{ account: 'charity', weight: 500 } // 5% to charity
// Total: 20% to beneficiaries, 80% to author
]
}
]
]
})
const key = PrivateKey.from('your-posting-key')
tx.sign(key)
const result = await tx.broadcast()
console.log('Post with beneficiaries created!', result.result.tx_id)
}
CommentOptionsOperation Type
interface CommentOptionsOperation {
author: string
permlink: string
max_accepted_payout: string // Maximum payout (e.g., '1000.000 HBD')
percent_hbd: number // 10000 = 100% HBD, 0 = 100% HP
allow_votes: boolean
allow_curation_rewards: boolean
extensions: Array<Array<any>> // Beneficiaries extension
}
interface Beneficiary {
account: string // Beneficiary account name
weight: number // Weight in basis points (500 = 5%, 1000 = 10%)
}
Beneficiary Weight Calculation
Beneficiary weights are in basis points (1/100th of a percent):100= 1%500= 5%1000= 10%2500= 25%5000= 50%
Decline Payout
To decline payout for a post, setmax_accepted_payout to '0.000 HBD'.
import { Transaction, PrivateKey } from 'hive-tx'
async function createPostWithoutRewards() {
const tx = new Transaction()
await tx.addOperation('comment', {
parent_author: '',
parent_permlink: 'announcement',
author: 'your-username',
permlink: 'important-announcement',
title: 'Important Announcement',
body: 'This is a non-monetized announcement.',
json_metadata: JSON.stringify({
tags: ['announcement'],
app: 'my-app/1.0.0'
})
})
await tx.addOperation('comment_options', {
author: 'your-username',
permlink: 'important-announcement',
max_accepted_payout: '0.000 HBD', // Decline payout
percent_hbd: 10000,
allow_votes: true,
allow_curation_rewards: true,
extensions: []
})
const key = PrivateKey.from('your-posting-key')
tx.sign(key)
const result = await tx.broadcast()
console.log('Post created without rewards!', result.result.tx_id)
}
Permlink Generation Helper
Generate unique permlinks for your posts and comments.function generatePermlink(title: string): string {
// Convert title to permlink format
const permlink = title
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-') // Replace non-alphanumeric with hyphens
.replace(/^-+|-+$/g, '') // Remove leading/trailing hyphens
.substring(0, 255) // Limit length
// Add timestamp for uniqueness
const timestamp = Date.now()
return `${permlink}-${timestamp}`
}
// Usage:
const permlink = generatePermlink('My Awesome Post Title')
// Result: 'my-awesome-post-title-1709557200000'
Important Notes
- Use posting key to sign comment operations
- Permlinks must be unique per author
- Posts can be edited, but edit history is visible on the blockchain
- You cannot delete posts or comments that have been voted on or have replies
- Beneficiaries can only be set when creating a post, not when updating
- Beneficiary total weight cannot exceed 10000 (100%)
Common permlink format for comments:
re-{parent-permlink}-{timestamp} ensures uniqueness.