Creates an instance of the Aptos client with the specified configuration. This allows you to interact with the Aptos blockchain using the provided settings.
The configuration settings for the Aptos client.
Represents the configuration settings for an Aptos SDK client instance. This class allows customization of various endpoints and client settings.
Initializes an instance of the Aptos client with the specified settings. This allows users to configure various aspects of the client, such as network and endpoints.
Optional
settings: AptosSettingsOptional configuration settings for the Aptos client.
Optional
Readonly
client?: ClientOptional
Readonly
clientConfig?: ClientConfigOptional
Readonly
faucet?: stringOptional
Readonly
faucetConfig?: FaucetConfigOptional
Readonly
fullnode?: stringOptional
Readonly
fullnodeConfig?: FullNodeConfigOptional
Readonly
indexer?: stringOptional
Readonly
indexerConfig?: IndexerConfigOptional
Readonly
network?: NetworkOptional
Readonly
pepper?: stringOptional
Readonly
prover?: stringimport { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
async function runExample() {
// Create a new Aptos client with default settings
const config = new AptosConfig({ network: Network.TESTNET }); // Specify the network
const aptos = new Aptos(config);
console.log("Aptos client initialized:", aptos);
}
runExample().catch(console.error);
Readonly
client: ClientThe client instance the SDK uses. Defaults to `@aptos-labs/aptos-client
Optional
Readonly
clientConfig?: ClientConfigOptional client configurations
Optional
Readonly
faucet?: stringThe optional hardcoded faucet URL to send requests to instead of using the network
Optional
Readonly
faucetConfig?: FaucetConfigOptional specific Faucet configurations
Optional
Readonly
fullnode?: stringThe optional hardcoded fullnode URL to send requests to instead of using the network
Optional
Readonly
fullnodeConfig?: ClientHeadersTypeOptional specific Fullnode configurations
Optional
Readonly
indexer?: stringThe optional hardcoded indexer URL to send requests to instead of using the network
Optional
Readonly
indexerConfig?: ClientHeadersTypeOptional specific Indexer configurations
Readonly
network: NetworkThe Network that this SDK is associated with. Defaults to DEVNET
Optional
Readonly
pepper?: stringThe optional hardcoded pepper service URL to send requests to instead of using the network
Optional
Readonly
prover?: stringThe optional hardcoded prover service URL to send requests to instead of using the network
Returns the URL endpoint to send the request to based on the specified API type. If a custom URL was provided in the configuration, that URL is returned. Otherwise, the URL endpoint is derived from the network.
The type of Aptos API to get the URL for. This can be one of the following: FULLNODE, FAUCET, INDEXER, PEPPER, PROVER.
import { Aptos, AptosConfig, Network, AptosApiType } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Getting the request URL for the FULLNODE API
const url = config.getRequestUrl(AptosApiType.FULLNODE);
console.log("Request URL for FULLNODE:", url);
}
runExample().catch(console.error);
Checks if the provided URL is a known pepper service endpoint.
The URL to check against the known pepper service endpoints.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
const url = "https://example.pepper.service"; // replace with a real pepper service URL
// Check if the URL is a known pepper service endpoint
const isPepperService = config.isPepperServiceRequest(url);
console.log(`Is the URL a known pepper service? ${isPepperService}`);
}
runExample().catch(console.error);
Checks if the provided URL is a known prover service endpoint.
The URL to check against known prover service endpoints.
A boolean indicating whether the URL is a known prover service endpoint.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
// Check if the URL is a known prover service endpoint
const url = "https://prover.testnet.aptos.dev"; // replace with a real URL if needed
const isProver = config.isProverServiceRequest(url);
console.log(`Is the URL a known prover service? ${isProver}`);
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
async function runExample() {
// Create a new Aptos client instance
const config = new AptosConfig({ network: Network.TESTNET }); // Specify the network
const aptos = new Aptos(config);
console.log("Aptos client created successfully:", aptos);
}
runExample().catch(console.error);
The arguments for batching transactions.
An array of transaction payloads to be processed.
Optional
options?: Omit<InputGenerateTransactionOptions, "accountSequenceNumber">Optional. Transaction generation configurations (excluding accountSequenceNumber).
The sender account to sign and submit the transactions.
Prefer to use aptos.transaction.batch.forSingleAccount()
Batch transactions for a single account by submitting multiple transaction payloads. This function is useful for efficiently processing and submitting transactions that do not depend on each other, such as batch funding or batch token minting.
import { Aptos, AptosConfig, Network, Account } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
const sender = Account.generate(); // Generate a new account for sending transactions
async function runExample() {
const transactions = [
{ }, // Build your first transaction payload
{ }, // Build your second transaction payload
];
// Batch transactions for the single account
await aptos.batchTransactionsForSingleAccount({
sender,
data: transactions,
});
console.log("Batch transactions submitted successfully.");
}
runExample().catch(console.error);
Estimates the gas unit price required to process a transaction on the Aptos blockchain in a timely manner. This helps users to understand the cost associated with their transactions. https://api.mainnet.aptoslabs.com/v1/spec#/operations/estimate_gas_price
An object containing the estimated gas price.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET }); // Specify your network
const aptos = new Aptos(config);
async function runExample() {
// Getting the gas price estimation
const gasPriceEstimation = await aptos.getGasPriceEstimation();
console.log("Estimated Gas Price:", gasPriceEstimation);
}
runExample().catch(console.error);
Returns a signing message for a transaction, allowing a user to sign it using their preferred method before submission to the network.
The arguments for obtaining the signing message.
A raw transaction for signing elsewhere.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
const transaction = await aptos.transaction.build.simple({
sender: "0x1", // replace with a real sender address
data: {
function: "0x1::aptos_account::transfer",
functionArguments: ["0x2", 100], // replace with a real destination address
},
});
const message = await aptos.getSigningMessage({ transaction });
console.log(message);
}
runExample().catch(console.error);
Queries on-chain transactions by their transaction hash, returning both pending and committed transactions.
The transaction from the mempool (pending) or the on-chain (committed) transaction.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Fetch a transaction by its hash
const transaction = await aptos.getTransactionByHash({ transactionHash: "0x123" }); // replace with a real transaction hash
console.log(transaction);
}
runExample().catch(console.error);
Queries on-chain transaction by version. This function will not return pending transactions.
On-chain transaction. Only on-chain transactions have versions, so this function cannot be used to query pending transactions.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Fetching a transaction by its version
const transaction = await aptos.getTransactionByVersion({ ledgerVersion: 1 }); // replace 1 with a real version
console.log(transaction);
}
runExample().catch(console.error);
Queries on-chain transactions, excluding pending transactions. Use this function to retrieve historical transactions from the blockchain.
Optional
args: { options?: PaginationArgs }Optional parameters for pagination.
Optional
options?: PaginationArgsOptional pagination options.
An array of on-chain transactions.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Fetch transactions with pagination
const transactions = await aptos.getTransactions({
options: {
offset: 0, // Start from the first transaction
limit: 10, // Limit to 10 results
},
});
console.log(transactions);
}
runExample().catch(console.error);
Defines if the specified transaction is currently in a pending state. This function helps you determine the status of a transaction using its hash.
true
if the transaction is in a pending state and false
otherwise.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Check if the transaction is pending using its hash
const isPendingTransaction = await aptos.isPendingTransaction({ transactionHash: "0x123" }); // replace with a real transaction hash
console.log("Is the transaction pending?", isPendingTransaction);
}
runExample().catch(console.error);
Generates a transaction to publish a Move package to the blockchain. This function helps you create a transaction that can be simulated or submitted to the chain for publishing a package.
To get the metadataBytes
and byteCode
, can compile using Aptos CLI with command
aptos move compile --save-metadata ...
,
https://aptos.dev/tutorials/your-first-dapp/#step-4-publish-a-move-module
The arguments for publishing the package.
The publisher account.
The package metadata bytes.
An array of the bytecode of each module in the package in compiler output order.
Optional
options?: InputGenerateTransactionOptionsOptional settings for generating the transaction.
A SimpleTransaction that can be simulated or submitted to the chain.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Replace with a real account address
const account = "0x1";
const metadataBytes = "0x..."; // replace with real metadata bytes
const byteCode = "0x..."; // replace with real module bytecode
const transaction = await aptos.publishPackageTransaction({
account,
metadataBytes,
moduleBytecode: [byteCode],
});
console.log(transaction);
}
runExample().catch(console.error);
Rotates the authentication key for a given account. Once an account is rotated, only the new private key or keyless signing scheme can be used to sign transactions for the account.
The arguments for rotating the authentication key.
The account from which the authentication key will be rotated.
Optional
dangerouslySkipVerification?: undefined(Optional) If true, skips verification steps after rotation. Required when using toAuthKey.
(Optional) The target account to rotate to. Required if not using toNewPrivateKey or toAuthKey.
Optional
dangerouslySkipVerification?: undefined(Optional) If true, skips verification steps after rotation. Required when using toAuthKey.
(Optional) The new private key to rotate to. Required if not using toAccount or toAuthKey.
(Optional) If true, skips verification steps after rotation. Required when using toAuthKey.
(Optional) The new authentication key to rotate to. Can only be used with dangerouslySkipVerification=true.
PendingTransactionResponse
This function supports three modes of rotation:
When not using dangerouslySkipVerification, the function performs additional safety checks and account setup.
If the new key is a multi key, skipping verification is dangerous because verification will publish the public key onchain and prevent users from being locked out of the account from loss of knowledge of one of the public keys.
import { Aptos, AptosConfig, Network, Account, PrivateKey } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Rotate the authentication key for an account
const response = await aptos.rotateAuthKey({
// replace with a real account
fromAccount: Account.generate(),
// replace with a real private key
toNewPrivateKey: new PrivateKey("0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"),
});
console.log(response);
}
runExample().catch(console.error);
Sign a transaction that can later be submitted to the chain. This function is essential for ensuring the authenticity of the transaction by using the provided account's signing capabilities.
The arguments for signing the transaction.
The account that will sign the transaction.
A raw transaction to sign.
AccountAuthenticator - The authenticator for the signed transaction.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
const sender = Account.generate(); // Generate a new account for signing
const transaction = await aptos.transaction.build.simple({
sender: sender.accountAddress,
data: {
function: "0x1::aptos_account::transfer",
functionArguments: [ "0x1", 100 ], // replace with a real account address and amount
},
});
const signedTransaction = await aptos.transaction.sign({
signer: sender,
transaction,
}); // Sign the transaction
console.log("Signed Transaction:", signedTransaction);
}
runExample().catch(console.error);
Sign and submit a single signer transaction as the fee payer to chain given an authenticator by the sender of the transaction.
The fee payer account to sign the transaction
The AccountAuthenticator signed by the sender of the transaction
An instance of a RawTransaction, plus optional secondary/fee payer addresses
PendingTransactionResponse
const transaction = await aptos.transaction.build.simple({sender: alice.accountAddress, feePayer: true ...})
const senderAuthenticator = alice.signTransactionWithAuthenticator(transaction)
const pendingTransaction = await aptos.signAndSubmitAsFeePayer({
senderAuthenticator,
feePayer: bob,
transaction,
})
Sign and submit a single signer transaction to the blockchain. This function allows you to execute a transaction after signing it with the specified account.
The arguments for signing and submitting the transaction.
The signer account to sign the transaction.
An instance of a RawTransaction, plus optional secondary/fee payer addresses.
PendingTransactionResponse
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
const sender = Account.generate(); // Generate a new account for sending the transaction
const transaction = await aptos.transaction.build.simple({
sender: sender.accountAddress,
data: {
function: "0x1::aptos_account::transfer",
functionArguments: [ "0x1", 100 ], // replace with a real account address
},
});
// Sign and submit the transaction
const pendingTransaction = await aptos.signAndSubmitTransaction({
signer: sender,
transaction,
});
console.log(pendingTransaction);
}
runExample().catch(console.error);
Sign a transaction as a fee payer that can later be submitted to the chain. This function ensures that the transaction is marked with the fee payer's address, allowing it to be processed correctly.
The arguments for signing the transaction.
The fee payer signer account.
A raw transaction to sign on. This transaction must include a feePayerAddress
property.
AccountAuthenticator - The authenticator for the signed transaction.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
const sender = Account.generate(); // Generate a new account for the fee payer
const transaction = await aptos.transaction.build.simple({
// All transactions on Aptos are implemented via smart contracts.
function: "0x1::aptos_account::transfer",
functionArguments: [sender.accountAddress, 100],
feePayerAddress: sender.accountAddress, // Set the fee payer address
});
const signedTransaction = await aptos.transaction.signAsFeePayer({
signer: sender,
transaction,
});
console.log("Signed transaction as fee payer:", signedTransaction);
}
runExample().catch(console.error);
Waits for a transaction to move past the pending state and provides the transaction response. There are 4 cases.
checkSuccess
is true, the function will throw a FailedTransactionError
If checkSuccess
is false, the function will resolve with the transaction response where the success
field is false.args.options.timeoutSecs
seconds.
Optional
options?: WaitForTransactionOptionsOptional parameters for waiting behavior.
The hash of a transaction previously submitted to the blockchain.
The transaction on-chain response.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Wait for a transaction to complete using its hash
const transactionHash = "0x123"; // replace with a real transaction hash
const transactionResponse = await aptos.waitForTransaction({
transactionHash,
options: {
timeoutSecs: 30, // specify your own timeout if needed
checkSuccess: true,
},
});
console.log(transactionResponse);
}
runExample().catch(console.error);
Represents a transaction in the Aptos blockchain, providing methods to build, simulate, submit, and manage transactions. This class encapsulates functionalities for querying transaction details, estimating gas prices, signing transactions, and handling transaction states.
This class is used as part of the Aptos object, so should be called like so:
Example