Generating TS Clients
Generate TypeScript clients from Anchor IDL for typed account resolution, instruction builders, and error decoding.
Recipe
anchor build
# Legacy Anchor client
anchor idl parse -f target/idl/my_program.json -o app/src/idl.tsimport { Program, AnchorProvider } from "@coral-xyz/anchor";
import idl from "./idl/my_program.json";
const program = new Program(idl as MyProgram, provider);
await program.methods.initialize().accounts({ payer }).rpc();When to reach for this: You build dApps or integration tests against Anchor programs.
Working Example
import { AnchorProvider, Wallet, Program } from "@coral-xyz/anchor";
import { Connection, Keypair } from "@solana/web3.js";
import idl from "../target/idl/counter.json";
const connection = new Connection("http://127.0.0.1:8899");
const wallet = new Wallet(Keypair.generate());
const provider = new AnchorProvider(connection, wallet, {});
const program = new Program(idl, provider);
await program.methods
.increment()
.accounts({
counter: counterPda,
authority: wallet.publicKey,
})
.rpc();What this demonstrates:
- IDL JSON is the contract between program and client
- methods.* builders encode instruction data
- accounts() maps IDL account names
- types from IDL for account decoding
Deep Dive
Modern Stack
For greenfield apps, prefer Codama + @solana/kit 7.0.0 generated clients (see next article). Anchor TS client remains common in anchor test workflows.
Publishing
Ship IDL npm package or commit generated types in CI.
Gotchas
- IDL not updated in client repo - Silent layout bugs.. Fix: Automate IDL copy in CI.
- Account name typos in .accounts() - Runtime failures.. Fix: Use generated types where possible.
- Wrong cluster program id in IDL - Transactions sent to wrong program.. Fix: Per-cluster IDL or override address.
- Mixing web3.js versions - Dependency conflicts.. Fix: Align with Anchor peer deps or migrate to kit.
- Skipping simulation - Poor UX on errors.. Fix: Simulate before wallet sign.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
| Codama + @solana/kit | Modern kit-native clients | Legacy anchor test stack |
| Hand-rolled instruction bytes | Minimal deps | Anchor IDL available |
| anchorpy (Python) | Python bots | TypeScript dApps |
FAQs
Anchor version?
0.32.1 across advanced topics.
When to use zero_copy?
Large fixed layouts when Borsh CU too high.
Is realloc reversible?
Not easily; plan forward growth.
Are remaining accounts in IDL?
No; document separately.
declare_program! needs what?
Callee IDL JSON at compile time.
Can Anchor CPI to Pinocchio?
Yes with correct account metas.
Feature flags and IDL?
Avoid different IDL per feature on mainnet.
How to measure CU?
LiteSVM, Surfpool, devnet simulation.
Custom account types common?
No; last resort.
Next steps?
See Related links for ts client.
Related
- Codama from an Anchor IDL - kit codegen
- The Anchor TS Client - client patterns
- IDL Versioning - sync
Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.