ts-sdk-client
Original:🇺🇸 English
Not Translated
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'.
5installs
Added on
NPX Install
npx skill4agent add aptos-labs/aptos-agent-skills ts-sdk-clientSKILL.md Content
TypeScript SDK: Aptos Client
Purpose
Guide creation and configuration of the Aptos client and AptosConfig in . One client
instance is used for all read/write and account/transaction APIs.
@aptos-labs/ts-sdkALWAYS
- Create one Aptos instance per app (singleton) and reuse it – avoid multiple for the same network.
new Aptos(config) - Configure network via – use
AptosConfigorNetwork.TESTNET(or custom endpoints).Network.MAINNET - Use environment variables for network/URLs in production – e.g. or
process.env.APTOS_NETWORK.import.meta.env.VITE_APP_NETWORK - Use as default for development – devnet resets frequently.
Network.TESTNET
NEVER
- Do not create a new Aptos client per request – reuse the singleton.
- Do not hardcode fullnode/indexer URLs in source when using public networks – use enum.
Network - Do not omit when using custom endpoints – in v5.2+ use
networkwith custom URLs.Network.CUSTOM
Basic setup
typescript
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);Network options
typescript
// 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"
});Singleton pattern (recommended)
typescript
// 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);Optional endpoints (override per service)
typescript
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);Client config (HTTP, timeouts, Bun)
typescript
// Disable HTTP/2 when using Bun (recommended)
const config = new AptosConfig({
network: Network.TESTNET,
clientConfig: { http2: false }
});
const aptos = new Aptos(config);Using the client
After construction, use the same instance for:
aptos- Account / balance: ,
aptos.getAccountInfo(),aptos.getBalance(), etc.aptos.getAccountResources() - Transactions: ,
aptos.transaction.build.simple(),aptos.signAndSubmitTransaction().aptos.waitForTransaction() - View: .
aptos.view() - Faucet: (devnet/testnet).
aptos.fundAccount() - Coin / token / object / ANS / staking: ,
aptos.coin.*,aptos.digitalAsset.*,aptos.fungibleAsset.*,aptos.object.*,aptos.ans.*.aptos.staking.*
Common mistakes
| 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 |
References
- SDK: ,
src/api/aptos.tssrc/api/aptosConfig.ts - Pattern: TYPESCRIPT_SDK.md
- Related: ts-sdk-account, ts-sdk-transactions, ts-sdk-wallet-adapter, use-ts-sdk