> ## Documentation Index
> Fetch the complete documentation index at: https://luminouslabs-cc5545c6-swen-add-code-runner.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Mint Compressed Tokens

> Guide to mint compressed tokens with mintTo(), troubleshooting, and advanced configurations.

The `mintTo()` function creates compressed token accounts for recipients and increases the mint's token supply. Only the mint authority can perform this operation.

```typescript function-mint-compressed-tokens.ts theme={null}
// Mint compressed tokens - mints SPL tokens to pool, creates compressed token accounts
const transactionSignature = await mintTo(
    rpc,
    payer,
    mint, // SPL mint with token pool for compression
    recipient, // recipient address (toPubkey parameter)
    payer, // mint authority
    amount,
);
```

# Full Code Example

<Steps>
  <Step>
    ### Prerequisites

    Make sure you have dependencies and developer environment set up!

    <Accordion title="Prerequisites & Setup">
      **Dependencies**

      <Tabs>
        <Tab title="npm">
          ```bash theme={null}
          npm install @lightprotocol/stateless.js@alpha \
                      @lightprotocol/compressed-token@alpha
          ```
        </Tab>

        <Tab title="yarn">
          ```bash theme={null}
          yarn add @lightprotocol/stateless.js@alpha \
                   @lightprotocol/compressed-token@alpha
          ```
        </Tab>

        <Tab title="pnpm">
          ```bash theme={null}
          pnpm add @lightprotocol/stateless.js@alpha \
                   @lightprotocol/compressed-token@alpha
          ```
        </Tab>
      </Tabs>

      **Developer Environment**

      <Tabs>
        <Tab title="Localnet">
          By default, all guides use Localnet.

          <Tabs>
            <Tab title="npm">
              ```bash theme={null}
              npm install -g @lightprotocol/zk-compression-cli@alpha
              ```
            </Tab>

            <Tab title="yarn">
              ```bash theme={null}
              yarn global add @lightprotocol/zk-compression-cli@alpha
              ```
            </Tab>

            <Tab title="pnpm">
              ```bash theme={null}
              pnpm add -g @lightprotocol/zk-compression-cli@alpha
              ```
            </Tab>
          </Tabs>

          ```bash theme={null}
          # Start a local test validator
          light test-validator

          ## ensure you have the Solana CLI accessible in your system PATH
          ```

          ```typescript theme={null}
          // createRpc() defaults to local test validator endpoints
          import {
            Rpc,
            createRpc,
          } from "@lightprotocol/stateless.js";

          const connection: Rpc = createRpc();

          async function main() {
            let slot = await connection.getSlot();
            console.log(slot);

            let health = await connection.getIndexerHealth(slot);
            console.log(health);
            // "Ok"
          }

          main();
          ```
        </Tab>

        <Tab title="Devnet">
          Replace `<your-api-key>` with your actual API key. [Get your API key here](https://www.helius.dev/zk-compression), if you don't have one yet.

          ```typescript theme={null}
          import { createRpc } from "@lightprotocol/stateless.js";

          // Helius exposes Solana and Photon RPC endpoints through a single URL
          const RPC_ENDPOINT = "https://devnet.helius-rpc.com?api-key=<your_api_key>";
          const connection = createRpc(RPC_ENDPOINT, RPC_ENDPOINT, RPC_ENDPOINT);

          console.log("Connection created!");
          console.log("RPC Endpoint:", RPC_ENDPOINT);
          ```
        </Tab>
      </Tabs>
    </Accordion>
  </Step>

  <Step>
    ### Minting Compressed Tokens

    Run this script to mint compressed tokens to a recipient!

    ```typescript mint-compressed-tokens.ts highlight={35-44} expandable theme={null}
    // 1. Setup funded payer and connect to local validator
    // 2. Create SPL mint with token pool for compression
    // 3. Call mintTo() with mint, recipient, and amount - mint SPL tokens to pool and create compressed token accounts
    // 4. Verify via getCompressedTokenAccountsByOwner

    import { Keypair, PublicKey } from '@solana/web3.js';
    import { createRpc } from '@lightprotocol/stateless.js';
    import { createMint, mintTo } from '@lightprotocol/compressed-token';

    async function mintCompressedTokens() {
        // Step 1: Setup funded payer and connect to local validator
        const rpc = createRpc(); // defaults to localhost:8899
        const payer = Keypair.generate();
        const airdropSignature = await rpc.requestAirdrop(payer.publicKey, 1000000000); // 1 SOL
        await rpc.confirmTransaction(airdropSignature);

        // Step 2: Create SPL mint with token pool for compression
        const { mint, transactionSignature: mintCreateTx } = await createMint(
            rpc,
            payer,
            payer.publicKey, // mint authority
            9
        );

        console.log("Mint with token pool for compression created!");
        console.log("Mint address:", mint.toBase58());
        console.log("Create mint transaction:", mintCreateTx);

        // Generate recipient keypair
        const recipient = Keypair.generate();

        // Define amount to mint
        const mintAmount = 1_000_000_000; // 1 token with 9 decimals

        // Step 3: Call mintTo() with mint, recipient, and amount
        // Mint SPL tokens to pool and create compressed token account
        const transactionSignature = await mintTo(
            rpc,
            payer,
            mint, // SPL mint with token pool for compression
            recipient.publicKey,
            payer, // mint authority
            mintAmount
        );

        console.log("\nCompressed token minted!");
        console.log("Recipient:", recipient.publicKey.toBase58());
        console.log("Compressed Token Balance:", mintAmount / 1_000_000_000, "tokens");
        console.log("Mint token transaction:", transactionSignature);

        // Step 4: Verify via getCompressedTokenAccountsByOwner
        const tokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
            recipient.publicKey,
            { mint }
        );

        if (tokenAccounts.items.length > 0) {
            const balance = tokenAccounts.items[0].parsed.amount;    }

        return { transactionSignature, recipient: recipient.publicKey, amount: mintAmount };
    }

    mintCompressedTokens().catch(console.error);
    ```

    <Info>
      Make sure the SPL mint has a token pool for compression.<br /> The script creates this token pool for you.

      For development, you can create a new mint with token pool via [`createMint()`](/compressed-tokens/guides/how-to-create-and-register-a-mint-account-for-compression) or add a token pool to an existing mint via [`createTokenPool()`](/compressed-tokens/guides/how-to-create-compressed-token-pools-for-mint-accounts).
    </Info>
  </Step>
</Steps>

# Troubleshooting

<AccordionGroup>
  <Accordion title="TokenPool not found">
    ```typescript theme={null}
    // Error message: "TokenPool not found. Please create a compressed token
    // pool for mint: [ADDRESS] via createTokenPool().
    ```

    The mint does no have a token pool for compression. Ensure you created the mint using `createMint`.

    ```typescript theme={null}
    // Create mint with token pool for compression
    import { createMint } from '@lightprotocol/compressed-token';
    const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
    ```
  </Accordion>

  <Accordion title="TokenPool mint does not match the provided mint">
    The token pool info doesn't correspond to the mint address. Ensure you're fetching the correct pool:

    ```typescript theme={null}
    // Get the correct token pool for your mint
    const tokenPoolInfo = await getTokenPoolInfos(rpc, mint);
    ```
  </Accordion>

  <Accordion title="Amount and toPubkey arrays must have the same length">
    When minting to multiple recipients, ensure arrays are the same size.

    ```typescript theme={null}
    // Wrong: Mismatched array lengths
    const recipients = [addr1, addr2, addr3];
    const amounts = [100, 200]; // Only 2 amounts for 3 recipients

    // Correct: Same length arrays
    const recipients = [addr1, addr2, addr3];
    const amounts = [100, 200, 300]; // 3 amounts for 3 recipients
    ```
  </Accordion>
</AccordionGroup>

# Advanced Configuration

<AccordionGroup>
  <Accordion title="Mint to Multiple Recipients">
    ```typescript theme={null}
    // Mint different amounts to multiple recipients
    const recipients = [
        Keypair.generate().publicKey,
        Keypair.generate().publicKey,
        Keypair.generate().publicKey,
    ];

    const amounts = [
        1_000_000_000, // 1 token
        2_000_000_000, // 2 tokens
        500_000_000,   // 0.5 tokens
    ];

    const transactionSignature = await mintTo(
        rpc,
        payer,
        mint, // SPL mint with token pool for compression
        recipients, // array of recipients (toPubkey parameter)
        payer, // mint authority
        amounts, // array of amounts (amount parameter)
    );
    ```
  </Accordion>

  <Accordion title="With Custom Mint Authority">
    Mint tokens using a custom mint authority with `approveAndMintTo()`:

    ```typescript theme={null}
    import { approveAndMintTo } from '@lightprotocol/compressed-token';

    // Mint tokens with a separate mint authority
    const transactionSignature = await approveAndMintTo(
        rpc,
        payer,
        mint, // SPL mint with token pool for compression
        recipient.publicKey, // recipient of minted tokens (toPubkey parameter)
        mintAuthority, // mint authority
        mintAmount,
    );
    ```
  </Accordion>
</AccordionGroup>

# Next Steps

<Card title="Learn how to transfer compressed tokens." icon="chevron-right" color="#0066ff" href="/compressed-tokens/guides/how-to-transfer-compressed-token" horizontal />
