Typed Clients Basics
8 examples for IDL-driven TypeScript clients - 5 basic and 3 intermediate.
Prerequisites
anchor build # Anchor 0.32.1
npm install @solana/kit@7.0.0
npm install -D @codama/cli @codama/renderers-jsBasic Examples
1. Why Generate Clients?
IDL is the contract between on-chain Rust and off-chain TypeScript.
ls target/idl/my_program.json- Hand-written layouts drift from Anchor 0.32.1 structs.
- Generated codecs match discriminators exactly.
- One source of truth reduces mainnet incidents.
2. Anchor TS Client
@coral-xyz/anchor Program class still works for legacy apps.
// Prefer Codama + kit for new modules- Anchor client couples to web3.js types historically.
- Migrate program calls incrementally.
- See The Anchor TS Client.
3. Codama Pipeline
IDL → node graph → renderer → TypeScript kit client.
npx codama run --config codama.json- Commit generated output or regenerate in CI.
- Fail CI when folder dirty after build.
- Pin @codama/cli version per repo.
4. Fetch Typed Account
Generated decoders parse account data.
import { createSolanaRpc, address } from "@solana/kit";
// import { fetchMyAccount } from "./generated";
const rpc = createSolanaRpc(process.env.RPC_URL!);- Fetch + decode in one helper when using gill/Codama output.
- Handle null when account not initialized.
- See Fetching & Decoding Accounts.
5. Build Typed Instruction
Instruction builders encode discriminator + args.
// getIncrementInstruction({ account, authority }) from generated client- Builders return kit Instruction type.
- Append to transaction message with kit APIs.
- Account metas are ordered per IDL.
Intermediate Examples
6. gill Convenience
Higher-level helpers bundle kit + common programs.
npm install gillimport { createSolanaClient } from "gill";- Faster bootstrap for standard flows.
- Drop to kit for uncommon account metas.
- See gill.
7. CI Regeneration
anchor build && npx codama run && git diff --exit-code clients/- Prevents shipping stale clients.
- Pair with Surfpool integration tests.
- Document codegen in CONTRIBUTING.md.
8. Multi-Program Apps
Monorepo may host several IDLs.
{ "idl": "target/idl/program_a.json", "before": [], "scripts": { "js": "@codama/renderers-js" } }- Namespace generated folders per program.
- Avoid duplicate export names across programs.
- See Keeping Clients in 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.