Get started with the Hedera Token Service - Part 1
Dec 07, 2020
by Cooper Kunz
Developer Evangelist

This blog post has been updated to include the latest capabilities of the Hedera Token Service. 

See the updated version: Get Started with the Hedera Token Service - Part 1: How to Mint NFTs

The Hedera Token Service (HTS) is used via a robust set of APIs for the configuration, minting, and management of tokens on Hedera, without needing to set up and deploy a smart contract. Tokens are as fast, fair, and secure as hbar and cost a fraction of 1¢ USD to transfer.

Let’s take a look at why you’d consider using it versus something like a fungible token with a smart contract on the Ethereum Blockchain, and the different types of functionalities that are available within the Hedera API (HAPI):

With HTS, it’s incredibly easy to create a new token that can represent anything from a stablecoin pegged to the USD value, or an in-game reward system.

Note: while most of the following examples are in JavaScript (v2.0.7), official SDKs supporting Go and Java are also available and implemented very similarly, alongside community-supported SDKs in .NET and various other frameworks and/or languages.

Create a Token

Code Snippet Background

//Create a token

const transaction = await new TokenCreateTransaction()

.setTokenName("Your Token Name")

.setTokenSymbol("F")

.setTreasuryAccountId(treasuryAccountId)

.setInitialSupply(5000)

.setAdminKey(adminPublicKey)

.freezeWith(client);


//Sign the transaction with the token adminKey and the token treasury account private key

const signTx = await (await transaction.sign(adminKey)).sign(treasuryKey);


//Sign the transaction with the client operator private key and submit to a Hedera network

const txResponse = await signTx.execute(client);

//Get the receipt of the the transaction

const receipt = await txResponse.getReceipt(client);


//Get the token ID from the receipt

const tokenId = receipt.tokenId;

console.log("The new token ID is " + tokenId);

To show how similar the Hedera Token Service is to use in any of our supported SDKs, here is the same example but in Java.

Code Snippet Background

//Create the transaction

TokenCreateTransaction transaction = new TokenCreateTransaction()

.setTokenName("Your Token Name")

.setTokenSymbol("F")

.setTreasuryAccountId(treasuryAccountId)

.setInitialSupply(5000)

.setAdminKey(adminKey.getPublicKey());


//Build the unsigned transaction, sign with admin private key of the token, sign with the token treasury private key, submit the transaction to a Hedera network

TransactionResponse txResponse = transaction.freezeWith(client).sign(adminKey).sign(treasuryKey).execute(client);


//Request the receipt of the transaction

TransactionReceipt receipt = txResponse.getReceipt(client);


//Get the token ID from the receipt

TokenId tokenId = receipt.tokenId;

System.out.println("The new token ID is " + tokenId);

And here is the same relevant code example for the Go SDK.

Code Snippet Background

//Create the transaction and freeze the unsigned transaction

tokenCreateTransaction, err := hedera.NewTokenCreateTransaction().

SetTokenName("Your Token Name").

SetTokenSymbol("F").

SetTreasuryAccountID(treasuryAccountId).

SetInitialSupply(1000).

SetAdminKey(adminKey).

FreezeWith(client)

if err != nil {

panic(err)

}

//Sign with the admin private key of the token, sign with the token treasury private key, sign with the client operator private key and submit the transaction to a Hedera network

txResponse, err := tokenCreateTransaction.Sign(adminKey).Sign(treasuryKey).Execute(client)

if err != nil {

panic(err)

}


//Request the receipt of the transaction

receipt, err := txResponse.GetReceipt(client)

if err != nil {

panic(err)

}


//Get the token ID from the receipt

tokenId := *receipt.TokenID

fmt.Printf("The new token ID is %v\n", tokenId)

Token Associations

Before another account can receive or send this specific token ID, they have to become “associated” with it — this helps reduce unwanted spam, potential tax liability, or other concerns from users that don’t want to be associated with any of the variety of tokens that will be created on HTS.

Code Snippet Background

//Associate a token to an account and freeze the unsigned transaction for signing

const transaction = await new TokenAssociateTransaction()

.setAccountId(accountId)

.setTokenIds([tokenId])

.freezeWith(client);


//Sign with the private key of the account that is being associated to a token

const signTx = await transaction.sign(accountKey);


//Submit the transaction to a Hedera network

const txResponse = await signTx.execute(client);


//Request the receipt of the transaction

const receipt = await txResponse.getReceipt(client);

//Get the transaction consensus status

const transactionStatus = receipt.status;

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

Transferring tokens


Transferring these newly created tokens between accounts, after the token has been created and both accounts are associated with the new token ID, is almost easier.

Code Snippet Background

//Create the transfer transaction

const transaction = await new TransferTransaction()

.addTokenTransfer(tokenId, accountId1, -10)

.addTokenTransfer(tokenId, accountId2, 10)

.freezeWith(client);


//Sign with the sender account private key

const signTx = await transaction.sign(accountKey1);

//Sign with the client operator private key and submit to a Hedera network

const txResponse = await signTx.execute(client);

//Request the receipt of the transaction

const receipt = await txResponse.getReceipt(client);

//Obtain the transaction consensus status

const transactionStatus = receipt.status;

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

Integrating HTS is incredibly easy, within just a few lines of code in your favorite programming language you can create, associate, and transfer tokens. Please continue reading onto Part 2 of this HTS introduction in order to learn more about the administration functionalities provided by HAPI, and in Part 3 we will discuss other compliance mechanisms like KYC compliance.