The Anchor TS Client
Use Anchor's TypeScript Program for RPC calls when maintaining legacy stacks.
Recipe
npm install @coral-xyz/anchor@0.32.1import { Program, AnchorProvider } from "@coral-xyz/anchor";
import idl from "./target/idl/my_program.json";When to reach for this:
- Brownfield Anchor apps not yet migrated to Codama.
- Quick prototypes already using Anchor Provider.
- Teams familiar with
.methods.fluent API.
Working Example
import { AnchorProvider, Program, Wallet } from "@coral-xyz/anchor";
import { createSolanaRpc } from "@solana/kit";
import idl from "../target/idl/my_program.json";
// Bridge: use kit RPC where possible; Anchor Provider for legacy Program
const rpc = createSolanaRpc(process.env.RPC_URL!);
// const provider = new AnchorProvider(connection, wallet, {});
// const program = new Program(idl as MyIdl, provider);
// await program.methods.increment().accounts({ counter }).rpc();What this demonstrates:
- IDL JSON from
anchor build(0.32.1) drives account layouts. .methodsbuilds and sends transactions in one call.- New modules should prefer Codama kit output.
Deep Dive
How It Works
- Anchor deserializes accounts using IDL definitions.
- Provider wraps wallet and connection for signing.
- Event parsing and error mapping come from IDL metadata.
- Migrating to kit means replacing Provider sends with message pipeline.
Gotchas
- IDL out of sync - runtime encode errors. Fix: rebuild + regenerate on every program change.
- web3.js PublicKey mixed with kit Address - dual types. Fix: migrate per feature flag.
- Implicit .rpc() sends - less control over simulation. Fix: use .transaction() + kit send for UX.
- Wrong program ID in IDL - calls hit wrong deployment. Fix: verify declare_id and Anchor.toml.
- Client-side IDL import size - large IDLs bloat bundle. Fix: split or lazy load.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
| Codama + kit | Greenfield clients | Frozen legacy code |
| Raw kit + codecs | Native programs | Anchor-heavy teams |
FAQs
Anchor 0.32.1 IDL?
Run anchor build; point Codama at target/idl JSON.
Regenerate when?
Every on-chain interface change in CI.
kit typed clients?
Codama renders @solana/kit native functions.
gill required?
Optional sugar atop kit.
Next.js?
Import generated clients in client components for writes.
Testing?
Surfpool + devnet integration tests.
web3.js?
Do not mix with generated kit clients.
Multiple programs?
One Codama config per program or monorepo workspace.
Account fetch?
Use generated decoders with getAccountInfo.
Errors?
Map program error codes from IDL metadata.
Related
- Codama - kit-native codegen
- Typed Clients Basics - overview
- Generating TS Clients - pipeline
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.