Generating Clients
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.
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.
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.
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:
$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.
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
Generate Clients
Once the configuration file is created, run the generation command to generate the clients:
$npx codama run --all
This will generate the program clients in file paths specified in the codama configuration file.
{"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}]}}}
Not all generated files are shown in corresponding code snippets.
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.
Is this page helpful?