Learn how to create a new topic and submit your first message on Hedera testnet using the JavaScript, Java, Go SDK, or Python. A topic on the Hedera Consensus Service (HCS) is like a public channel: anyone who knows the topic ID can publish timestamped messages, and anyone can subscribe to the stream from a mirror node.
A Hedera testnet operator account ID and DER-encoded private key (from the Quickstart).
A small amount of testnet HBAR (ℏ) to cover the fees
Topic creation: ≈ $0.01
Each message: < $0.001
NoteYou can always check the ”✅ Code Check” section at the bottom of each page to view the entire code if you run into issues. You can also post your issue to the respective SDK channel in our Discord communityhere.
Create a createTopicDemo.js file and add the following imports:
Copy
Ask AI
import { Client, TopicCreateTransaction, TopicMessageSubmitTransaction,} from "@hashgraph/sdk";
Add the Java SDK depenency to your Maven project’s pom.xml and create your source file:Create a new Maven project and name it HederaExamples. Add the following dependencies to your pom.xml file:
Or for Gradle projects using the Groovy DSL, add these dependencies to your build.gradle file and install the dependencies using ./gradlew build
Copy
Ask AI
plugins { id 'java' id 'application'}repositories { mavenCentral()}dependencies { implementation 'com.hedera.hashgraph:sdk:2.60.0' implementation 'com.google.code.gson:gson:2.10.1' implementation 'io.grpc:grpc-netty-shaded:1.61.0'}application { mainClass = 'CreateTopicDemo' // or 'com.example.CreateTopicDemo' if it's in a package}
Create a CreateTopicDemo.java class in src/main/java/ with the following imports:
Copy
Ask AI
import com.hedera.hashgraph.sdk.*;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;import java.net.URI;import com.google.gson.Gson;import com.google.gson.JsonObject;import com.google.gson.JsonArray;public class CreateTopicDemo { public static void main(String[] args) throws Exception { // Your topic creation code will go here }}
Create a new file create_topic_demo.go and import the following packages to your file:
In your project’s root directory, initialize modules and pull in the Go SDK:
-module
Copy
Ask AI
go mod init create_topic_demogo get github.com/hiero-ledger/hiero-sdk-go/v2@latestgo mod tidy
Before you start: Ensure you have Python 3.10+ installed on your machine. Run this command to verify.
Copy
Ask AI
python --version
If the python --version command is not found or shows a version lower than 3.10, install or upgrade Python from Python Install.Note: On some systems, you may need to use python3 instead of python for initial setup commands. If python --version doesn’t work, try python3 --version and use python3 for the virtual environment creation. After activating the virtual environment, always use python for all commands.Open your terminal and create a working directory for your Hedera project. Then navigate into the new directory:
Copy
Ask AI
mkdir hedera-examples && cd hedera-examples
Verify Python and pip: Ensure you have Python 3.10+ and pip installed on your machine. Run these commands to check:
Copy
Ask AI
python --version
Copy
Ask AI
python -m pip --version
Create a virtual environment to isolate your project dependencies (Python best practice):
Copy
Ask AI
python -m venv .venv
Activate the virtual environment to use the isolated Python installation:
Copy
Ask AI
source .venv/bin/activate
Upgrade pip to ensure you have the latest package installer (recommended):
Set your operator credentials as environment variables. Your OPERATOR_ID is your testnet account ID. Your OPERATOR_KEY is your testnet account’s corresponding ECDSA private key.
Load your operator credentials from environment variables and configure your Hedera testnet client. The client manages your connection to the Hedera test network and uses your operator account to sign transactions and pay transaction fees.
Copy
Ask AI
// Load your operator credentialsconst operatorId = process.env.OPERATOR_ID;const operatorKey = process.env.OPERATOR_KEY;// Initialize your testnet client and set operatorconst client = Client.forTestnet().setOperator(operatorId, operatorKey);
TopicCreateTransaction builds and sends the transaction to register a new HCS topic with the provided memo, and once it reaches consensus you retrieve the transaction receipt to extract the new topicId.
Messages on HCS are consensus-timestamped and immutable. Once published, they become part of the permanent record that anyone can verify and subscribe to in real-time.
Copy
Ask AI
// Build and send the transactionconst txResponse = await new TopicCreateTransaction() .setTopicMemo("My first HCS topic") // optional description .execute(client);const receipt = await txResponse.getReceipt(client);const topicId = receipt.topicId;console.log(`\nTopic created: ${topicId.toString()}`);
TopicMessageSubmitTransaction constructs and sends a transaction that submits your payload (string, bytes, or Uint8Array) as a message to a specified HCS topic. Once it reaches consensus, the message becomes part of that topic’s immutable record.
Copy
Ask AI
// Build & execute the message submission transactionconst message = "Hello, Hedera!";const messageTransaction = new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage(message);await messageTransaction.execute(client);console.log(`\nMessage submitted: ${message}\n`);
NoteMessages can be up to 1 KiB each. Larger payloads must be chunked automatically by the SDK or split manually
Use the Mirror Node REST API to verify your message was published to the topic. Mirror nodes provide free access to network data without transaction fees. Mirror nodes stream every consensus-timestamped message in order, letting your app react in real time.API endpoint:
Copy
Ask AI
/api/v1/topics/{topicId}/messages
Replace the placeholder:
{topicId} - Your topic ID from the creation transaction
Why this endpoint?This endpoint retrieves all messages published to a specific topic, ordered by consensus timestamp. It returns detailed information including message content, timestamp, and sequence number, making it ideal for verifying your message was published successfully.
| Error message | Likely cause | Fix | | ---------------------------- |
---------------------------------------------- |
-------------------------------------------------------------- | |
INSUFFICIENT_PAYER_BALANCE | Not enough HBAR for topic creation fee | Top up
your operator account on the testnet faucet | | INVALID_SIGNATURE | Operator
key doesn’t match operator account | Verify OPERATOR_KEY matches your
OPERATOR_ID | | INVALID_ACCOUNT_ID | Malformed account ID in environment
variables | Verify OPERATOR_ID format is 0.0.1234 | | INVALID_PRIVATE_KEY |
Malformed private key in environment variables | Verify OPERATOR_KEY is a
valid DER-encoded private key string | | INVALID_TOPIC_ID | Topic ID not
found or malformed | Ensure topic was created successfully and ID is correct |
| No messages found yet | Mirror Node sync delay | Wait a few more seconds
and try again - this is normal |