---
title: Generating Clients
description:
  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.

<Callout type="warn">
  Codama is in active development and subject to change. Refer to the linked
  repositories in the quick reference for the latest information.
</Callout>

## 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).

<ScrollyCoding>

## !!steps 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.

<Callout type="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`.

</Callout>

```json !! title="example/idl.json"
{
  "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 !! title="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"
  }
}
```

## !!steps 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. Create
this configuration by running:

```terminal
$ npx codama init
```

This will prompt you with questions to create a `codama.json` file in the root
of your project and install the necessary Codama dependencies.

```shell title="example output"
Welcome to Codama!
✔ Where is your IDL located? (Supports Codama and Anchor IDLs). … idl.json
✔ Which script preset would you like to use? › Generate JavaScript client, Generate Rust client
✔ [js] Where should the JavaScript code be generated? … clients/js/src/generated
✔ [rust] Where is the Rust client crate located? … clients/rust
✔ [rust] Where should the Rust code be generated? … clients/rust/src/generated
▲ Your configuration requires additional dependencies.
▲ Install command: pnpm install @codama/nodes-from-anchor @codama/renderers-js @codama/renderers-rust
✔ Install dependencies? … yes
→ Installing
  ├─ @codama/nodes-from-anchor
  ├─ @codama/renderers-js
  └─ @codama/renderers-rust
✔ Dependencies installed successfully.

✔ Configuration file created.
  └─ Path: /example/codama.json
```

```json !! title="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 !! title="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.2.9",
    "@codama/renderers-js": "^1.4.3",
    "@codama/renderers-rust": "^1.2.7"
  }
}
```

## !!steps Generate Clients

Once the configuration file is created, run the generation command to generate
the clients:

```terminal
$ npx codama run --all
```

<WithMentions>

This will generate the program clients in [file paths](mention:path) specified
in the codama configuration file.

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

</WithMentions>

<Callout type="info">
  Not all generated files are shown in corresponding code snippets.
</Callout>

```ts !! title="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 "./programs";
```

```rs !! title="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::*;
```

</ScrollyCoding>

## Using the Generated Clients

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.

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.
