> For the complete documentation index, see [llms.txt](https://abc.bharatblockchain.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://abc.bharatblockchain.io/hedera/tutorials/consensus-service/submit-a-message.md).

# Submit a message

## Step 1 : **Imports**

* The script imports necessary modules from the Hedera JavaScript SDK: `PrivateKey`, `TopicMessageSubmitTransaction`, and `Client`.
* It also imports the `dotenv` module to load environment variables from a `.env` file located in the `Consensus_Service` directory.

```javascript
const {
    PrivateKey,
    TopicMessageSubmitTransaction,
    Client
} = require("@hashgraph/sdk");
require('dotenv').config({ path: 'Consensus_Service/.env' });
```

## Step 2 : **Environment Variables Retrieval**

These lines retrieve environment variables such as `MY_ACCOUNT_ID`, `MY_PRIVATE_KEY`, and `TOPIC_ID` from the `.env` file using `process.env`.

```javascript
const myAccountId = process.env.MY_ACCOUNT_ID;
const myPrivateKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);
const topicId = process.env.TOPIC_ID;
```

## Step 3 : **Validation**

This block of code ensures that the required environment variables (`MY_ACCOUNT_ID` and `MY_PRIVATE_KEY`) are present. If not, it throws an error.

```javascript
if (myAccountId == null ||
    myPrivateKey == null ) {
    throw new Error("Environment variables myAccountId and myPrivateKey must be present");
}
```

## Step 4 : **Main Function**

* This is the main function named `main()`.
* It creates a client instance for the Hedera testnet and sets the operator account using the user's account ID and private key.
* It creates a new `TopicMessageSubmitTransaction` object to submit a message to a topic.
* The topic ID and message content are set using the retrieved environment variables.
* The transaction is executed using the `execute()` method, and the resulting transaction response (`sendResponse`) is obtained.
* The receipt of the transaction is retrieved using `getReceipt()`.
* The status of the transaction is extracted from the receipt and logged to the console.

```javascript
async function main() {
    const client = Client.forTestnet(); // Create a client for the Hedera testnet
    client.setOperator(myAccountId, myPrivateKey); // Set the operator account for the client

    // Send one message to the specified topic
    let sendResponse = await new TopicMessageSubmitTransaction({
        topicId: topicId, // Set the topic ID
        message: "This is a very first message to my new topic!", // Set the message content
    }).execute(client);

    // Get the receipt of the transaction
    const getReceipt = await sendResponse.getReceipt(client);

    // Get the status of the transaction
    const transactionStatus = getReceipt.status;
    console.log("The message transaction status: " + transactionStatus);

    process.exit(); // Exit the process
}

main();

```

This script submits a message to a consensus topic on the Hedera network and logs the transaction status to the console.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://abc.bharatblockchain.io/hedera/tutorials/consensus-service/submit-a-message.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
