Create a Topic

Introduction to Creating a Topic

This tutorial will walk you through a TopicCreateTransaction to create a new consensus topic and a TopicMessageSubmitTransaction to publish your first message using the Hedera Consensus Service (HCS). The service provides developers a decentralized tool for logging, ordering, and timestamping messages or transactions on the Hedera network.

What you will accomplish

By the end of this tutorial, you will be able to:

  • Create new consensus topics using HCS.

  • Publish your first message using HCS.

  • Query the transaction via Mirror Node API.

  • View your transaction on a Mirror Node Explorer.


Prerequisites

Before you begin, you should have completed the following tutorials:


Step 1: Navigate to the hcs example in the project directory

From the root directory of the hedera-future-world-language project CD (change directories) to the topic create transaction example.

cd hcs

If you completed a previous example in the series you can use to go back to the root directory and cd into this example.

cd ../hcs

If you want to get back to the root directory, you can CD out from any directory with this command

cd ../

You can follow along through the code walkthrough or skip ahead to execute the program here.


Step 2: Guided Code Walkthrough

Open the HCS create a topic script (e.g., /hcs/script-hcs-topic.js) in a code editor llike VS Code, IntelliJ, or a Gitpod instance.. The imports at the top include modules for interacting with the Hedera network via the SDK. The @hashgraph/sdk enables account management and transactions like creating a topic while the dotenv package loads environment variables from the .env file, such as the operator account ID, private key, and name variables.

script-hcs-topic.js
import {
    Client,
    PrivateKey,
    AccountId,
    TokenCreateTransaction,
    TokenType,
} from '@hashgraph/sdk';
import dotenv from 'dotenv';
import {
    createLogger,
} from '../util/util.js';

const logger = await createLogger({
    scriptId: 'htsFt',
    scriptCategory: 'task',
});
let client;

async function scriptHtsFungibleToken() {
    logger.logStart('Hello Future World - HTS Fungible Token - start');

    // Read in environment variables from `.env` file in parent directory
    dotenv.config({ path: '../.env' });
    logger.log('Read .env file');

    // Initialise the operator account
    const operatorIdStr = process.env.OPERATOR_ACCOUNT_ID;
    const operatorKeyStr = process.env.OPERATOR_ACCOUNT_PRIVATE_KEY;
    if (!operatorIdStr || !operatorKeyStr) {
        throw new Error('Must set OPERATOR_ACCOUNT_ID and OPERATOR_ACCOUNT_PRIVATE_KEY environment variables');
    }
    const operatorId = AccountId.fromString(operatorIdStr);
    const operatorKey = PrivateKey.fromStringECDSA(operatorKeyStr);
    client = Client.forTestnet().setOperator(operatorId, operatorKey);
    logger.log('Using account:', operatorIdStr);
}

Create a Hedera Testnet Client

To set up your Hedera Testnet client, create the client and configure the operator using your Testnet account ID and private key. The operator account covers transaction and query fees in HBAR, with all transactions requiring a signature from the operator's private key for authorization.

script-hcs-topic.js
// The client operator ID and key is the account that will be automatically set to pay for the transaction fees for each transaction
client = Client.forTestnet().setOperator(operatorId, operatorKey);

//Set the default maximum transaction fee (in Hbar)
client.setDefaultMaxTransactionFee(new Hbar(100));

//Set the maximum payment for queries (in Hbar)
client.setDefaultMaxQueryPayment(new Hbar(50));

To avoid encountering the INSUFFICIENT_TX_FEE error while executing transactions, you can also specify the maximum transaction fee limit through the .setDefaultMaxTransactionFee() method and the maximum query payment through the .setDefaultMaxQueryPayment() method to control costs, ensuring your client operates within your desired financial limits on the Hedera Testnet.

🚨 How to resolve the INSUFFICIENT_TX_FEE error

To resolve this error, you must adjust the max transaction fee to a higher value suitable for your needs.

Here is a simple example addition to your code:

Copy

const maxTransactionFee = new Hbar(XX); // replace XX with desired fee in Hbar

In this example, you can set maxTransactionFee to any value greater than 5 HBAR (or 500,000,000 tinybars) to avoid the "INSUFFICIENT_TX_FEE" error for transactions greater than 5 HBAR. Please replace XX with the desired value.

To implement this new max transaction fee, you use the setDefaultMaxTransactionFee() method as shown below:

Copy

client.setDefaultMaxTransactionFee(maxTransactionFee);

Create a Topic Create Transaction

To create your first topic, you will use the TopicCreateTransaction(), set its properties, and submit it to the Hedera network. In this guide, you will create a public topic by not setting any properties on the topic. This means that anyone can send messages to your topic. Refer to the transaction and query fees table for the full list of base transaction fees.

After the transaction is completed, you'll need to extract the topicID from the transaction receipt, as it's required for submitting messages to the topic. This step has already been completed for you, no further modification is necessary.

script-hcs-topic.js
// Create a new HCS topic
const topicCreateTx = await new TopicCreateTransaction()
    .setTopicMemo(`Hello Future World topic - ${logger.version}`)
    // Freeze the transaction to prepare for signing
    .freezeWith(client);

// Get the transaction ID of the transaction.
// The SDK automatically generates and assigns a transaction ID when the transaction is created
const topicCreateTxId = topicCreateTx.transactionId;
logger.log("The topic create transaction ID: ", topicCreateTxId.toString());

