Skip to main content
Account constraints provide automatic validation and security checks.

Initialization constraints

init

Initialize a new account.
#[account(init, payer = user, space = 8 + 8)]
pub data: Account<'info, Data>
Required with:
  • payer - Who pays for the account
  • space - Account size in bytes

init_if_needed

Initialize only if the account doesn’t exist.
#[account(init_if_needed, payer = user, space = 8 + 8)]
pub data: Account<'info, Data>

Mutability constraints

mut

Mark account as mutable.
#[account(mut)]
pub data: Account<'info, Data>

Validation constraints

has_one

Validate an account field matches another account.
#[account(has_one = authority)]
pub data: Account<'info, Data>
Checks: data.authority == authority.key()

constraint

Custom validation expression.
#[account(constraint = data.value < 100)]
pub data: Account<'info, Data>

PDA constraints

seeds

Validate PDA derivation.
#[account(
    seeds = [b"data", user.key().as_ref()],
    bump
)]
pub data: Account<'info, Data>

bump

Specify bump seed for PDA.
#[account(seeds = [b"data"], bump = data.bump)]
pub data: Account<'info, Data>

Closing constraints

close

Close account and return lamports.
#[account(mut, close = authority)]
pub data: Account<'info, Data>

Reallocation constraints

realloc

Change account size.
#[account(
    mut,
    realloc = 8 + new_size,
    realloc::payer = user,
    realloc::zero = true
)]
pub data: Account<'info, Data>

Token constraints

For SPL token accounts:
#[account(
    init,
    payer = user,
    token::mint = mint,
    token::authority = authority
)]
pub token_account: Account<'info, TokenAccount>
See SPL Integrations for token constraint details.

Complete reference

See the source code for all constraints.

Build docs developers (and LLMs) love