> ## Documentation Index
> Fetch the complete documentation index at: https://solana.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# CPI Guard

> Enable the CpiGuard account extension to block token instructions invoked through cross-program invocations.

## What Is CPI Guard?

The Token Extension Program's `CpiGuard` account extension prevents another
onchain program from using a token account for certain token instructions
through a cross-program invocation (CPI).

When CPI guard is enabled, the Token Extension Program rejects these
instructions when another onchain program invokes them through CPI:

- `Transfer`, `TransferChecked`, and `TransferCheckedWithFee` when
  the token account owner is used as the instruction authority
- `Burn` and `BurnChecked` when the token account owner is used as the
  instruction authority
- Confidential `Transfer` and `TransferWithFee`, and confidential `Burn`, when
  the token account owner is used as the instruction authority
- `UnwrapLamports` (Token-2022 v11 and later)
- `Approve` and `ApproveChecked`
- `CloseAccount` when lamports would be sent somewhere other than the token
  account owner
- `SetAuthority` when adding or replacing the close authority

When CPI guard is enabled, these cases are still allowed:

- `Transfer`, `TransferChecked`, `TransferCheckedWithFee`,
  `Burn`, and `BurnChecked` through CPI when a delegate or permanent
  delegate signs instead of the token account owner
- `CloseAccount` through CPI when lamports are sent to the token account
  owner
- `SetAuthority` to remove an existing close authority
- `Revoke`

When the token account owner is used as the instruction authority, Token
Extension Program instructions can still be processed if the instruction is
added directly to the transaction instead of being invoked through CPI. Changing
the token account owner with `SetAuthority` stays blocked while CPI guard is
enabled, even outside CPI. `EnableCpiGuard` and `DisableCpiGuard` also
cannot be invoked through CPI.

## Before you start

