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