Skip to main content
Anchor provides a TypeScript client library (@anchor-lang/core) that simplifies the process of interacting with Solana programs from JavaScript or TypeScript applications.
The @anchor-lang/core library is only compatible with the legacy version (v1) of @solana/web3.js and @solana/spl-token. It is not compatible with the new version (v2) of @solana/web3.js.

Installation

Install the Anchor client library along with Solana web3.js:

Core Concepts

The Anchor TypeScript client is built around three main concepts:

Program

The main interface for interacting with on-chain programs

Provider

Combines connection and wallet for signing and sending transactions

Workspace

Auto-discovers programs in Anchor projects (Node.js only)

Creating a Program Instance

To interact with an Anchor program, you need to create a Program instance using the program’s IDL file.

Constructor Signature

Frontend/React Example

When integrating with a frontend using the Solana wallet adapter:

Without Wallet

You can create a Program instance without a wallet for read-only operations:

Fetch IDL from Chain

If an IDL has been deployed to the blockchain using anchor idl init:

AnchorProvider

The AnchorProvider combines a Solana connection and wallet to handle transaction signing and sending.

API

Example: Node.js Provider

Workspace (Node.js Only)

In Anchor projects, you can use the workspace object to automatically discover and load programs:
The workspace automatically:
  • Reads IDL files from target/idl/
  • Parses Anchor.toml for program configuration
  • Creates typed Program instances

Invoking Instructions

The program.methods namespace provides a builder API for creating and sending transactions.

Builder Pattern

1

Start with .methods

Call the instruction name with its arguments
2

Provide accounts with .accounts()

Pass required accounts (PDAs and common accounts are often auto-resolved)
3

Add signers with .signers() (optional)

Include additional signers beyond the wallet
4

Execute

Choose how to execute: .rpc(), .transaction(), or .instruction()

Execution Methods

Builds, signs, and sends the transaction in one call. Returns the transaction signature.
Best for: Simple, single-instruction transactions

Additional Options

The methods builder supports additional configuration:

Fetching Accounts

The program.account namespace provides methods to fetch and deserialize program accounts.

Fetch Single Account

Fetch Multiple Accounts

Fetch All Accounts

Fetch with Filters

Use memcmp (memory compare) to filter accounts. Remember: the first 8 bytes are the account discriminator.

Event Listeners

Subscribe to program events emitted via logs:

Program Properties

The Program instance exposes useful properties:

Complete Example

Here’s a complete example demonstrating common operations:
example.ts

TypeScript Type Safety

When you run anchor build, Anchor generates TypeScript types in target/types/:

API Reference

Program Class

MethodsBuilder Class

Next Steps

Rust Client

Learn about the Rust client library

Program IDL

Understand the IDL format