$npx -y skills add aptos-labs/aptos-agent-skills --skill ts-sdk-clientHow 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.TESTNE
| 1 | # TypeScript SDK: Aptos Client |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Guide creation and configuration of the **Aptos** client and **AptosConfig** in `@aptos-labs/ts-sdk`. One client |
| 6 | instance is used for all read/write and account/transaction APIs. |
| 7 | |
| 8 | ## ALWAYS |
| 9 | |
| 10 | 1. **Create one Aptos instance per app** (singleton) and reuse it – avoid multiple `new Aptos(config)` for the same |
| 11 | network. |
| 12 | 2. **Configure network via `AptosConfig`** – use `Network.TESTNET` or `Network.MAINNET` (or custom endpoints). |
| 13 | 3. **Use environment variables for network/URLs** in production – e.g. `process.env.APTOS_NETWORK` or |
| 14 | `import.meta.env.VITE_APP_NETWORK`. |
| 15 | 4. **Use `Network.TESTNET` as default for development** – devnet resets frequently. |
| 16 | |
| 17 | ## NEVER |
| 18 | |
| 19 | 1. **Do not create a new Aptos client per request** – reuse the singleton. |
| 20 | 2. **Do not hardcode fullnode/indexer URLs** in source when using public networks – use `Network` enum. |
| 21 | 3. **Do not omit `network` when using custom endpoints** – in v5.2+ use `Network.CUSTOM` with custom URLs. |
| 22 | |
| 23 | --- |
| 24 | |
| 25 | ## Basic setup |
| 26 | |
| 27 | ```typescript |
| 28 | import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk"; |
| 29 | |
| 30 | const config = new AptosConfig({ network: Network.TESTNET }); |
| 31 | const aptos = new Aptos(config); |
| 32 | ``` |
| 33 | |
| 34 | --- |
| 35 | |
| 36 | ## Network options |
| 37 | |
| 38 | ```typescript |
| 39 | // Predefined networks |
| 40 | const devnet = new AptosConfig({ network: Network.DEVNET }); |
| 41 | const testnet = new AptosConfig({ network: Network.TESTNET }); |
| 42 | const mainnet = new AptosConfig({ network: Network.MAINNET }); |
| 43 | |
| 44 | // Custom endpoints (network is REQUIRED in v5.2+) |
| 45 | const custom = new AptosConfig({ |
| 46 | network: Network.CUSTOM, |
| 47 | fullnode: "https://your-fullnode.example.com/v1", |
| 48 | indexer: "https://your-indexer.example.com/v1/graphql", |
| 49 | faucet: "https://your-faucet.example.com" |
| 50 | }); |
| 51 | ``` |
| 52 | |
| 53 | --- |
| 54 | |
| 55 | ## Singleton pattern (recommended) |
| 56 | |
| 57 | ```typescript |
| 58 | // lib/aptos.ts or similar |
| 59 | import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk"; |
| 60 | |
| 61 | function getNetwork(): Network { |
| 62 | const raw = typeof process !== "undefined" ? process.env.APTOS_NETWORK : import.meta.env?.VITE_APP_NETWORK; |
| 63 | switch (raw) { |
| 64 | case "mainnet": |
| 65 | return Network.MAINNET; |
| 66 | case "devnet": |
| 67 | return Network.DEVNET; |
| 68 | default: |
| 69 | return Network.TESTNET; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | const config = new AptosConfig({ network: getNetwork() }); |
| 74 | export const aptos = new Aptos(config); |
| 75 | ``` |
| 76 | |
| 77 | --- |
| 78 | |
| 79 | ## Optional endpoints (override per service) |
| 80 | |
| 81 | ```typescript |
| 82 | const config = new AptosConfig({ |
| 83 | network: Network.TESTNET, |
| 84 | fullnode: "https://fullnode.testnet.aptoslabs.com/v1", // override default |
| 85 | indexer: "https://indexer.testnet.aptoslabs.com/v1/graphql", |
| 86 | faucet: "https://faucet.testnet.aptoslabs.com", |
| 87 | pepper: "https://...", // keyless pepper service |
| 88 | prover: "https://..." // keyless prover |
| 89 | }); |
| 90 | const aptos = new Aptos(config); |
| 91 | ``` |
| 92 | |
| 93 | --- |
| 94 | |
| 95 | ## Client config (HTTP, timeouts, Bun) |
| 96 | |
| 97 | ```typescript |
| 98 | // Disable HTTP/2 when using Bun (recommended) |
| 99 | const config = new AptosConfig({ |
| 100 | network: Network.TESTNET, |
| 101 | clientConfig: { http2: false } |
| 102 | }); |
| 103 | const aptos = new Aptos(config); |
| 104 | ``` |
| 105 | |
| 106 | --- |
| 107 | |
| 108 | ## Using the client |
| 109 | |
| 110 | After construction, use the same `aptos` instance for: |
| 111 | |
| 112 | - **Account / balance:** `aptos.getAccountInfo()`, `aptos.getBalance()`, `aptos.getAccountResources()`, etc. |
| 113 | - **Transactions:** `aptos.transaction.build.simple()`, `aptos.signAndSubmitTransaction()`, |
| 114 | `aptos.waitForTransaction()`. |
| 115 | - **View:** `aptos.view()`. |
| 116 | - **Faucet:** `aptos.fundAccount()` (devnet/testnet). |
| 117 | - **Coin / token / object / ANS / staking:** `aptos.coin.*`, `aptos.digitalAsset.*`, `aptos.fungibleAsset.*`, |
| 118 | `aptos.object.*`, `aptos.ans.*`, `aptos.staking.*`. |
| 119 | |
| 120 | --- |
| 121 | |
| 122 | ## Common mistakes |
| 123 | |
| 124 | | Mistake | Correct approach | |
| 125 | | ---------------------------------- | -------------------------------------------------------------------- | |
| 126 | | Creating Aptos in every function | One singleton; pass `aptos` or import from shared module | |
| 127 | | Using devnet for persistent dev | Prefer testnet; devnet resets | |
| 128 | | Custom URLs without Network.CUSTOM | Set `network: Network.CUSTOM` when providing fullnode/indexer/faucet | |
| 129 | | Forgetting http2: false on Bun | Set `clientConfig: { http2: false }` for Bun | |
| 130 | |
| 131 | --- |
| 132 | |
| 133 | ## References |
| 134 | |
| 135 | - SDK: `src/api/aptos.ts`, `src/api/aptosConfig.ts` |
| 136 | - Pattern: [TYPESCRIPT_SDK.md](../../../../patterns/fullstack/TYPESCRIPT_SDK.md) |
| 137 | - Related: [ts-sdk-account](../ts-sdk-account/SKILL.md), [ts-sdk-transactions](../ts-sdk-transactions/SKI |