// Sign the transaction with the account key that will be paying for this transaction
const topicCreateTxSigned = await topicCreateTx.sign(operatorKey);

// Submit the transaction to the Hedera Testnet
const topicCreateTxSubmitted = await topicCreateTxSigned.execute(client);

// Get the transaction receipt
const topicCreateTxReceipt = await topicCreateTxSubmitted.getReceipt(client);

// Get the topic ID
const topicId = topicCreateTxReceipt.topicId;
logger.log('topicId:', topicId.toString());

Publish a Message to the New Topic

Now you are ready to submit your first message to the topic. You will use the TopicMessageSubmitTransaction() class to submit your first message to the topic. This involves setting the topicId and the message content, signing the transaction, and submitting it to the Hedera network. Messages can be any data up to 1024 bytes, and each message costs $0.0001 to send. This means you can send 10,000 messages for just $1 on the Hedera network. For the message content, use a string in the format "Hello HCS!".

script-hcs-topic.js
// Publish a message to the HCS topic
const topicMsgSubmitTx = await new TopicMessageSubmitTransaction()
    //Set the transaction memo with the hello future world ID
    .setTransactionMemo(`Hello Future World topic message - ${logger.version}`)
    .setTopicId(topicId)
    //Set the topic message contents
    .setMessage('Hello HCS!')
    // Freeze the transaction to prepare for signing
    .freezeWith(client);

// Get the transaction ID of the transaction. The SDK automatically generates and assigns a transaction ID when the transaction is created
const topicMsgSubmitTxId = topicMsgSubmitTx.transactionId;
logger.log('The message submit create transaction ID: ',
    topicMsgSubmitTxId.toString());

// Sign the transaction with the account key that will be paying for this transaction
const topicMsgSubmitTxSigned = await topicMsgSubmitTx.sign(operatorKey);

// Submit the transaction to the Hedera Testnet
const topicMsgSubmitTxSubmitted = await topicMsgSubmitTxSigned.execute(client);

// Get the transaction receipt
const topicMsgSubmitTxReceipt = await topicMsgSubmitTxSubmitted.getReceipt(client);

// Get the topic message sequence number
const topicMsgSeqNum = topicMsgSubmitTxReceipt.topicSequenceNumber;
logger.log('topicMsgSeqNum:', topicMsgSeqNum.toString());

The total cost to create a topic and send a message to it is approximately $0.0101, making it an affordable way to interact with the Hedera network.

Query the Topic Data from the Mirror Node API

To query the specified topic ID and verify the messages within the topic, you will want to use the Mirror Node API with the path /api/v1/topics/{topicId}/messages for this task.

  • Specify topicId within the URL path

  • Specify base64 as the encoding query parameter

  • Specify 5 as the limit query parameter

  • Specify asc as the order query parameter

  • Specify 1 as the sequencenumber query parameter

The constructed topicVerifyMirrorNodeApiUrl should look like this:

script-hcs-topic.js
const topicVerifyMirrorNodeApiUrl =
    `https://testnet.mirrornode.hedera.com/api/v1/topics/${topicId.toString()}/
    messages?encoding=base64&limit=5&order=asc&sequencenumber=1`;
Learn more about Mirror Node APIs

You can explore the Mirror Node APIs interactively via its Swagger page: Hedera Testnet Mirror Node REST API.

You can perform the same Mirror Node API query as topicVerifyMirrorNodeApiUrl above. This is what the relevant part of the Swagger page would look like when doing so:

➑ You can learn more about the Mirror Nodes via its documentation: REST API.


Step 3: Run and Verify Topic on HashScan

In the terminal, cd into the hcs directory and run the topic create transaction script:

node script-hcs-topic.js

Sample output:

🏁 Hello Future World - HCS Topic - start
Using account: 0.0.1455
Using operatorKey: 1a83c7c0fe90a1d24b8f5bcd3b074aecad834d7a9fbe130ea133eb4e01775300
🟣 Creating new HCS topic
The topic create transaction ID: 0.0.1455@1732734923.966673221
Topic ID: 0.0.5181441
🟣 Publish message to HCS topic
The topic message submit transaction ID: 0.0.1455@1732734922.264842548
Topic Message Sequence Number: 1
🟣 View the topic on HashScan
Topic Hashscan URL: https://hashscan.io/testnet/topic/0.0.5181441
🟣 Get topic data from the Hedera Mirror Node
The token Hedera Mirror Node API URL: https://testnet.mirrornode.hedera.com/api/v1/topics/0.0.5181441/messages?encoding=base64&limit=5&order=asc&sequencenumber=1
Messages retrieved from this topic:
#1: Hello HCS!
πŸŽ‰ Hello Future World - HCS Topic - complete

To verify that both the TopicCreateTransaction and TopicMessageSubmitTransaction have been successful, check the transaction details on HashScan. Open View the topic on HashScan URL from the console output in your browser and check that:

  • The topic exists, and its topic ID matches topicId output by the script. (1)

  • There is one entry in the topic, and its message is Hello HCS - followed by your name/ nickname. (2)


Code Check βœ…


Complete

Congratulations, you have completed the Create a Topic tutorial in the Getting Started series for the Web2 Developers learning path! πŸŽ‰πŸŽ‰πŸŽ‰!

You learned how to:


Next Steps

Continue building on Hedera with another tutorial in the series to explore more Hedera services.

Have questions? Join the Hedera Discord and post them in the developer-general channel or ask on Stack Overflow.

Last updated