# Transfer HBAR

## Prerequisites <a href="#pre-requisites" id="pre-requisites"></a>

* Completed the Introduction step.
* Completed the Environment Setup step.
* Completed the Created an Account step.

## Step 1 : Imports

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

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

```

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

These lines retrieve environment variables such as `MY_ACCOUNT_ID`, `MY_PRIVATE_KEY`, and `OTHER_ACCOUNT_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 otherAccountId = process.env.OTHER_ACCOUNT_ID;
```

## Step 3 : **Main Function**

* 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 `TransferTransaction` object to transfer HBars between accounts.
* The transaction subtracts 10 HBars from the operator account (`myAccountId`) and adds 10 HBars to the other account (`otherAccountId`).
* The transaction is executed, and the resulting transaction ID (`txId`) is obtained.
* The receipt of the transaction is retrieved, and the transaction consensus status is obtained from the receipt.
* Queries are created to retrieve the account balances of both the operator account and the other account.
* The queries are executed, and the account balances are 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

    const transaction = new TransferTransaction() // Create a transfer transaction
    .addHbarTransfer(myAccountId, new Hbar(-10)) // Subtract 10 HBars from the operator account
    .addHbarTransfer(otherAccountId, new Hbar(10)); // Add 10 HBars to the other account

    console.log(`Doing transfer from ${myAccountId} to ${otherAccountId}`);

    const txId = await transaction.execute(client); // Execute the transfer transaction
    const receipt = await txId.getReceipt(client); // Get the receipt of the transaction
    const transactionStatus = receipt.status; // Get the transaction consensus status

    console.log("The transaction consensus status is " + transactionStatus);

    const queryMine = new AccountBalanceQuery().setAccountId(myAccountId); // Create a query for operator account balance
    const queryOther = new AccountBalanceQuery().setAccountId(otherAccountId); // Create a query for other account balance

    const accountBalanceMine = await queryMine.execute(client); // Execute the query for operator account balance
    const accountBalanceOther = await queryOther.execute(client); // Execute the query for other account balance

    console.log(`My account balance ${accountBalanceMine.hbars} HBar, other account balance ${accountBalanceOther.hbars}`);
}

```

## Step 4: Execution

This line calls the `main()` function to start the execution of the script.

```javascript
main();
```


---

# Agent Instructions: 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:

```
GET https://abc.bharatblockchain.io/hedera/tutorials/accounts/transfer-hbar.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
