CPI Basics in Anchor
8 examples to get you started with Anchor CPIs - 5 basic and 3 intermediate.
Search across all documentation pages
8 examples to get you started with Anchor CPIs - 5 basic and 3 intermediate.
Cargo.tomlSystem program CPI moves lamports between accounts.
use anchor_lang::system_program::{self, Transfer};
system_program::transfer(
CpiContext::new(
ctx.accounts.system_program.to_account_info(),
Transfer {
from: ctx.accounts.payer.to_account_info(),
to: ctx.accounts.recipient.to_account_info(),
},
),
lamports,
)?;? to propagate ProgramErroranchor-spl wraps token program invoke. Use transfer_checked - token::transfer / Transfer are deprecated because they carry no mint or decimals.
use anchor_spl::token_interface::{self, TransferChecked};
token_interface::transfer_checked(
CpiContext::new(
ctx.accounts.token_program.to_account_info(),
TransferChecked {
from: ctx.accounts.from_ata.to_account_info(),
mint: ctx.accounts.mint.to_account_info(),
to: ctx.accounts.to_ata.to_account_info(),
authority: ctx.accounts.authority.to_account_info(),
},
),
amount,
ctx.accounts.mint.decimals,
)?;PDAs sign CPIs via seed slices.
use anchor_spl::token_interface::{self, TransferChecked};
let seeds: &[&[u8]] = &[b"vault", &[ctx.bumps.vault]];
let signer: &[&[&[u8]]] = &[seeds];
token_interface::transfer_checked(
CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
TransferChecked {
from: ctx.accounts.vault_ata.to_account_info(),
mint: ctx.accounts.mint.to_account_info(),
to: ctx.accounts.user_ata.to_account_info(),
authority: ctx.accounts.vault.to_account_info(),
},
signer,
),
amount,
ctx.accounts.mint.decimals,
)?;signer_seeds is &[&[&[u8]]] - one inner slice per signing PDA.key() result to a let before .as_ref() in a seed array, or the temporary is dropped too early (E0716)Add seeds to an existing context with .with_signer.
let cpi_ctx = CpiContext::new(
ctx.accounts.token_program.to_account_info(),
transfer_accounts, // a TransferChecked struct built earlier
);
token_interface::transfer_checked(
cpi_ctx.with_signer(signer),
amount,
ctx.accounts.mint.decimals,
)?;Pin CPI program IDs in accounts struct.
pub token_program: Program<'info, Token>,
pub system_program: Program<'info, System>,Anchor 0.32 generates CPI clients from external IDL.
// lib.rs
declare_program!(other_program);
pub fn call_other(ctx: Context<CallOther>) -> Result<()> {
other_program::cpi::do_something(
CpiContext::new(
ctx.accounts.other_program.to_account_info(),
other_program::cpi::accounts::DoSomething { /* ... */ },
),
)?;
Ok(())
}Related: CPI to Custom Programs
Forward extra accounts to CPI target.
let mut cpi_ctx = CpiContext::new(program, accounts);
cpi_ctx = cpi_ctx.with_remaining_accounts(ctx.remaining_accounts.to_vec());
other_program::cpi::advanced(cpi_ctx)?;Related: Passing Remaining Accounts
CPI failures return as ProgramError in caller.
token_interface::transfer_checked(cpi_ctx, amount, decimals)
.map_err(|_| MyError::TransferFailed)?;Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.
Reviewed by Chris St. John·Last updated Jul 16, 2026