Skip to main content

Overview

The workspace object provides a convenient way to automatically load and access Anchor programs defined in your workspace. It searches for compiled IDLs in the target/idl directory and creates Program instances on demand.
The workspace API is only available in Node.js environments, not in browsers.

Usage

How It Works

The workspace object uses a JavaScript Proxy to dynamically load programs when accessed:
  1. IDL Discovery: Searches the target/idl directory for matching IDL files
  2. Name Matching: Converts the property name to camelCase and matches against IDL filenames
  3. Program Creation: Automatically creates a Program instance with the loaded IDL
  4. Caching: Caches loaded programs for subsequent access

Workspace Type

The workspace acts as a dictionary where keys are program names (in any case format) and values are Program instances.

Program Name Resolution

The workspace converts program names to camelCase for flexible access:

Configuration

Anchor.toml Override

You can override workspace program locations in Anchor.toml:

Default Behavior

Without configuration overrides, the workspace:
  • Reads IDL files from target/idl/
  • Matches filenames against the requested program name (after converting both to camelCase)
  • Uses the program address specified in the IDL’s address field

Properties

Program
Access a program by name. The name is case-insensitive and can use any format.

Error Handling

Browser Environment

Attempting to use workspace in a browser throws an error:

IDL Not Found

If no matching IDL file is found:

IDL File Missing

If the IDL path doesn’t exist:

Usage Examples

Basic Test Setup

Multiple Programs

Type-Safe Access

Testing with Multiple Clusters

Custom IDL Path

For programs with custom IDL locations specified in Anchor.toml:

Accessing Program Properties

Integration with Anchor CLI

The workspace object is designed to work seamlessly with the Anchor CLI workflow:

Best Practices

Use the generated TypeScript types for type safety:
Ensure your programs are built before accessing the workspace:
The workspace is ideal for testing but shouldn’t be used in production clients:
Check if a program exists before using it: