---
title: "How to Auto-Create Hedera Accounts with HBAR and Token Transfers"
id: "15820"
type: "post"
slug: "how-to-auto-create-hedera-accounts-with-hbar-and-token-transfers"
published_at: "2022-10-26T23:25:00+00:00"
modified_at: "2025-12-08T18:57:03+00:00"
url: "https://hedera.com/blog/how-to-auto-create-hedera-accounts-with-hbar-and-token-transfers/"
markdown_url: "https://hedera.com/blog/how-to-auto-create-hedera-accounts-with-hbar-and-token-transfers.md"
excerpt: "In this tutorial learn how to auto-create Hedera accounts by sending HBAR and tokens to an alias."
taxonomy_category:
  - "Uncategorized"
taxonomy_post_tag:
  - "technical"
---

[Skip to content](#content)
blog

# How to Auto-Create Hedera Accounts with HBAR and Token Transfers

October 26, 2022

Hedera

Hedera provides secure, scalable infrastructure for real-world decentralized applications in finance, AI, and sustainability, governed by global enterprises.

[HIP-32](https://hips.hedera.com/hip/hip-32)
 introduced the ability to auto-create accounts when sending HBAR to an [alias](https://docs.hedera.com/hedera/sdks-and-apis/sdks/cryptocurrency/create-an-account#create-an-account-via-an-account-alias)
 that does not exist on the network. When HBAR is sent to an alias that does not exist on the network, the account creation fee is deducted from the HBAR sent and the account is auto-created. The new account’s initial balance is (sent HBAR – account creation fee). This new method of account creation allowed wallet providers to create free “accounts” to users. However, if a user sends fungible tokens or NFT’s to an alias, it would result in an INVALID_ACCOUNT_ID error because the alias does not exist on the network. The auto-account creation flow could not deduct the account creation fee from an HTS token; the account creation fee must be paid in HBAR.

[HIP-542](https://hips.hedera.com/hip/hip-542)
 provides a solution to allow sending HTS tokens to an alias that does not exist on the network. This is achieved by charging the account creation fee to the transfer transaction payer. In addition, there will be one auto-association slot included in the transaction for the new account to associate with the HTS token. You won’t have to first create the account, complete a token association, and then finally do a token transfer.

Furthermore, this change also applies to sending hbar to an alias. Instead of deducting the creation fee from the sent hbar, it will be deducted from the payer of the transfer transaction. The new account balance will receive the sent hbar amount in full. Learn more [here](https://hedera.com/blog/auto-create-a-hedera-account-with-hbar-and-token-transfers)
.

The figure below highlights the transaction flow before HIP-542 and after.

#### **Try It Yourself:**

- Get a [Hedera testnet](https://portal.hedera.com/register) account
  - This portal acts like a faucet, giving you 10,000 test HBAR every 24 hours!

- Use this [Codesandbox](https://codesandbox.io/s/auto-account-create-with-ft-th4lnh?file=/.env) to try auto-creating an account by sending FT to an alias
- Use this [Codesandbox](https://codesandbox.io/s/auto-account-create-with-nft-c4me4d?file=/.env) to try auto-creating an account by sending an NFT to an alias
  - Fork the sandbox
  - Remember to provide testnet account credentials in the .env file
  - Open a new terminal to execute: npm run start

- Get the example code on Github:
  - [auto-create account by sending FT](https://github.com/a-ridley/hedera-auto-account-creation-with-ft)
  - [auto-create account by sending NFT](https://github.com/a-ridley/hedera-auto-create-account-with-nft)

Let’s work through the below example which will walk us through auto-account creation when sending HTS tokens. If you need assistance creating a client and setting up your environment make sure to start on our [getting started section](https://docs.hedera.com/hedera/getting-started/environment-set-up)
.

#### Example: Treasury sends FT’s and an NFT to Bob’s alias to auto-create an account

This example guides you through the following steps:

1. Creating a treasury account
2. Creating fungible tokens and an NFT collection (1000 FT / 5 NFT)
3. Creating Bob’s ECDSA public key alias
4. Treasury account transfers HTS token to Bob’s alias using the transfer transaction (10 FT / 1 NFT)
5. Return Bob’s new account ID
6. Show Bob’s new account ID owns the tokens and NFT

#### Set up helper functions

We will create the functions necessary to create a new account, create fungible tokens, and create a new NFT collection. Use the code tab switch on the upper left of the code block to see the helper functions.

#### 1. Create a treasury account

We create the treasury account which will be the holder of the fungible and non-fungible tokens. The treasury account will be created with an initial balance of 100 HBAR.

```
const [treasuryAccId, treasuryAccPvKey] = await createAccount(client, 100);
```

#### 2. Create FTs and create an NFT collection

Leverage the ***createFungibleToken*** helper function defined above to create 10000 “Hip-542 example” fungible tokens. Use the code tab switch on the upper left of the code block to see how we use ***createNewNftCollection*** to create our new NFT collection consisting of 5 NFTs.

#### 3. Create Bob’s ECDSA public key alias

An alias is an initial public key that will convert into a Hedera account through auto-account creation. An alias consists of <shard>.<realm>.<bytes>.

To learn more about accounts created via an account alias go [here](https://docs.hedera.com/hedera/sdks-and-apis/sdks/cryptocurrency/create-an-account#create-an-account-via-an-account-alias)
.

```
 const privateKey = PrivateKey.generateECDSA();
 const publicKey = privateKey.publicKey;
 
 // Assuming that the target shard and realm are known.
 // For now they are virtually always 0 and 0.
 const aliasAccountId = publicKey.toAccountId(0, 0);
 
 console.log(`- New account ID: ${aliasAccountId.toString()}`);
 if (aliasAccountId.aliasKey === null) { throw new Error('alias key is empty') }
 console.log(`- Just the aliasKey: ${aliasAccountId.aliasKey.toString()}n`);
```

#### Set up helper functions for transferring HTS tokens

Once we have our treasury account with FT and a new NFT collection created, our next step is to transfer them to Bob using their alias. We’ll create the ***sendToken*** helper function to send fungible tokens and create***transferNft***to send a single NFT.

A quick reminder to use the tab on the left of the code block to switch between the two helper functions.

#### 4. Transfer FT and an NFT to Bob using their alias

Transfer 10 fungible tokens to Bob using their alias and the helper function ***sendToken***.

Transfer the NFT with serial number 1 to Bob using the helper function ***transfertNFT***.

#### 5. Return the new account ID

Create a helper function to return the corresponding account Id to the given an alias.

```
export const getAccountIdByAlias = async (client: Client, aliasAccountId: AccountId ) => {
 const accountInfo =  await new AccountInfoQuery()
   .setAccountId(aliasAccountId)
   .execute(client);
  
return accountInfo.accountId;
}
```

Next we call getAccountIdByAlias and pass in our client and Bob’s alias as the arguments.

```
const accountId = await getAccountIdByAlias(client, aliasAccountId);
console.log(`The normal account ID of the given alias: ${accountId}`);
```

#### 6. Show Bob’s new account owns the 10 FT tokens

Complete an AccountBalanceQuery to show that Bob’s new account owns the 10 fungible tokens the treasury account sent.

```
 const accountBalances = await new AccountBalanceQuery()
   .setAccountId(aliasAccountId)
   .execute(client);
 
 if (!accountBalances.tokens || !accountBalances.tokens._map) {
   throw new Error('account balance shows no tokens.')
 }
 
 const tokenBalanceAccountId = accountBalances.tokens._map
   .get(tokenId.toString());
 
 if (!tokenBalanceAccountId) {
   throw new Error(`account balance does not have tokens for token id: ${tokenId}.`);
 }
 
 tokenBalanceAccountId.toInt() === 10
   ? console.log(
     `Account is created successfully using HTS 'TransferTransaction'`
   )
   : console.log(
     "Creating account with HTS using public key alias failed"
   );
 
 client.close();
```

6a. Show Bob’s new account owns the NFT

First create a helper function that creates a TokenNftInfoQuery transaction and returns the account id of the nft owner for a specific nft serial number.

```
export const getNftOwnerByNftId = async (client: Client, nftTokenId: TokenId, exampleNftId: number) => {
 const nftInfo = await new TokenNftInfoQuery()
   .setNftId(new NftId(nftTokenId, exampleNftId))
   .execute(client);
 
 if (nftInfo === null) { throw new Error('nftInfo is null.') }
 const nftOwnerAccountId = nftInfo[0].accountId.toString();
 console.log(`- Current owner account id: ${nftOwnerAccountId} for NFT with serial number: ${exampleNftId}`);
  return nftOwnerAccountId;
}
```

Then call ***getNftOwnerByNft*** and do a simple check to ensure the account id returned matches the account id created when we sent the NFT to Bob’s alias.

```
 const nftOwnerAccountId = await getNftOwnerByNftId(client, nftTokenId, exampleNftId);
 
 nftOwnerAccountId === accountId
   ? console.log(
     `The NFT owner accountId matches the accountId created with the HTSn`
   )
   : console.log(`The two account IDs does not matchn`);
  
 client.close();
```

And that’s a wrap! You’ve completed sending HTS tokens to an alias and triggering an auto-account creation! As well as learned that the account creation fee is paid by the payer of the transfer transaction.

Join and collaborate with Hedera Developers on the [Hedera Discord Server](https://hedera.com/discord)
!

Happy Building!

[Back to Blog](/blog)

discover

See more articles

[View All](/blog)

August 5, 2026

### Details decide. What digital asset policy did in July 2026

Halfway through 2026, the regulatory picture is coming into focus. Hedera Chief Policy Officer Nilmini Rubin and VP Global Policy Isadora Arredondo break down what moved in June across the

[Read More](https://hedera.com/blog/details-decide-what-digital-asset-policy-did-in-july-2026/)

July 17, 2026

### Deploy Multichain Dapps on Hedera in 60 Seconds with scaffold-hbar

Deploying multichain dapps on Hedera has never been easier! With just one command, you can spin up a fully functional dapp using Next.js, Hardhat or Foundry, and AI agent context…

[Read More](https://hedera.com/blog/deploy-multichain-dapps-on-hedera-in-60-seconds-with-scaffold-hbar/)

July 13, 2026

### Regulation is finding its form in summer 2026

Halfway through 2026, the regulatory picture is coming into focus. Hedera Chief Policy Officer Nilmini Rubin and VP Global Policy Isadora Arredondo break down what moved in June across the

[Read More](https://hedera.com/blog/regulation-is-finding-its-form-in-summer-2026/)

## Ready to get started?

Discover why Hedera is the trusted institutional-grade network powering the new digital economy.

[Start Building](/start-building)

[Contact](/contact)

We use cookies to deliver the best experience on our website and to analyze traffic. By continuing to use this site, you consent to our cookie policy.

Review our [Privacy Policy](/privacy)
 to understand how Hedera collects and uses information.

Accept All CookiesAccept Necessary Cookies
