Loading...
Loading...
How to create and configure the Aptos client (Aptos, AptosConfig) in @aptos-labs/ts-sdk. Covers Network, fullnode/indexer/faucet URLs, singleton pattern, and Bun compatibility. Triggers on: 'Aptos client', 'AptosConfig', 'SDK client', 'client setup', 'new Aptos(', 'Network.TESTNET', 'Network.MAINNET'.
npx skill4agent add iskysun96/aptos-agent-skills ts-sdk-client@aptos-labs/ts-sdknew Aptos(config)AptosConfigNetwork.TESTNETNetwork.MAINNETprocess.env.APTOS_NETWORKimport.meta.env.VITE_APP_NETWORKNetwork.TESTNETNetworknetworkNetwork.CUSTOMimport { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);// Predefined networks
const devnet = new AptosConfig({ network: Network.DEVNET });
const testnet = new AptosConfig({ network: Network.TESTNET });
const mainnet = new AptosConfig({ network: Network.MAINNET });
// Custom endpoints (network is REQUIRED in v5.2+)
const custom = new AptosConfig({
network: Network.CUSTOM,
fullnode: "https://your-fullnode.example.com/v1",
indexer: "https://your-indexer.example.com/v1/graphql",
faucet: "https://your-faucet.example.com",
});// lib/aptos.ts or similar
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
function getNetwork(): Network {
const raw = typeof process !== "undefined" ? process.env.APTOS_NETWORK : import.meta.env?.VITE_APP_NETWORK;
switch (raw) {
case "mainnet": return Network.MAINNET;
case "devnet": return Network.DEVNET;
default: return Network.TESTNET;
}
}
const config = new AptosConfig({ network: getNetwork() });
export const aptos = new Aptos(config);const config = new AptosConfig({
network: Network.TESTNET,
fullnode: "https://fullnode.testnet.aptoslabs.com/v1", // override default
indexer: "https://indexer.testnet.aptoslabs.com/v1/graphql",
faucet: "https://faucet.testnet.aptoslabs.com",
pepper: "https://...", // keyless pepper service
prover: "https://...", // keyless prover
});
const aptos = new Aptos(config);// Disable HTTP/2 when using Bun (recommended)
const config = new AptosConfig({
network: Network.TESTNET,
clientConfig: { http2: false },
});
const aptos = new Aptos(config);aptosaptos.getAccountInfo()aptos.getBalance()aptos.getAccountResources()aptos.transaction.build.simple()aptos.signAndSubmitTransaction()aptos.waitForTransaction()aptos.view()aptos.fundAccount()aptos.coin.*aptos.digitalAsset.*aptos.fungibleAsset.*aptos.object.*aptos.ans.*aptos.staking.*| Mistake | Correct approach |
|---|---|
| Creating Aptos in every function | One singleton; pass |
| Using devnet for persistent dev | Prefer testnet; devnet resets |
| Custom URLs without Network.CUSTOM | Set |
| Forgetting http2: false on Bun | Set |
src/api/aptos.tssrc/api/aptosConfig.ts