APTOS Developer’s Guide | Your First Transaction | Typescript

This guide describes step by step how to generate, send and verify transactions submitted to the Aptos blockchain:

1. Create an image of the account.

Each Aptos account has a unique account address. The owner of that account owns a public and private key pair that is mapped to the Aptos account address and, in turn, the authentication key stored in that account.

For more information about Aptos accounts, see Accounts.

2. Prepare a shell for REST interfaces.

Aptos provides REST APIs to interact with the blockchain. This step prepares the shells around that API to retrieve account information and to create a transaction, sign it, and send it. In this tutorial, we use the Typescript SDK for the Typescript example

It is preferable to use Aptos SDK if it is available.

3. Prepare a shell for the Faucet interface.

Using the Faucet interface in Aptos devnet, this programming code automatically creates an account with an account address of 0x1 and replenishes it.

4. Combine the above shells into an application, execute and test.

Before you start.

First complete the following steps so that you can begin training.

  1. Clone the Aptos repository.
git clone https://github.com/aptos-labs/aptos-core.git
Enter fullscreen mode. Exit fullscreen mode
cd aptos-core
Enter fullscreen mode. Exit fullscreen mode
  1. Check the devnet branch with git checkout --track origin/devnet.
  2. Run the bash script scripts/dev_setup.sh as shown below. This will prepare your environment for development.
Enter fullscreen mode Exit fullscreen mode
  1. Refresh your current console environment.
source ~/.cargo/env
Enter fullscreen mode Exit fullscreen mode

Your development environment is ready, now you’re ready to run through this tutorial.

Source from GitHub

Follow the links below to access the source code for the tutorial:
See the code first_transaction.ts in the Typescript project guide.

Step 1: Create an account image

This step creates an account image. See also Aptos Accounts and Creating a Signed Transaction.

/** AptosAccount provides methods around addresses, key-pairs */
import { AptosAccount, TxnBuilderTypes, BCS, MaybeHexString } from "aptos";
Enter fullscreen mode. Exit fullscreen mode

Step 2: The REST interface

While you can read data from the REST interface directly, the following code examples demonstrate a more ergonomic approach using the REST interface:

  • Retrieving data from the FullNode registry, including account data and account resources.
  • Creating signed transactions, presented in JSON format.
/** Wrappers around the Aptos Node and Faucet API */
import { AptosClient, FaucetClient } from "aptos";
Enter fullscreen mode Exit fullscreen mode

Step 2.1: Reading an account

Below are the shells for requesting credentials.

const client = new AptosClient(NODE_URL);
 * https://aptos-labs.github.io/ts-sdk-doc/classes/AptosClient.html#getAccount
 * returns the sequence number and authentication key for an account
 * https://aptos-labs.github.io/ts-sdk-doc/classes/AptosClient.html#getAccountResource
 * returns all resources associated with the account
Enter fullscreen mode Exit fullscreen mode

Step 2.2: Sending a transaction

The following shows basic information on creating, signing and executing a transaction.

 * https://aptos-labs.github.io/ts-sdk-doc/classes/AptosClient.html#generateBCSTransaction
 * signs a raw transaction, which can be submitted to the blockchain.

 * https://aptos-labs.github.io/ts-sdk-doc/classes/AptosClient.html#submitSignedBCSTransaction
 * submits a signed transaction to the blockchain.
Enter fullscreen mode Exit fullscreen mode

Step 2.3: Application-specific logical actions

The following shows how to read data from the blockchain and how to send a specific transaction.

/** Helper method returns the coin balance associated with the account */
export async function accountBalance(accountAddress: MaybeHexString): Promise<number | null> {
  const resource = await client.getAccountResource(accountAddress, "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
  if (resource == null) {
    return null;

  return parseInt((resource.data as any)["coin"]["value"]);

 * Transfers a given coin amount from a given accountFrom to the recipient's account address.
 * Returns the transaction hash of the transaction used to transfer.
async function transfer(accountFrom: AptosAccount, recipient: MaybeHexString, amount: number): Promise<string> {
  const token = new TxnBuilderTypes.TypeTagStruct(TxnBuilderTypes.StructTag.fromString("0x1::aptos_coin::AptosCoin"));

  const entryFunctionPayload = new TxnBuilderTypes.TransactionPayloadEntryFunction(
      [BCS.bcsToBytes(TxnBuilderTypes.AccountAddress.fromHex(recipient)), BCS.bcsSerializeUint64(amount)],

  const [{ sequence_number: sequenceNumber }, chainId] = await Promise.all([

  const rawTxn = new TxnBuilderTypes.RawTransaction(
    BigInt(Math.floor(Date.now() / 1000) + 10),
    new TxnBuilderTypes.ChainId(chainId),

  const bcsTxn = AptosClient.generateBCSTransaction(accountFrom, rawTxn);
  const pendingTxn = await client.submitSignedBCSTransaction(bcsTxn);

  return pendingTxn.hash;
Enter fullscreen mode Exit fullscreen mode

Step 3: The Faucet interface

Aptos Blockchain Faucets issue test tokens to accounts. These test tokens can be used for testing purposes, such as paying for gas or transferring tokens between users. Faucet Aptos can also create accounts if none exist. The Faucet Aptos interface requires a public key represented as a string in hexadecimal encoding.

/** Faucet creates and funds accounts. */
const faucetClient = new FaucetClient(NODE_URL, FAUCET_URL);
Enter fullscreen mode Exit fullscreen mode

Step 4: Run the application

At last we can launch the application and check the result.

For Typescript:

  • Make sure you have met the prerequisites described in Before you start.
  • Install the required libraries: yarn install.
  • Run the example: yarn first_transaction.


Output after execution:

=== Addresses ===
Alice: e26d69b8d3ff12874358da6a4082a2ac
Bob: c8585f009c8a90f22c6b603f28b9ed8c

=== Initial Balances ===
Alice: 5000
Bob: 0

=== Final Balances ===
Alice: 3927
Bob: 1000
Enter fullscreen mode Exit fullscreen mode

The output shows that Bob received 1000 coins from Alice. Alice paid 73 coins for the gas.


You can check the data by visiting either the REST interface or the Explorer:

  • Alice’s account through the Aptos REST interface.
  • Bob’s account through the Aptos Explorer.

Aptos devnet will reboot from time to time so the above links may not work. Try following the instructions yourself and then check the accounts in Aptos Explorer.

Оцените статью
Добавить комментарий