Skip to main content
The anchor-spl crate provides CPI clients and helper modules for interacting with Solana’s core SPL programs from your Anchor programs. This page covers the available modules and how to use them effectively.

Installation

Add anchor-spl to your program’s dependencies:
Update your Cargo.toml to include the IDL build feature:
Cargo.toml

Available Modules

The anchor-spl crate provides the following modules for common SPL program interactions:

Token Programs

Other SPL Programs

Token Program Integration

The most common use case is interacting with Solana’s Token Programs. The anchor-spl crate provides both program-specific modules (token, token_2022) and a unified interface (token_interface). The token_interface module provides account types that work with both the original Token Program and Token Extensions Program. This is the recommended approach for maximum compatibility:

Program-Specific Modules

For scenarios requiring specific token program features:

Associated Token Accounts

The associated_token module provides helper functions for working with Associated Token Accounts (ATAs):

Token Extensions

Token 2022 introduces extensions that add functionality to mints and token accounts. Use the token_2022_extensions module:

Metadata Integration

For NFTs and fungible tokens with metadata, use the metadata module:
The metadata module requires enabling the metadata feature in your Cargo.toml:

Memo Program

Add on-chain memos to your transactions:

Feature Flags

The anchor-spl crate uses feature flags to control which modules are included:
By default, token, token_2022, token_2022_extensions, associated_token, and mint features are enabled.

Common Patterns

Creating and Minting Tokens

Transfer with Authority

Best Practices

Always use InterfaceAccount<'info, T> and Interface<'info, TokenInterface> when you want your program to work with both Token Program and Token Extensions Program:
Always validate that token accounts belong to the expected token program:
Ensure token accounts belong to the correct mint:

Resources