> ## Documentation Index
> Fetch the complete documentation index at: https://solana.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# Generating Clients

> Complete guide to generating clients for Solana programs using the Codama CLI.

[Codama](https://github.com/codama-idl/codama) generates TypeScript and Rust
clients from a program's IDL file. The generated clients include functions for
calling instructions and fetching accounts, eliminating the need to manually
serialize and deserialize program accounts or instruction data.

<Warning>
  Codama is in active development and subject to change. Refer to the linked
  repositories in the quick reference for the latest information.
</Warning>

## Quick Reference

### CLI Commands

| Command       | Description                                          |
| ------------- | ---------------------------------------------------- |
| `codama init` | Generate a codama configuration file                 |
| `codama run`  | Generate program clients from the configuration file |

### Client Generators

| Package                                                                                                  | Description                                                                             |
| -------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
| [`@codama/renderers-js`](https://github.com/codama-idl/renderers-js)                                     | Generate [`@solana/kit`](https://www.solanakit.com) compatible TypeScript clients       |
| [`@codama/renderers-rust`](https://github.com/codama-idl/renderers-rust)                                 | Generate [`solana_sdk`](https://github.com/anza-xyz/solana-sdk) compatible Rust clients |
| [`@codama/nodes-from-anchor`](https://github.com/codama-idl/codama/tree/main/packages/nodes-from-anchor) | Convert Anchor IDLs to Codama IDLs for generating clients                               |

### Additional Resources

| Resource                                                                                                   | Description                                                                            |
| ---------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
| [Codama](https://github.com/codama-idl/codama)                                                             | Codama GitHub                                                                          |
| [Codama CLI](https://github.com/codama-idl/codama/blob/main/packages/cli/README.md)                        | Codama CLI GitHub                                                                      |
| [Codama Macros](https://github.com/codama-idl/codama-rs)                                                   | Codama Macros GitHub. Generate Codama IDL from native Rust programs with Codama macros |
| [Codama Macros Example Program](https://github.com/lorisleiva/codama-demo-2025-08/tree/main/3-from-macros) | Example program demonstrating Codama macros usage                                      |

## Example

This example walks through the process of generating clients from an
[Anchor program IDL](https://www.anchor-lang.com/docs/basics/idl).

<Steps>
  <Step title="Setup Project Files">
    To follow along, create a new directory called `example` and add the `idl.json`
    file and the `package.json` file from code snippets.

    <Info>

    This example uses a counter program IDL.
    [View source](https://github.com/solana-developers/anchor-web3js-nextjs/blob/main/program/programs/counter/src/lib.rs).

    In an Anchor project, the IDL is found at `target/idl/<program_name>.json` after
    running `anchor build`.

    </Info>

    ```json example/idl.json expandable
    {
      "address": "8PY1q5J3Aq2z7TBDLBrVjv77mYzjXSCz6iHQaFEFw9hY",
      "metadata": {
        "name": "counter",
        "version": "0.1.0",
        "spec": "0.1.0",
        "description": "Created with Anchor"
      },
      "instructions": [
        {
          "name": "decrement",
          "discriminator": [106, 227, 168, 59, 248, 27, 150, 101],
          "accounts": [
            {
              "name": "user",
              "writable": true,
              "signer": true
            },
            {
              "name": "counter",
              "writable": true,
              "pda": {
                "seeds": [
                  {
                    "kind": "const",
                    "value": [99, 111, 117, 110, 116, 101, 114]
                  }
                ]
              }
            },
            {
              "name": "vault",
              "writable": true,
              "pda": {
                "seeds": [
                  {
                    "kind": "const",
                    "value": [118, 97, 117, 108, 116]
                  },
                  {
                    "kind": "account",
                    "path": "user"
                  }
                ]
              }
            },
            {
              "name": "system_program",
              "address": "11111111111111111111111111111111"
            }
          ],
          "args": []
        },
        {
          "name": "increment",
          "discriminator": [11, 18, 104, 9, 104, 174, 59, 33],
          "accounts": [
            {
              "name": "user",
              "writable": true,
              "signer": true
            },
            {
              "name": "counter",
              "writable": true,
              "pda": {
                "seeds": [
                  {
                    "kind": "const",
                    "value": [99, 111, 117, 110, 116, 101, 114]
                  }
                ]
              }
            },
            {
              "name": "vault",
              "writable": true,
              "pda": {
                "seeds": [
                  {
                    "kind": "const",
                    "value": [118, 97, 117, 108, 116]
                  },
                  {
                    "kind": "account",
                    "path": "user"
                  }
                ]
              }
            },
            {
              "name": "system_program",
              "address": "11111111111111111111111111111111"
            }
          ],
          "args": []
        }
      ],
      "accounts": [
        {
          "name": "Counter",
          "discriminator": [255, 176, 4, 245, 188, 253, 124, 25]
        }
      ],
      "errors": [
        {
          "code": 6000,
          "name": "UnderflowError",
          "msg": "Counter cannot be decremented below zero"
        }
      ],
      "types": [
        {
          "name": "Counter",
          "type": {
            "kind": "struct",
            "fields": [
              {
                "name": "count",
                "type": "u64"
              }
            ]
          }
        }
      ]
    }
    ```

    ```json example/package.json
    {
      "name": "example",
      "version": "1.0.0",
      "type": "module",
      "devDependencies": {
        "@types/node": "^24.10.0",
        "tsx": "^4.20.6",
        "typescript": "^5.9.3"
      }
    }
    ```
  </Step>

  <Step title="Create Codama Configuration File">
    Codama uses a configuration file to specify where the IDL file is located, which
    client languages to generate, and where to output the generated code.

    Install the Codama CLI and the renderers at the versions this walkthrough
    uses:

    ```bash Terminal
    npm install codama@1.11.0 @codama/nodes-from-anchor@1.5.6 @codama/renderers-js@1.7.1 @codama/renderers-rust@1.2.10
    ```

    Then create `codama.json` in the `example` directory with this
    configuration:

    ```json example/codama.json
    {
      "idl": "idl.json",
      "before": [],
      "scripts": {
        "js": {
          "from": "@codama/renderers-js",
          "args": ["clients/js/src/generated"]
        },
        "rust": {
          "from": "@codama/renderers-rust",
          "args": [
            "clients/rust/src/generated",
            {
              "crateFolder": "clients/rust",
              "formatCode": true
            }
          ]
        }
      }
    }
    ```

    ```json example/package.json
    {
      "name": "example",
      "version": "1.0.0",
      "type": "module",
      "devDependencies": {
        "@types/node": "^24.10.0",
        "tsx": "^4.20.6",
        "typescript": "^5.9.3"
      },
      "dependencies": {
        "@codama/nodes-from-anchor": "^1.5.6",
        "@codama/renderers-js": "^1.7.1",
        "@codama/renderers-rust": "^1.2.10",
        "codama": "^1.11.0"
      }
    }
    ```

    <Info>
      `npx codama init` can write `codama.json` for you through a series of
      prompts, but it installs the latest renderers (`@codama/renderers-js` 2.x
      and `@codama/renderers-rust` 3.x) and writes a different configuration.
      Their generated output differs from the snippets below, and
      `@codama/renderers-js` 2.x also writes a `clients/js/package.json`.
    </Info>
  </Step>

  <Step title="Generate Clients">
    Once the configuration file is created, run the generation command to generate
    the clients:

    ```bash Terminal
    npx codama run --all
    ```

    This will generate the program clients in file paths specified
    in the codama configuration file.

    ```json codama.json highlight={7,12}
    {
      "idl": "idl.json",
      "before": [],
      "scripts": {
        "js": {
          "from": "@codama/renderers-js",
          "args": ["clients/js/src/generated"]
        },
        "rust": {
          "from": "@codama/renderers-rust",
          "args": [
            "clients/rust/src/generated",
            {
              "crateFolder": "clients/rust",
              "formatCode": true
            }
          ]
        }
      }
    }
    ```

    <Info>
      Not all generated files are shown in corresponding code snippets.
    </Info>

    <Note>
      With `formatCode: true`, the Rust renderer runs `cargo fmt` against
      `clients/rust/Cargo.toml`. Until that file exists, `codama run` prints a
      `(cargo-fmt) cargo metadata exited with an error` warning and leaves
      `mod.rs` unformatted. The run still succeeds. Create a minimal
      `clients/rust/Cargo.toml` first (or set `formatCode` to `false`) to avoid
      the warning.
    </Note>

    ```ts example/clients/js/src/generated/index.ts
    /**
     * This code was AUTOGENERATED using the Codama library.
     * Please DO NOT EDIT THIS FILE, instead use visitors
     * to add features, then rerun Codama to update it.
     *
     * @see https://github.com/codama-idl/codama
     */

    export * from "./accounts";
    export * from "./errors";
    export * from "./instructions";
    export * from "./pdas";
    export * from "./programs";
    ```

    ```rs example/clients/rust/src/generated/mod.rs
    //! This code was AUTOGENERATED using the codama library.
    //! Please DO NOT EDIT THIS FILE, instead use visitors
    //! to add features, then rerun codama to update it.
    //!
    //! <https://github.com/codama-idl/codama>
    //!

    pub mod accounts;
    pub mod errors;
    pub mod instructions;
    pub mod programs;
    pub mod shared;

    pub(crate) use programs::*;
    ```
  </Step>
</Steps>

## Using the Generated Clients

With the renderer versions above, the generated clients include source code for
interacting with your program but do not include `package.json` (for TypeScript)
or `Cargo.toml` (for Rust) files. `@codama/renderers-js` 2.x and later also
writes a `clients/js/package.json` that lists `@solana/kit` as a peer
dependency.

You will need to either create a new `package.json` and `Cargo.toml` files or
add the required dependencies from the generated clients to your project's
existing files.

### Integrated Into Your Application

Generate the client directly into your application's source directory and then
add the required dependencies to your existing `package.json` or `Cargo.toml`.

Use this approach when building an application where the client code doesn't
need to be shared or published.

### Standalone Packages or Crates

Create separate TypeScript packages or Rust crates for each client. For example,
add `package.json` in `clients/js/` and `Cargo.toml` in `clients/rust/`.

Use this approach to publish the client as a reusable library (npm or crates.io)
or share the client across multiple applications in a monorepo.
