AnchorPy
AnchorPy generates Python clients from Anchor IDLs so backends and bots can call on-chain programs with typed instruction builders. It targets Anchor 0.32.1 IDL semantics and runs on top of solders + solana-py against Agave 4.1.1 RPC.
Search across all documentation pages
AnchorPy generates Python clients from Anchor IDLs so backends and bots can call on-chain programs with typed instruction builders. It targets Anchor 0.32.1 IDL semantics and runs on top of solders + solana-py against Agave 4.1.1 RPC.
Quick-reference recipe card - copy-paste ready.
pip install anchorpy
from anchorpy import Program, Provider, Wallet
from solana.rpc.async_api import AsyncClient
PROGRAM_ID = "<YOUR_PROGRAM_PUBKEY>"
async def load_program():
client = AsyncClient("https://api.devnet.solana.com")
wallet = Wallet.local()
provider = Provider(client, wallet)
program = await Program.at(PROGRAM_ID, provider)
return programWhen to reach for this:
anchor build.import asyncio
import json
from pathlib import Path
from anchorpy import Program, Provider, Wallet, Idl
from solana.rpc.async_api import AsyncClient
PROGRAM_ID = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
RPC = "https://api.devnet.solana.com"
async def main():
idl_path = Path("target/idl/my_program.json")
idl = Idl.from_json(idl_path.read_text())
async with AsyncClient(RPC) as client:
wallet = Wallet.local()
provider = Provider(client, wallet, opts={"preflight_commitment": "confirmed"})
program = await Program.at(PROGRAM_ID, provider, idl=idl)
# Example: fetch an account defined in the IDL
# state = await program.account["MyAccount"].fetch(pda)
# Example: send an instruction
# sig = await program.rpc["initialize"](ctx=..., signer=wallet.payer)
print("Loaded program:", program.program_id)
asyncio.run(main())What this demonstrates:
Provider couples RPC client, wallet, and default transaction options.Program.at resolves program ID and attaches IDL metadata.anchor build emits target/idl/<program>.json matching anchor-lang 0.32.1.Wallet.local() reads ANCHOR_WALLET or ~/.config/solana/id.json like the TS toolchain.| Concern | AnchorPy | @coral-xyz/anchor TS |
|---|---|---|
| IDL source | Same JSON from Anchor 0.32.1 | Same |
| Runtime | Python asyncio services | Browser / Node |
| Types | Generated Python stubs | Generated TS |
| Best for | Bots, ETL triggers, backends | dApp frontends |
# Pin IDL in repo - regenerate in CI when program changes
# anchor build && cp target/idl/my_program.json clients/python/
# Use explicit keypair in production
from solders.keypair import Keypair
from anchorpy import Wallet
secret = Keypair.from_bytes(bytes(json.load(open("bot.json"))))
wallet = Wallet(secret)
# Fetch multiple accounts concurrently
# accounts = await program.account["Position"].all()rpc calls - Anchor accounts need explicit signers. Fix: pass ctx with Signer accounts.AsyncClient throughout. Fix: no sync Client inside asyncio loops.| Alternative | Use When | Don't Use When |
|---|---|---|
Hand-packed Instruction | Tiny, stable instruction set | Large IDL surface |
@solana/kit + Codama TS | Frontend + shared types | Python-only team |
declare_program! Rust client | Off-chain Rust services | Python stack |
| Raw CPI from another program | On-chain composition | Off-chain automation |
Yes - use IDLs generated by Anchor CLI 0.32.1 / anchor-lang 0.32.1.
Use anchorpy CLI codegen from IDL when you want checked-in stubs; Program.at works dynamically too.
Not via IDL helpers - build raw instructions with solders instead.
AnchorPy exposes program_id seeds helpers aligned with Anchor conventions.
Parse transaction logs or use IDL event parsers where available.
Point provider at local validator or Surfpool 0.12.0 fork.
Instantiate one Program per program ID / IDL pair.
Map custom error codes using IDL error table - mirror TS client patterns.
Use hardware or cloud HSM wrappers around Wallet - never commit secrets.
Yes if your Anchor program and IDL include those account types.
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.
Reviewed by Chris St. John·Last updated Jul 16, 2026