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

# Your first private transaction

> Step by step: open an account, check what a payment costs, shield, send privately and withdraw, with the output to expect at each step

This walkthrough runs one complete private payment and explains what happens at each step. It uses a
Node.js script so you can see every result, and the same calls work unchanged in a browser.

**What you will do:** open a Cloak account, quote costs, shield 2 USDC, send 1 USDC privately, and
withdraw it back to a public address.

**Time:** about 10 minutes. **Cost:** the withdrawal fee (about 0.45 USDC) plus Solana network fees.

## Prerequisites

* Node 22 or later.
* A mainnet RPC URL. Any provider works; the public endpoint is rate-limited and may fail some calls.
* A Solana keypair file with at least 2 USDC and 0.01 SOL. Cloak runs on mainnet only, so use a wallet
  you can afford to experiment with.

```bash theme={null}
mkdir cloak-first && cd cloak-first
npm init -y && npm pkg set type=module
npm install @cloak.dev/sdk@^0.2.20 @solana/kit
npm install -D tsx
export SOLANA_RPC_URL="https://your-mainnet-rpc"
export KEYPAIR_PATH="$HOME/.config/solana/id.json"
```

## Step 1: Open the account

Put the setup in one module that every step imports:

```ts title="setup.ts" theme={null}
import { readFile, writeFile } from "node:fs/promises";
import { createCloak, encryptedStorage, signerFromSecretKey } from "@cloak.dev/sdk";

export const wallet = await signerFromSecretKey(
  Uint8Array.from(JSON.parse(await readFile(process.env.KEYPAIR_PATH!, "utf8")) as number[]),
);

// A file-backed store, so the SDK's records survive between steps.
const file = "./cloak-state.json";
const load = async () => JSON.parse(await readFile(file, "utf8").catch(() => "{}")) as Record<string, string>;
const store = {
  get: async (key: string) => (await load())[key] ?? null,
  set: async (key: string, value: string) => writeFile(file, JSON.stringify({ ...(await load()), [key]: value })),
};

export const cloak = await createCloak({
  wallet,
  rpc: process.env.SOLANA_RPC_URL,
  storage: encryptedStorage(store, "a passphrase of at least 12 characters"),
});
```

```ts title="step1.ts" theme={null}
import { cloak } from "./setup";

console.log("Cloak address:", cloak.address);
console.log("Balance:", await cloak.balance());
```

```bash theme={null}
npx tsx step1.ts
```

```
Cloak address: cloak_2CSg3VMq7Snz91UdkKurr5rqH8enSwvwUJs3ZFoT4ggfTtYLVy1CNXQmVb1F7Dupsig3D4wwHhGPFtRsgvxYg5LLrwfQJV
Balance: { USDC: '0', USDT: '0', SOL: '0', BRS: '0' }
```

**What happened.** The key signed the fixed Cloak account message, and the SDK derived the account
from the signature: its spending key, its viewing key and the `cloak_` address. The same key always
opens the same account. Nothing was sent to the chain.

## Step 2: Quote before moving money

```ts title="step2.ts" theme={null}
import { cloak, wallet } from "./setup";

console.log("shield:", await cloak.quote({ from: "solana", token: "USDC", amount: "2" }));
console.log("send:", await cloak.quote({ to: cloak.address, token: "USDC", amount: "1" }));
console.log("withdraw:", await cloak.quote({ to: wallet.address, token: "USDC", amount: "1" }));
```

```
shield: { arrives: { token: 'USDC', atLeast: '2', network: 'cloak' }, fee: '0' }
send: { arrives: { token: 'USDC', atLeast: '1', network: 'cloak' }, fee: '0' }
withdraw: { arrives: { token: 'USDC', atLeast: '1', network: 'solana' }, fee: '0.454363' }
```

The fee above was observed on mainnet on 2026-11-30 and follows the pool's live config.

**What happened.** Quotes are read-only. Shields and private sends carry no protocol fee. A
withdrawal pays the pool's fee, read live from chain, on top of the amount, so exactly `1` arrives.

## Step 3: Shield 2 USDC

```ts title="step3.ts" theme={null}
import { cloak } from "./setup";

const receipt = await cloak.shield({
  from: "solana",
  token: "USDC",
  amount: "2",
  onProgress: (step) => console.log("  ", step),
});
console.log(receipt);
console.log("Balance:", await cloak.balance());
```

```
   Validating transaction parameters...
   Downloading circuit files (… of 23 MB)...
   Generating zero-knowledge proof...
   Signing and sending transaction...
   Confirming transaction...
{ status: 'shielded', token: 'USDC', amount: '2', fee: '0', txs: { solana: '…' }, notes: { spent: 0, created: 1 } }
Balance: { USDC: '2', USDT: '0', SOL: '0', BRS: '0' }
```

**What happened.** The SDK created a note worth 2 USDC owned by the account, proved it, and the key
signed and paid for one Solana transaction. On chain, anyone can see 2 USDC entering the USDC pool
from this wallet. They cannot see the note.

## Step 4: Send 1 USDC privately

Send to your own `cloak_` address here, or to a friend's. Run each step with `npx tsx stepN.ts`.

```ts title="step4.ts" theme={null}
import { cloak } from "./setup";

const receipt = await cloak.send({ to: cloak.address, token: "USDC", amount: "1" });
console.log(receipt);
```

```
{ status: 'sent', token: 'USDC', amount: '1', fee: '0', txs: { solana: '…' }, notes: { spent: 1, created: 2 } }
```

**What happened.** The SDK spent the 2 USDC note and created two new ones: 1 USDC for the recipient
and 1 USDC of change. The request was signed by a key derived from the account, not by the wallet,
and was submitted for you. On chain, an observer sees a transaction in the pool with a public amount
of zero: no amount, no sender, no recipient.

## Step 5: Withdraw 1 USDC

```ts title="step5.ts" theme={null}
import { cloak, wallet } from "./setup";

const receipt = await cloak.withdraw({ to: wallet.address, token: "USDC", amount: "1" });
console.log(receipt);
console.log("Balance:", await cloak.balance());
```

```
{ status: 'withdrawn', token: 'USDC', amount: '1', fee: '0.454363', txs: { solana: '…' }, notes: { … } }
Balance: { USDC: '0.545637', USDT: '0', SOL: '0', BRS: '0' }
```

**What happened.** Exactly 1 USDC arrived at the wallet, and the fee came out of the private balance.
The withdrawal is public: the amount and the recipient are visible, but not which deposit funded it.

## What you built

| Step | Who signed | Visible on chain |
| - | - | - |
| Open account | The wallet, a message | Nothing |
| Shield | The wallet, a transaction | Wallet and amount entering the pool |
| Private send | The account's derived key | A pool transaction, amount zero |
| Withdraw | The account's derived key | Amount and recipient leaving the pool |

## Next steps

> [card] [Next.js application](https://docs.cloak.ag/examples/nextjs.md)
>
> The same flow in a browser, with a connected wallet.

> [card] [Wallet integrations](https://docs.cloak.ag/wallets/overview.md)
>
> Phantom, Privy, hardware and smart wallets.

> [card] [Notes and balances](https://docs.cloak.ag/concepts/notes-and-balances.md)
>
> Where the notes live and what to back up.

> [card] [Error codes](https://docs.cloak.ag/reference/errors.md)
>
> What each failure means and whether to retry.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.