The examples on this page run against a local validator at
`http://localhost:8899` (WebSocket `ws://localhost:8900`) and fund their
accounts with airdrops from it. Start one in a separate terminal with
[Surfpool](https://surfpool.run) (`surfpool start`) or
`solana-test-validator`.

Each code section below lists the packages or crates it needs. The TypeScript
examples use top-level `await`, so run them as ES modules, for example with
`npx tsx index.ts` in a project whose `package.json` sets `"type": "module"`.

## How to Enable CPI Guard

To enable CPI guard:

1. Create and initialize a mint.
2. Calculate the token account size and rent needed for a token account with
   `CpiGuard`.
3. Create and initialize a token account for the mint.
4. Invoke `EnableCpiGuard` on the token account.
5. Invoke `DisableCpiGuard` on the token account to allow those instructions
   through CPI again.

<Steps>
  <Step title="Create and initialize the mint">
    Create and initialize the mint that the token account will use.

    ```ts Example focus={20-34}
    const client = await createClient()
      .use(generatedPayer())
      .use(
        solanaRpc({
          rpcUrl: "http://localhost:8899",
          rpcSubscriptionsUrl: "ws://localhost:8900"
        })
      )
      .use(rpcAirdrop())
      .use(airdropPayer(lamports(1_000_000_000n)));

    const mint = await generateKeyPairSigner();
    const tokenAccount = await generateKeyPairSigner();

    const mintSpace = BigInt(getMintSize());
    const mintRent = await client.rpc
      .getMinimumBalanceForRentExemption(mintSpace)
      .send();

    await client.sendTransaction([
      getCreateAccountInstruction({
        payer: client.payer,
        newAccount: mint,
        lamports: mintRent,
        space: mintSpace,
        programAddress: TOKEN_2022_PROGRAM_ADDRESS
      }),
      getInitializeMintInstruction({
        mint: mint.address,
        decimals: 0,
        mintAuthority: client.payer.address,
        freezeAuthority: client.payer.address
      })
    ]);
    ```
  </Step>

  <Step title="Calculate token account size">
    Calculate the token account size for the base token account plus the
    `CpiGuard` extension. This is the size used in `CreateAccount`.

    ```ts Example focus={36-39}
    const client = await createClient()
      .use(generatedPayer())
      .use(
        solanaRpc({
          rpcUrl: "http://localhost:8899",
          rpcSubscriptionsUrl: "ws://localhost:8900"
        })
      )
      .use(rpcAirdrop())
      .use(airdropPayer(lamports(1_000_000_000n)));

    const mint = await generateKeyPairSigner();
    const tokenAccount = await generateKeyPairSigner();

    const mintSpace = BigInt(getMintSize());
    const mintRent = await client.rpc
      .getMinimumBalanceForRentExemption(mintSpace)
      .send();

    await client.sendTransaction([
      getCreateAccountInstruction({
        payer: client.payer,
        newAccount: mint,
        lamports: mintRent,
        space: mintSpace,
        programAddress: TOKEN_2022_PROGRAM_ADDRESS
      }),
      getInitializeMintInstruction({
        mint: mint.address,
        decimals: 0,
        mintAuthority: client.payer.address,
        freezeAuthority: client.payer.address
      })
    ]);

    const cpiGuardExtension = extension("CpiGuard", {
      lockCpi: false
    });
    const tokenSpace = BigInt(getTokenSize([cpiGuardExtension]));
    ```
  </Step>

  <Step title="Calculate rent">
    Calculate rent using the token account size needed for the `CpiGuard`
    extension.

    ```ts Example focus={41-43} expandable
    const client = await createClient()
      .use(generatedPayer())
      .use(
        solanaRpc({
          rpcUrl: "http://localhost:8899",
          rpcSubscriptionsUrl: "ws://localhost:8900"
        })
      )
      .use(rpcAirdrop())
      .use(airdropPayer(lamports(1_000_000_000n)));

    const mint = await generateKeyPairSigner();
    const tokenAccount = await generateKeyPairSigner();

    const mintSpace = BigInt(getMintSize());
    const mintRent = await client.rpc
      .getMinimumBalanceForRentExemption(mintSpace)
      .send();

    await client.sendTransaction([
      getCreateAccountInstruction({
        payer: client.payer,
        newAccount: mint,
        lamports: mintRent,
        space: mintSpace,
        programAddress: TOKEN_2022_PROGRAM_ADDRESS
      }),
      getInitializeMintInstruction({
        mint: mint.address,
        decimals: 0,
        mintAuthority: client.payer.address,
        freezeAuthority: client.payer.address
      })
    ]);

    const cpiGuardExtension = extension("CpiGuard", {
      lockCpi: false
    });
    const tokenSpace = BigInt(getTokenSize([cpiGuardExtension]));

    const tokenRent = await client.rpc
      .getMinimumBalanceForRentExemption(tokenSpace)
      .send();
    ```
  </Step>

  <Step title="Create and initialize the token account">
    Create the token account with the calculated space and lamports, then initialize
    it for the mint.

    ```ts Example focus={45-58} expandable
    const client = await createClient()
      .use(generatedPayer())
      .use(
        solanaRpc({
          rpcUrl: "http://localhost:8899",
          rpcSubscriptionsUrl: "ws://localhost:8900"
        })
      )
      .use(rpcAirdrop())
      .use(airdropPayer(lamports(1_000_000_000n)));

    const mint = await generateKeyPairSigner();
    const tokenAccount = await generateKeyPairSigner();

    const mintSpace = BigInt(getMintSize());
    const mintRent = await client.rpc
      .getMinimumBalanceForRentExemption(mintSpace)
      .send();

    await client.sendTransaction([
      getCreateAccountInstruction({
        payer: client.payer,
        newAccount: mint,
        lamports: mintRent,
        space: mintSpace,
        programAddress: TOKEN_2022_PROGRAM_ADDRESS
      }),
      getInitializeMintInstruction({
        mint: mint.address,
        decimals: 0,
        mintAuthority: client.payer.address,
        freezeAuthority: client.payer.address
      })
    ]);

    const cpiGuardExtension = extension("CpiGuard", {
      lockCpi: false
    });
    const tokenSpace = BigInt(getTokenSize([cpiGuardExtension]));

    const tokenRent = await client.rpc
      .getMinimumBalanceForRentExemption(tokenSpace)
      .send();

    await client.sendTransaction([
      getCreateAccountInstruction({
        payer: client.payer,
        newAccount: tokenAccount,
        lamports: tokenRent,
        space: tokenSpace,
        programAddress: TOKEN_2022_PROGRAM_ADDRESS
      }),
      getInitializeAccountInstruction({
        account: tokenAccount.address,
        mint: mint.address,
        owner: client.payer.address
      })
    ]);
    ```
  </Step>

  <Step title="Enable CPI guard">
    Enable `CpiGuard` on the token account.

    ```ts Example focus={60-65} expandable
    const client = await createClient()
      .use(generatedPayer())
      .use(
        solanaRpc({
          rpcUrl: "http://localhost:8899",
          rpcSubscriptionsUrl: "ws://localhost:8900"
        })
      )
      .use(rpcAirdrop())
      .use(airdropPayer(lamports(1_000_000_000n)));

    const mint = await generateKeyPairSigner();
    const tokenAccount = await generateKeyPairSigner();

    const mintSpace = BigInt(getMintSize());
    const mintRent = await client.rpc
      .getMinimumBalanceForRentExemption(mintSpace)
      .send();

    await client.sendTransaction([
      getCreateAccountInstruction({
        payer: client.payer,
        newAccount: mint,
        lamports: mintRent,
        space: mintSpace,
        programAddress: TOKEN_2022_PROGRAM_ADDRESS
      }),
      getInitializeMintInstruction({
        mint: mint.address,
        decimals: 0,
        mintAuthority: client.payer.address,
        freezeAuthority: client.payer.address
      })
    ]);

    const cpiGuardExtension = extension("CpiGuard", {
      lockCpi: false
    });
    const tokenSpace = BigInt(getTokenSize([cpiGuardExtension]));

    const tokenRent = await client.rpc
      .getMinimumBalanceForRentExemption(tokenSpace)
      .send();

    await client.sendTransaction([
      getCreateAccountInstruction({
        payer: client.payer,
        newAccount: tokenAccount,
        lamports: tokenRent,
        space: tokenSpace,
        programAddress: TOKEN_2022_PROGRAM_ADDRESS
      }),
      getInitializeAccountInstruction({
        account: tokenAccount.address,
        mint: mint.address,
        owner: client.payer.address
      })
    ]);

    await client.sendTransaction([
      getEnableCpiGuardInstruction({
        token: tokenAccount.address,
        owner: client.payer
      })
    ]);
    ```
  </Step>

  <Step title="Disable CPI guard">
    Disable `CpiGuard` on the token account.

    ```ts Example focus={67-72} expandable
    const client = await createClient()
      .use(generatedPayer())
      .use(
        solanaRpc({
          rpcUrl: "http://localhost:8899",
          rpcSubscriptionsUrl: "ws://localhost:8900"
        })
      )
      .use(rpcAirdrop())
      .use(airdropPayer(lamports(1_000_000_000n)));

    const mint = await generateKeyPairSigner();
    const tokenAccount = await generateKeyPairSigner();

    const mintSpace = BigInt(getMintSize());
    const mintRent = await client.rpc
      .getMinimumBalanceForRentExemption(mintSpace)
      .send();

    await client.sendTransaction([
      getCreateAccountInstruction({
        payer: client.payer,
        newAccount: mint,
        lamports: mintRent,
        space: mintSpace,
        programAddress: TOKEN_2022_PROGRAM_ADDRESS
      }),
      getInitializeMintInstruction({
        mint: mint.address,
        decimals: 0,
        mintAuthority: client.payer.address,
        freezeAuthority: client.payer.address
      })
    ]);

    const cpiGuardExtension = extension("CpiGuard", {
      lockCpi: false
    });
    const tokenSpace = BigInt(getTokenSize([cpiGuardExtension]));

    const tokenRent = await client.rpc
      .getMinimumBalanceForRentExemption(tokenSpace)
      .send();

    await client.sendTransaction([
      getCreateAccountInstruction({
        payer: client.payer,
        newAccount: tokenAccount,
        lamports: tokenRent,
        space: tokenSpace,
        programAddress: TOKEN_2022_PROGRAM_ADDRESS
      }),
      getInitializeAccountInstruction({
        account: tokenAccount.address,
        mint: mint.address,
        owner: client.payer.address
      })
    ]);

    await client.sendTransaction([
      getEnableCpiGuardInstruction({
        token: tokenAccount.address,
        owner: client.payer
      })
    ]);

    await client.sendTransaction([
      getDisableCpiGuardInstruction({
        token: tokenAccount.address,
        owner: client.payer
      })
    ]);
    ```
  </Step>
</Steps>

### Source Reference

| Item                           | Description                                                                                                                                                                                                  | Source                                                                                                                                 |
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------- |
| `CpiGuard`                     | Account extension that stores whether CPI guard is currently enabled for the token account.                                                                                                                  | [Source](https://github.com/solana-program/token-2022/blob/program%40v10.0.0/interface/src/extension/cpi_guard/mod.rs#L20-L25)         |
| `CpiGuardInstruction::Enable`  | Instruction that enables CPI guard on a token account.                                                                                                                                                       | [Source](https://github.com/solana-program/token-2022/blob/program%40v10.0.0/interface/src/extension/cpi_guard/instruction.rs#L20-L38) |
| `CpiGuardInstruction::Disable` | Instruction that disables CPI guard on a token account.                                                                                                                                                      | [Source](https://github.com/solana-program/token-2022/blob/program%40v10.0.0/interface/src/extension/cpi_guard/instruction.rs#L39-L53) |
| `process_toggle_cpi_guard`     | Processor logic used by `Enable` and `Disable` that validates account ownership, rejects CPI changes to the guard itself, and initializes `CpiGuard` if it is not already present before setting `lock_cpi`. | [Source](https://github.com/solana-program/token-2022/blob/program%40v10.0.0/program/src/extension/cpi_guard/processor.rs#L19-L52)     |

### Kit

These examples use the generated Token-2022 instructions directly.

```bash Terminal
npm install @solana/kit @solana/kit-plugin-rpc @solana/kit-plugin-signer @solana-program/system @solana-program/token-2022
```

```ts Instructions highlight={81-84,93-96}
import {
  lamports,
  createClient,
  generateKeyPairSigner,
  unwrapOption
} from "@solana/kit";
import { solanaRpc, rpcAirdrop } from "@solana/kit-plugin-rpc";
import { generatedPayer, airdropPayer } from "@solana/kit-plugin-signer";
import { getCreateAccountInstruction } from "@solana-program/system";
import {
  extension,
  fetchToken,
  getDisableCpiGuardInstruction,
  getEnableCpiGuardInstruction,
  getInitializeAccountInstruction,
  getInitializeMintInstruction,
  getMintSize,
  getTokenSize,
  isExtension,
  TOKEN_2022_PROGRAM_ADDRESS
} from "@solana-program/token-2022";
const client = await createClient()
  .use(generatedPayer())
  .use(
    solanaRpc({
      rpcUrl: "http://localhost:8899",
      rpcSubscriptionsUrl: "ws://localhost:8900"
    })
  )
  .use(rpcAirdrop())
  .use(airdropPayer(lamports(1_000_000_000n)));

const mint = await generateKeyPairSigner();
const tokenAccount = await generateKeyPairSigner();

const mintSpace = BigInt(getMintSize());
const mintRent = await client.rpc
  .getMinimumBalanceForRentExemption(mintSpace)
  .send();

await client.sendTransaction([
  getCreateAccountInstruction({
    payer: client.payer, // Account funding account creation.
    newAccount: mint, // New mint account to create.
    lamports: mintRent, // Lamports funding the mint account rent.
    space: mintSpace, // Account size in bytes for the mint account.
    programAddress: TOKEN_2022_PROGRAM_ADDRESS // Program that owns the mint account.
  }),
  getInitializeMintInstruction({
    mint: mint.address, // Mint account to initialize.
    decimals: 0, // Number of decimals for the token.
    mintAuthority: client.payer.address, // Authority allowed to mint new tokens.
    freezeAuthority: client.payer.address // Authority allowed to freeze token accounts.
  })
]);

const cpiGuardExtension = extension("CpiGuard", {
  lockCpi: false
});
const tokenSpace = BigInt(getTokenSize([cpiGuardExtension]));
const tokenRent = await client.rpc
  .getMinimumBalanceForRentExemption(tokenSpace)
  .send();

await client.sendTransaction([
  getCreateAccountInstruction({
    payer: client.payer, // Account funding account creation.
    newAccount: tokenAccount, // New token account to create.
    lamports: tokenRent, // Lamports funding the token account rent.
    space: tokenSpace, // Account size in bytes for the token account plus CpiGuard.
    programAddress: TOKEN_2022_PROGRAM_ADDRESS // Program that owns the token account.
  }),
  getInitializeAccountInstruction({
    account: tokenAccount.address, // Token account to initialize.
    mint: mint.address, // Mint for the token account.
    owner: client.payer.address // Owner allowed to control the token account.
  })
]);

await client.sendTransaction([
  getEnableCpiGuardInstruction({
    token: tokenAccount.address, // Token account that stores the CpiGuard extension.
    owner: client.payer // Token account owner authorized to enable CPI guard.
  })
]);

const enabledTokenAccount = await fetchToken(client.rpc, tokenAccount.address);
const enabledCpiGuard = (
  unwrapOption(enabledTokenAccount.data.extensions) ?? []
).find((item) => isExtension("CpiGuard", item));

await client.sendTransaction([
  getDisableCpiGuardInstruction({
    token: tokenAccount.address, // Token account that stores the CpiGuard extension.
    owner: client.payer // Token account owner authorized to disable CPI guard.
  })
]);

const disabledTokenAccount = await fetchToken(client.rpc, tokenAccount.address);
const disabledCpiGuard = (
  unwrapOption(disabledTokenAccount.data.extensions) ?? []
).find((item) => isExtension("CpiGuard", item));

console.log("\nMint Address:", mint.address);
console.log("\nToken Account:", tokenAccount.address);
console.log("\nEnabled CPI Guard:", enabledCpiGuard);
console.log("\nDisabled CPI Guard:", disabledCpiGuard);
```

### Web3.js v3

These examples use the class-based `@solana/web3.js` v3 API with
`@solana-program` instruction builders.

```bash Terminal
npm install @solana/web3.js@3 @solana/kit @solana-program/token-2022
```

```ts Instructions highlight={92-95,113-116}
import {
  Connection,
  Keypair,
  LAMPORTS_PER_SOL,
  PublicKey,
  sendAndConfirmTransaction,
  SystemProgram,
  Transaction
} from "@solana/web3.js";
import { unwrapOption } from "@solana/kit";
import {
  getDisableCpiGuardInstruction,
  getEnableCpiGuardInstruction,
  getInitializeAccountInstruction,
  getInitializeMint2Instruction,
  getMintSize,
  getTokenDecoder,
  getTokenSize,
  TOKEN_2022_PROGRAM_ADDRESS
} from "@solana-program/token-2022";

const connection = new Connection("http://localhost:8899", "confirmed");
const latestBlockhash = await connection.getLatestBlockhash();

const feePayer = await Keypair.generate();
const mint = await Keypair.generate();
const tokenAccount = await Keypair.generate();

const airdropSignature = await connection.requestAirdrop(
  feePayer.publicKey,
  5 * LAMPORTS_PER_SOL
);
await connection.confirmTransaction({
  blockhash: latestBlockhash.blockhash,
  lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
  signature: airdropSignature
});

const mintSpace = getMintSize();
const mintRent = await connection.getMinimumBalanceForRentExemption(mintSpace);

await sendAndConfirmTransaction(
  connection,
  new Transaction().add(
    SystemProgram.createAccount({
      fromPubkey: feePayer.publicKey, // Account funding account creation.
      newAccountPubkey: mint.publicKey, // New mint account to create.
      space: mintSpace, // Account size in bytes for the mint account.
      lamports: mintRent, // Lamports funding the mint account rent.
      programId: new PublicKey(TOKEN_2022_PROGRAM_ADDRESS) // Program that owns the mint account.
    }),
    getInitializeMint2Instruction({
      mint: mint.publicKey, // Mint account to initialize.
      decimals: 0, // Number of decimals for the token.
      mintAuthority: feePayer.publicKey.toBase58(), // Authority allowed to mint new tokens.
      freezeAuthority: feePayer.publicKey.toBase58() // Authority allowed to freeze token accounts.
    })
  ),
  [feePayer, mint],
  { commitment: "confirmed" }
);

const tokenAccountSpace = getTokenSize([
  { __kind: "CpiGuard", lockCpi: false }
]);
const tokenAccountRent =
  await connection.getMinimumBalanceForRentExemption(tokenAccountSpace);

await sendAndConfirmTransaction(
  connection,
  new Transaction().add(
    SystemProgram.createAccount({
      fromPubkey: feePayer.publicKey, // Account funding account creation.
      newAccountPubkey: tokenAccount.publicKey, // New token account to create.
      space: tokenAccountSpace, // Account size in bytes for the token account plus CpiGuard.
      lamports: tokenAccountRent, // Lamports funding the token account rent.
      programId: new PublicKey(TOKEN_2022_PROGRAM_ADDRESS) // Program that owns the token account.
    }),
    getInitializeAccountInstruction({
      account: tokenAccount.publicKey, // Token account to initialize.
      mint: mint.publicKey, // Mint for the token account.
      owner: feePayer.publicKey // Owner allowed to control the token account.
    })
  ),
  [feePayer, tokenAccount],
  { commitment: "confirmed" }
);

await sendAndConfirmTransaction(
  connection,
  new Transaction().add(
    getEnableCpiGuardInstruction({
      token: tokenAccount.publicKey, // Token account that stores the CpiGuard extension.
      owner: feePayer // Token account owner authorized to enable CPI guard.
    })
  ),
  [feePayer],
  { commitment: "confirmed" }
);

const enabledAccountInfo = await connection.getAccountInfo(
  tokenAccount.publicKey,
  "confirmed"
);
const enabledTokenAccount = getTokenDecoder().decode(enabledAccountInfo!.data);
const enabledCpiGuard = (
  unwrapOption(enabledTokenAccount.extensions) ?? []
).find((extension) => extension.__kind === "CpiGuard");

await sendAndConfirmTransaction(
  connection,
  new Transaction().add(
    getDisableCpiGuardInstruction({
      token: tokenAccount.publicKey, // Token account that stores the CpiGuard extension.
      owner: feePayer // Token account owner authorized to disable CPI guard.
    })
  ),
  [feePayer],
  { commitment: "confirmed" }
);

const disabledAccountInfo = await connection.getAccountInfo(
  tokenAccount.publicKey,
  "confirmed"
);
const disabledTokenAccount = getTokenDecoder().decode(
  disabledAccountInfo!.data
);
const disabledCpiGuard = (
  unwrapOption(disabledTokenAccount.extensions) ?? []
).find((extension) => extension.__kind === "CpiGuard");

console.log("\nMint Address:", mint.publicKey.toBase58());
console.log("\nToken Account:", tokenAccount.publicKey.toBase58());
console.log("\nEnabled CPI Guard:", enabledCpiGuard);
console.log("\nDisabled CPI Guard:", disabledCpiGuard);
```

### Rust

Add these dependencies to `Cargo.toml` (tested with Rust 1.89, 1.98, and 1.99):

```toml Cargo.toml
[dependencies]
anyhow = "1"
tokio = { version = "1", features = ["full"] }
solana-client = "2"
solana-commitment-config = "2"
solana-sdk = "2"
solana-system-interface = "1"
spl-token-2022-interface = "1"
```

```rust Rust highlight={106-111,128-133}
use anyhow::Result;
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_commitment_config::CommitmentConfig;
use solana_sdk::{
    program_pack::Pack,
    signature::{Keypair, Signer},
    transaction::Transaction,
};
use solana_system_interface::instruction::create_account;
use spl_token_2022_interface::{
    extension::{
        cpi_guard::{
            instruction::{disable_cpi_guard, enable_cpi_guard},
            CpiGuard,
        },
        BaseStateWithExtensions, ExtensionType, StateWithExtensions,
    },
    instruction::{initialize_account, initialize_mint},
    state::{Account, Mint},
    ID as TOKEN_2022_PROGRAM_ID,
};

#[tokio::main]
async fn main() -> Result<()> {
    let client = RpcClient::new_with_commitment(
        String::from("http://localhost:8899"),
        CommitmentConfig::confirmed(),
    );
    let fee_payer = Keypair::new();

    let airdrop_signature = client
        .request_airdrop(&fee_payer.pubkey(), 5_000_000_000)
        .await?;

    loop {
        let confirmed = client.confirm_transaction(&airdrop_signature).await?;
        if confirmed {
            break;
        }
    }

    let mint = Keypair::new();
    let token_account = Keypair::new();

    let mint_space = Mint::LEN;
    let mint_rent = client
        .get_minimum_balance_for_rent_exemption(mint_space)
        .await?;

    let create_mint_transaction = Transaction::new_signed_with_payer(
        &[
            create_account(
                &fee_payer.pubkey(), // Account funding account creation.
                &mint.pubkey(), // New mint account to create.
                mint_rent, // Lamports funding the mint account rent.
                mint_space as u64, // Account size in bytes for the mint account.
                &TOKEN_2022_PROGRAM_ID, // Program that owns the mint account.
            ),
            initialize_mint(
                &TOKEN_2022_PROGRAM_ID, // Program that owns the mint account.
                &mint.pubkey(), // Mint account to initialize.
                &fee_payer.pubkey(), // Authority allowed to mint new tokens.
                Some(&fee_payer.pubkey()), // Authority allowed to freeze token accounts.
                0, // Number of decimals for the token.
            )?,
        ],
        Some(&fee_payer.pubkey()),
        &[&fee_payer, &mint],
        client.get_latest_blockhash().await?,
    );
    client
        .send_and_confirm_transaction(&create_mint_transaction)
        .await?;

    let token_account_space =
        ExtensionType::try_calculate_account_len::<Account>(&[ExtensionType::CpiGuard])?;

    let token_account_rent = client
        .get_minimum_balance_for_rent_exemption(token_account_space)
        .await?;

    let create_token_account_transaction = Transaction::new_signed_with_payer(
        &[
            create_account(
                &fee_payer.pubkey(), // Account funding account creation.
                &token_account.pubkey(), // New token account to create.
                token_account_rent, // Lamports funding the token account rent.
                token_account_space as u64, // Account size in bytes for the token account plus CpiGuard.
                &TOKEN_2022_PROGRAM_ID, // Program that owns the token account.
            ),
            initialize_account(
                &TOKEN_2022_PROGRAM_ID, // Program that owns the token account.
                &token_account.pubkey(), // Token account to initialize.
                &mint.pubkey(), // Mint for the token account.
                &fee_payer.pubkey(), // Owner allowed to control the token account.
            )?,
        ],
        Some(&fee_payer.pubkey()),
        &[&fee_payer, &token_account],
        client.get_latest_blockhash().await?,
    );
    client
        .send_and_confirm_transaction(&create_token_account_transaction)
        .await?;

    let enable_cpi_guard_instruction = enable_cpi_guard(
        &TOKEN_2022_PROGRAM_ID, // Token program that owns the token account.
        &token_account.pubkey(), // Token account that stores the CpiGuard extension.
        &fee_payer.pubkey(), // Token account owner authorized to enable CPI guard.
        &[], // Additional multisig signers.
    )?;

    let enable_transaction = Transaction::new_signed_with_payer(
        &[enable_cpi_guard_instruction],
        Some(&fee_payer.pubkey()),
        &[&fee_payer],
        client.get_latest_blockhash().await?,
    );
    client
        .send_and_confirm_transaction(&enable_transaction)
        .await?;

    let enabled_token_account_data = client.get_account(&token_account.pubkey()).await?;
    let enabled_token_account_state =
        StateWithExtensions::<Account>::unpack(&enabled_token_account_data.data)?;
    let enabled_cpi_guard = enabled_token_account_state.get_extension::<CpiGuard>()?;

    let disable_cpi_guard_instruction = disable_cpi_guard(
        &TOKEN_2022_PROGRAM_ID, // Token program that owns the token account.
        &token_account.pubkey(), // Token account that stores the CpiGuard extension.
        &fee_payer.pubkey(), // Token account owner authorized to disable CPI guard.
        &[], // Additional multisig signers.
    )?;

    let disable_transaction = Transaction::new_signed_with_payer(
        &[disable_cpi_guard_instruction],
        Some(&fee_payer.pubkey()),
        &[&fee_payer],
        client.get_latest_blockhash().await?,
    );
    client
        .send_and_confirm_transaction(&disable_transaction)
        .await?;

    let disabled_token_account_data = client.get_account(&token_account.pubkey()).await?;
    let disabled_token_account_state =
        StateWithExtensions::<Account>::unpack(&disabled_token_account_data.data)?;
    let disabled_cpi_guard = disabled_token_account_state.get_extension::<CpiGuard>()?;

    println!("\nMint Address: {}", mint.pubkey());
    println!("\nToken Account: {}", token_account.pubkey());
    println!("\nEnabled CPI Guard: {:#?}", enabled_cpi_guard);
    println!("\nDisabled CPI Guard: {:#?}", disabled_cpi_guard);

    Ok(())
}
```

### Web3.js (legacy)

These examples use legacy `@solana/web3.js` with `@solana/spl-token`.

<Warning>
  **`@solana/web3.js` 1.x is no longer actively maintained**

  It receives critical fixes only. Use [Kit](/developers/docs/clients/official/javascript#solana-kit) for new code. To migrate existing code, follow the [Kit upgrade guide](https://www.solanakit.com/docs/upgrade-guide), or the [web3.js v3 migration guide](https://github.com/solana-foundation/solana-web3.js/blob/main/docs/web3js-v1-to-v3-migration.md) to keep the class-based API.

  To run this code, install 1.x with `npm install @solana/web3.js@1`. A plain `npm install @solana/web3.js` installs v3, which this code doesn't run on.
</Warning>

```bash Terminal
npm install @solana/web3.js@1 @solana/spl-token
```

```ts Instructions highlight={92-97,114-119}
import {
  Connection,
  Keypair,
  LAMPORTS_PER_SOL,
  sendAndConfirmTransaction,
  SystemProgram,
  Transaction
} from "@solana/web3.js";
import {
  createDisableCpiGuardInstruction,
  createEnableCpiGuardInstruction,
  createInitializeAccountInstruction,
  createInitializeMintInstruction,
  ExtensionType,
  getAccountLen,
  getAccount,
  getCpiGuard,
  getMintLen,
  TOKEN_2022_PROGRAM_ID
} from "@solana/spl-token";

const connection = new Connection("http://localhost:8899", "confirmed");
const latestBlockhash = await connection.getLatestBlockhash();

const feePayer = Keypair.generate();
const mint = Keypair.generate();
const tokenAccount = Keypair.generate();

const airdropSignature = await connection.requestAirdrop(
  feePayer.publicKey,
  5 * LAMPORTS_PER_SOL
);
await connection.confirmTransaction({
  blockhash: latestBlockhash.blockhash,
  lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
  signature: airdropSignature
});

const mintSpace = getMintLen([]);
const mintRent = await connection.getMinimumBalanceForRentExemption(mintSpace);

await sendAndConfirmTransaction(
  connection,
  new Transaction().add(
    SystemProgram.createAccount({
      fromPubkey: feePayer.publicKey, // Account funding account creation.
      newAccountPubkey: mint.publicKey, // New mint account to create.
      space: mintSpace, // Account size in bytes for the mint account.
      lamports: mintRent, // Lamports funding the mint account rent.
      programId: TOKEN_2022_PROGRAM_ID // Program that owns the mint account.
    }),
    createInitializeMintInstruction(
      mint.publicKey, // Mint account to initialize.
      0, // Number of decimals for the token.
      feePayer.publicKey, // Authority allowed to mint new tokens.
      feePayer.publicKey, // Authority allowed to freeze token accounts.
      TOKEN_2022_PROGRAM_ID // Program that owns the mint account.
    )
  ),
  [feePayer, mint],
  { commitment: "confirmed" }
);

const tokenAccountSpace = getAccountLen([ExtensionType.CpiGuard]);
const tokenAccountRent =
  await connection.getMinimumBalanceForRentExemption(tokenAccountSpace);

await sendAndConfirmTransaction(
  connection,
  new Transaction().add(
    SystemProgram.createAccount({
      fromPubkey: feePayer.publicKey, // Account funding account creation.
      newAccountPubkey: tokenAccount.publicKey, // New token account to create.
      space: tokenAccountSpace, // Account size in bytes for the token account plus CpiGuard.
      lamports: tokenAccountRent, // Lamports funding the token account rent.
      programId: TOKEN_2022_PROGRAM_ID // Program that owns the token account.
    }),
    createInitializeAccountInstruction(
      tokenAccount.publicKey, // Token account to initialize.
      mint.publicKey, // Mint for the token account.
      feePayer.publicKey, // Owner allowed to control the token account.
      TOKEN_2022_PROGRAM_ID // Program that owns the token account.
    )
  ),
  [feePayer, tokenAccount],
  { commitment: "confirmed" }
);

await sendAndConfirmTransaction(
  connection,
  new Transaction().add(
    createEnableCpiGuardInstruction(
      tokenAccount.publicKey, // Token account that stores the CpiGuard extension.
      feePayer.publicKey, // Token account owner authorized to enable CPI guard.
      [], // Additional multisig signers.
      TOKEN_2022_PROGRAM_ID // Token program that owns the token account.
    )
  ),
  [feePayer],
  { commitment: "confirmed" }
);

const enabledTokenAccount = await getAccount(
  connection,
  tokenAccount.publicKey,
  "confirmed",
  TOKEN_2022_PROGRAM_ID
);
const enabledCpiGuard = getCpiGuard(enabledTokenAccount);

await sendAndConfirmTransaction(
  connection,
  new Transaction().add(
    createDisableCpiGuardInstruction(
      tokenAccount.publicKey, // Token account that stores the CpiGuard extension.
      feePayer.publicKey, // Token account owner authorized to disable CPI guard.
      [], // Additional multisig signers.
      TOKEN_2022_PROGRAM_ID // Token program that owns the token account.
    )
  ),
  [feePayer],
  { commitment: "confirmed" }
);

const disabledTokenAccount = await getAccount(
  connection,
  tokenAccount.publicKey,
  "confirmed",
  TOKEN_2022_PROGRAM_ID
);
const disabledCpiGuard = getCpiGuard(disabledTokenAccount);

console.log("\nMint Address:", mint.publicKey.toBase58());
console.log("\nToken Account:", tokenAccount.publicKey.toBase58());
console.log("\nEnabled CPI Guard:", enabledCpiGuard);
console.log("\nDisabled CPI Guard:", disabledCpiGuard);
```
