Typescript SDK for Aptos
The TypeScript SDK allows you to connect, explore, and interact on the Aptos blockchain. You can use it to request data, send transactions, set up test environments, and more!
Learn How To Use The TypeScript SDK
Installation
For use in Node.js or a web application
Install with your favorite package manager such as npm, yarn, or pnpm:
pnpm install @aptos-labs/ts-sdk
For use in a browser (<= 1.9.1 version only)
You can add the SDK to your web application using a script tag:
<script src="https://unpkg.com/@aptos-labs/ts-sdk/dist/browser/index.global.js" />
Then, the SDK can be accessed through window.aptosSDK
.
Usage
Create an Aptos
client in order to access the SDK's functionality.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk"
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
Reading Data From Onchain (Guide)
const fund = await aptos.getAccountInfo({ accountAddress: "0x123" });
const modules = await aptos.getAccountModules({ accountAddress: "0x123" });
const tokens = await aptos.getAccountOwnedTokens({ accountAddress: "0x123" });
Account management (default to Ed25519)
Note: We introduce a Single Sender authentication (as introduced in AIP-55). Generating an account defaults to Legacy Ed25519 authentication with the option to use the Single Sender unified authentication.
Generate new keys
const account = Account.generate();
const account = Account.generate({ scheme: SigningSchemeInput.Secp256k1Ecdsa });
const account = Account.generate({ scheme: SigningSchemeInput.Ed25519, legacy: false });
Derive from private key
const privateKey = new Ed25519PrivateKey("myEd25519privatekeystring");
const privateKey = new Secp256k1PrivateKey("mySecp256k1privatekeystring");
const account = await Account.fromPrivateKey({ privateKey });
const aptos = new Aptos();
const account = await aptos.deriveAccountFromPrivateKey({ privateKey });
Derive from private key and address
const privateKey = new Ed25519PrivateKey("myEd25519privatekeystring");
const privateKey = new Secp256k1PrivateKey("mySecp256k1privatekeystring");
const address = AccountAddress.from("myaccountaddressstring");
const account = await Account.fromPrivateKeyAndAddress({ privateKey, address });
Derive from path
const path = "m/44'/637'/0'/0'/1";
const mnemonic = "various float stumble...";
const account = Account.fromDerivationPath({ path, mnemonic });
Submit transaction (Tutorial)
import {
Account,
Aptos,
AptosConfig,
Network,
} from "@aptos-labs/ts-sdk";
async function example() {
console.log("This example will create two accounts (Alice and Bob) and send a transaction transfering APT to Bob's account.");
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
let alice = Account.generate();
let bob = Account.generate();
console.log("=== Addresses ===\n");
console.log(`Alice's address is: ${alice.accountAddress}`);
console.log(`Bob's address is: ${bob.accountAddress}`);
console.log("\n=== Funding accounts ===\n");
await aptos.fundAccount({
accountAddress: alice.accountAddress,
amount: 100_000_000,
});
await aptos.fundAccount({
accountAddress: bob.accountAddress,
amount: 100,
});
console.log("Funded Alice and Bob's accounts!")
console.log("\n=== 1. Building the transaction ===\n");
const transaction = await aptos.transaction.build.simple({
sender: alice.accountAddress,
data: {
function: "0x1::aptos_account::transfer",
functionArguments: [bob.accountAddress, 100],
},
});
console.log("Built the transaction!")
console.log("\n === 2. Simulating Response (Optional) === \n")
const [userTransactionResponse] = await aptos.transaction.simulate.simple({
signerPublicKey: alice.publicKey,
transaction,
});
console.log(userTransactionResponse)
console.log("\n=== 3. Signing transaction ===\n");
const senderAuthenticator = aptos.transaction.sign({
signer: alice,
transaction,
});
console.log("Signed the transaction!")
console.log("\n=== 4. Submitting transaction ===\n");
const submittedTransaction = await aptos.transaction.submit.simple({
transaction,
senderAuthenticator,
});
console.log(`Submitted transaction hash: ${submittedTransaction.hash}`);
console.log("\n=== 5. Waiting for result of transaction ===\n");
const executedTransaction = await aptos.waitForTransaction({ transactionHash: submittedTransaction.hash });
console.log(executedTransaction)
};
example();
Troubleshooting
If you see import error when you do this
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
It could be your tsconfig.json
is not using node
. Make sure your moduleResolution
in the tsconfig.json
is set to node
instead of bundler
.
Contributing
If you found a bug or would like to request a feature, please file an issue.
If, based on the discussion on an issue you would like to offer a code change, please make a pull request.
If neither of these describes what you would like to contribute, checkout out the contributing guide.