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.
import * as anchor from "@anchor-lang/anchor";import { Program } from "@anchor-lang/anchor";import { MyProgram } from "./target/types/my_program";// Access program from workspaceconst program = anchor.workspace.MyProgram as Program<MyProgram>;// TypeScript types are automatically inferredconst tx = await program.methods.initialize().rpc();
The workspace converts program names to camelCase for flexible access:
// All of these access the same program:anchor.workspace.myProgramanchor.workspace.MyProgramanchor.workspace["my-program"]anchor.workspace["my_program"]
import * as anchor from "@anchor-lang/anchor";import { Program } from "@anchor-lang/anchor";import { MyProgram } from "../target/types/my_program";import { assert } from "chai";describe("my-program", () => { // Configure the client to use the local cluster anchor.setProvider(anchor.AnchorProvider.env()); const program = anchor.workspace.MyProgram as Program<MyProgram>; it("initializes", async () => { const tx = await program.methods.initialize().rpc(); console.log("Transaction signature:", tx); });});
import * as anchor from "@anchor-lang/anchor";const tokenProgram = anchor.workspace.TokenProgram;const nftProgram = anchor.workspace.NftProgram;const stakingProgram = anchor.workspace.StakingProgram;// All programs share the same providerconst provider = anchor.getProvider();
import { Program } from "@anchor-lang/anchor";import { MyProgram } from "../target/types/my_program";// TypeScript knows all available methods and accountsconst program = anchor.workspace.MyProgram as Program<MyProgram>;// Autocomplete works for instruction namesconst tx = await program.methods .initialize() .accounts({ // Autocomplete works for account names myAccount: myAccountPubkey, }) .rpc();// Autocomplete works for account typesconst account = await program.account.myAccount.fetch(address);console.log(account.balance); // Type-safe access to fields
import * as anchor from "@anchor-lang/anchor";import { Program } from "@anchor-lang/anchor";import { MyProgram } from "../target/types/my_program";describe("my-program", () => { let program: Program<MyProgram>; before(async () => { // Set up provider for desired cluster const provider = anchor.AnchorProvider.local(); anchor.setProvider(provider); // Load program from workspace program = anchor.workspace.MyProgram; }); it("works on localnet", async () => { const tx = await program.methods.initialize().rpc(); assert.ok(tx); });});
The workspace object is designed to work seamlessly with the Anchor CLI workflow:
# Build programs and generate IDLsanchor build# IDLs are output to target/idl/# TypeScript types are output to target/types/# Run tests that use workspaceanchor test
Use the generated TypeScript types for type safety:
// Good - type-safeconst program = anchor.workspace.MyProgram as Program<MyProgram>;// Bad - no type checkingconst program = anchor.workspace.MyProgram;
Run anchor build First
Ensure your programs are built before accessing the workspace:
anchor buildanchor test
Use in Tests, Not Production
The workspace is ideal for testing but shouldn’t be used in production clients:
// Good - test environmentdescribe("tests", () => { const program = anchor.workspace.MyProgram;});// Bad - production app// Use explicit Program constructor insteadconst program = new Program(idl, provider);
Handle Missing Programs Gracefully
Check if a program exists before using it:
try { const program = anchor.workspace.MyProgram; // Use program...} catch (error) { console.error("Program not found. Run 'anchor build' first.");}