> 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/scheduled_tx/create-scheduled-transaction.md).

# Create Scheduled Transaction

## Summary

This script sets up a scheduled transaction to transfer cryptocurrency between two accounts on the Hedera network. It demonstrates the process of creating a scheduled transaction, setting up the scheduled transaction parameters, executing the schedule transaction, and obtaining information about the created schedule.

## Step 1: Imports and Environment Setup

* The script imports necessary modules from the Hedera JavaScript SDK (`@hashgraph/sdk`): `TransferTransaction`, `Client`, `ScheduleCreateTransaction`, `PrivateKey`, `Hbar`, and `ScheduleInfoQuery`.
* It also imports the `dotenv` module to load environment variables from a `.env` file located in the `Scheduled_TX` directory.

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

## Step 2: Environment Variables Retrieval and Validation

* The code retrieves necessary environment variables such as account IDs and private keys from the `.env` file.
* It validates that the required environment variables (`MY_ACCOUNT_ID` and `MY_PRIVATE_KEY`) are present. If not, it throws an error.

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

const otherAccountId = process.env.OTHER_ACCOUNT_ID;
const otherAccountId2 = process.env.OTHER_ACCOUNT_ID_2;
const otherPrivateKey = PrivateKey.fromString(process.env.OTHER_PRIVATE_KEY);

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

## Step 3: Client Setup

It creates a client instance for the Hedera testnet and sets the operator account using the user's account ID and private key.

```javascript
const client = Client.forTestnet();
client.setOperator(myAccountId, myPrivateKey);
```

## Step 4: Main Function

* This is the main function named `main()`.
* It creates a `TransferTransaction` instance to transfer cryptocurrency between two accounts.
* It transfers 100 Hbars from `myAccountId` to `otherAccountId` and transfers 100 Hbars from `myAccountId` to `otherAccountId2`.

```javascript
async function main() {
    const transaction = new TransferTransaction()
        .addHbarTransfer(otherAccountId, Hbar.fromTinybars(-100))
        .addHbarTransfer(otherAccountId2, Hbar.fromTinybars(100));

```

* It creates a `ScheduleCreateTransaction` instance to schedule the previously created transfer transaction.
* It sets the scheduled transaction to the transfer transaction created earlier, sets a memo for the schedule, and sets the admin key (private key) for the schedule.
* The schedule transaction is then executed using the client.

```javascript
const scheduleTransaction = await new ScheduleCreateTransaction()
    .setScheduledTransaction(transaction)
    .setScheduleMemo("Scheduled TX!")
    .setAdminKey(myPrivateKey)
    .execute(client);

```

* t retrieves the receipt of the schedule transaction to obtain information about the created schedule.
* It extracts the schedule ID and the ID of the scheduled transaction from the receipt.
* The script then logs the schedule ID and the scheduled transaction ID to the console.

```javascript
const receipt = await scheduleTransaction.getReceipt(client);

const scheduleId = receipt.scheduleId;
console.log("The schedule ID is " +scheduleId);

const scheduledTxId = receipt.scheduledTransactionId;
console.log("The scheduled transaction ID is " +scheduledTxId);

process.exit();

```

## Step 5: Execution

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

```javascript
main();
```
