Skip to content
Development
Command

/generate-idl-client

Generate TypeScript client from Solana program IDL using Codama or Anchor

From plugin
solana-ai-kit
10130 skills15 agents30 commands7 MCP
Install
> /plugin marketplace add solanabr/solana-ai-kit
> /plugin install solana-ai-kit@stbr

How it fires

How this command gets triggered: by you, by Claude, or both.

  • Fires itselfClaude auto-loads it when your prompt matches the work.
  • You can call itInvoke it directly when you want it.
  • Slash command/generate-idl-client

Context preview

What this command does when you run it.

Generate TypeScript client from Solana program IDL using Codama or Anchor

Command definition

generate-idl-client.md
description: "Generate TypeScript client from Solana program IDL using Codama or Anchor"

You are generating a TypeScript client from a Solana program's IDL. Detect the IDL source and generator, run the appropriate pipeline, and verify the output compiles.

Related Skills

  • [ext/solana-dev/skill/references/idl-codegen.md](../skills/ext/solana-dev/skill/references/idl-codegen.md) - Codama/Shank client generation patterns
  • [ext/solana-dev/skill/references/programs/anchor.md](../skills/ext/solana-dev/skill/references/programs/anchor.md) - Anchor IDL format

Step 1: Detect IDL Source

echo "Detecting IDL source..."
echo ""

