Codama is in active development and subject to change. Refer to the linked
repositories in the quick reference for the latest information.
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 | Generate @solana/kit compatible TypeScript clients |
@codama/renderers-rust | Generate solana_sdk compatible Rust clients |
@codama/nodes-from-anchor | Convert Anchor IDLs to Codama IDLs for generating clients |
Additional Resources
| Resource | Description |
|---|---|
| Codama | Codama GitHub |
| Codama CLI | Codama CLI GitHub |
| Codama Macros | Codama Macros GitHub. Generate Codama IDL from native Rust programs with Codama macros |
| Codama Macros Example Program | Example program demonstrating Codama macros usage |
Example
This example walks through the process of generating clients from an Anchor program IDL.1
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.This example uses a counter program IDL.
View source.In an Anchor project, the IDL is found at
target/idl/<program_name>.json after
running anchor build.example/idl.json
2
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:Then create
Terminal
codama.json in the example directory with this
configuration: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.3
Generate Clients
Once the configuration file is created, run the generation command to generate
the clients:This will generate the program clients in file paths specified
in the codama configuration file.
Terminal
codama.json
Not all generated files are shown in corresponding code snippets.
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.Using the Generated Clients
With the renderer versions above, the generated clients include source code for interacting with your program but do not includepackage.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 existingpackage.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, addpackage.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.