# Check for Anchor IDL
if ls target/idl/*.json 2>/dev/null; then
    echo "Found Anchor IDL(s) in target/idl/"
    IDL_SOURCE="anchor"
    ls -la target/idl/*.json
fi

# Check Anchor.toml for program names
if [ -f "Anchor.toml" ]; then
    echo ""
    echo "Anchor.toml programs:"
    grep -A 10 '\[programs' Anchor.toml
fi

# Check for Shank-annotated programs
if grep -r "ShankInstruction\|ShankAccount\|shank" --include="*.rs" programs/ src/ 2>/dev/null | head -5; then
    echo ""
    echo "Shank annotations detected"
    IDL_SOURCE="${IDL_SOURCE:-shank}"
fi

# Check for Codama config
if [ -f "codama.json" ] || [ -f "codama.config.ts" ] || [ -f "codama.config.js" ]; then
    echo ""
    echo "Codama config found"
    IDL_SOURCE="${IDL_SOURCE:-codama}"
fi

# Check for existing IDL files in other locations
if ls idl/*.json 2>/dev/null || ls *.idl.json 2>/dev/null; then
    echo ""
    echo "Found IDL files in project root or idl/ directory"
fi

echo ""
echo "IDL source: ${IDL_SOURCE:-unknown}"

Step 2: Generate IDL (if needed)

Anchor IDL Generation

if [ "$IDL_SOURCE" = "anchor" ] || [ -f "Anchor.toml" ]; then
    echo "Generating Anchor IDL..."

    # Build IDL
    anchor build

    if [ $? -ne 0 ]; then
        echo "Anchor build failed. Fix errors before generating client."
        exit 1
    fi

    echo ""
    echo "Generated IDL files:"
    ls -la target/idl/*.json

    # Show IDL summary
    for idl in target/idl/*.json; do
        PROGRAM=$(basename "$idl" .json)
        INSTRUCTIONS=$(grep -c '"name"' "$idl" | head -1)
        echo "  $PROGRAM: $(cat "$idl" | python3 -c "import sys,json; d=json.load(sys.stdin); print(f'{len(d.get(\"instructions\",[]))} instructions, {len(d.get(\"accounts\",[]))} accounts')" 2>/dev/null || echo "parsed")"
    done
fi

Shank IDL Generation

if [ "$IDL_SOURCE" = "shank" ]; then
    echo "Generating IDL with Shank..."

    # Install shank-cli if needed
    if ! command -v shank >/dev/null 2>&1; then
        echo "Installing shank-cli..."
        cargo install shank-cli
    fi

    # Find program crate
    PROGRAM_DIR=$(find programs/ src/ -name "Cargo.toml" -maxdepth 2 | head -1 | xargs dirname)

    if [ -n "$PROGRAM_DIR" ]; then
        shank idl -r "$PROGRAM_DIR" -o target/idl/
        echo "Shank IDL generated in target/idl/"
    else
        echo "Could not find program crate for Shank"
    fi
fi

Step 3: Determine Client Generator

echo ""
echo "Detecting client generator..."

# Check for Codama pipeline
if [ -f "codama.json" ] || [ -f "codama.config.ts" ]; then
    GENERATOR="codama"
    echo "Using Codama pipeline"
elif [ -f "Anchor.toml" ]; then
    GENERATOR="anchor"
    echo "Using Anchor client generation"
else
    GENERATOR="codama"
    echo "Defaulting to Codama (recommended)"
fi

echo "Generator: $GENERATOR"

Step 4: Generate Client - Codama

if [ "$GENERATOR" = "codama" ]; then
    echo "Generating client with Codama..."

    # Install Codama if needed
    if ! npm ls @codama/renderers-js 2>/dev/null | grep -q "@codama"; then
        echo "Installing Codama dependencies..."
        npm install --save-dev @codama/renderers-js @codama/nodes-from-anchor
    fi

    # Determine output directory
    CLIENT_DIR="src/generated"
    if [ -d "app" ]; then
        CLIENT_DIR="app/src/generated"
    elif [ -d "web" ]; then
        CLIENT_DIR="web/src/generated"
    fi

    mkdir -p "$CLIENT_DIR"

    # If codama config exists, use it
    if [ -f "codama.config.ts" ]; then
        npx tsx codama.config.ts
    elif [ -f "codama.config.js" ]; then
        node codama.config.js
    else
        # Generate using Codama API directly
        # The agent should create a codama generation script based on the IDL
        echo "No codama config found. Create codama.config.ts or use Anchor generation."
        echo ""
        echo "Example codama.config.ts:"
        echo '
import { createFromRoot } from "@codama/nodes";
import { rootNodeFromAnchor } from "@codama/nodes-from-anchor";
import { renderJavaScriptVisitor } from "@codama/renderers-js";
import anchorIdl from "./target/idl/program_name.json";

const node = rootNodeFromAnchor(anchorIdl);
const codama = createFromRoot(node);
codama.accept(renderJavaScriptVisitor("./src/generated"));
'
    fi

    echo "Client generated in $CLIENT_DIR"
fi

Step 5: Generate Client - Anchor

if [ "$GENERATOR" = "anchor" ]; then
    echo "Generating Anchor TypeScript client..."

    # Anchor generates types automatically during build
    # Check for generated types
    if [ -d "target/types" ]; then
        echo "Anchor types found in target/types/"
        ls target/types/
    fi

    # For standalone client generation from IDL
    CLIENT_DIR="src/generated"
    if [ -d "app" ]; then
        CLIENT_DIR="app/src/generated"
    fi
    mkdir -p "$CLIENT_DIR"

    # Copy IDL for frontend consumption
    for idl in target/idl/*.json; do
        PROGRAM=$(basename "$idl" .json)
        cp "$idl" "$CLIENT_DIR/${PROGRAM}_idl.json"
        echo "Copied IDL: $CLIENT_DIR/${PROGRAM}_idl.json"
    done

    echo ""
    echo "For Anchor projects, import the IDL and use Program class:"
    echo '  import idl from "./generated/program_name_idl.json";'
    echo '  const program = new Program(idl, provider);'
fi

S

Read more
Ships withsolana-ai-kit

Production-ready Claude Code configuration for full-stack Solana development. Combines best practices from multiple sources into an agent-optimized, token-efficient config you can install and adapt to your specific project.

Get the whole plugin

Other commands on solana-ai-kit.