> ## 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.

# Minimal TypeScript

> A single Node.js script that opens a Cloak account and shields, sends and withdraws

The smallest complete integration: one file, run with `tsx`. It opens the account from a key the
script holds, then shields, sends privately and withdraws.

## Prerequisites

* Node 22 or later.
* A mainnet RPC URL.
* A Solana keypair file holding a few USDC and a little SOL. Use a wallet you can afford to
  experiment with: Cloak has no test network.

## Setup

```bash theme={null}
mkdir cloak-minimal && cd cloak-minimal
npm init -y && npm pkg set type=module
npm install @cloak.dev/sdk@^0.2.20 @solana/kit
npm install -D tsx
```

## The script

```ts title="index.ts" theme={null}
import { readFileSync } from "node:fs";
import { createCloak, isCloakCallError, memoryStorage, signerFromSecretKey } from "@cloak.dev/sdk";

const rpc = process.env.SOLANA_RPC_URL;
const keypairPath = process.env.KEYPAIR_PATH;
if (!rpc || !keypairPath) throw new Error("Set SOLANA_RPC_URL and KEYPAIR_PATH.");

const wallet = await signerFromSecretKey(
  Uint8Array.from(JSON.parse(readFileSync(keypairPath, "utf8")) as number[]),
);
const cloak = await createCloak({ wallet, rpc, storage: memoryStorage() });
console.log("Cloak address:", cloak.address);
console.log("Balance before:", await cloak.balance());

try {
  // Read-only: what a withdrawal would cost. Moves nothing.
  console.log("Quote:", await cloak.quote({ to: wallet.address, token: "USDC", amount: "1" }));

  // 1. Shield 2 USDC from the wallet. The only step the wallet signs and pays for.
  console.log(await cloak.shield({ from: "solana", token: "USDC", amount: "2" }));

  // 2. Send 1 USDC privately. To yourself here; pass someone's cloak_ address instead.
  console.log(await cloak.send({ to: cloak.address, token: "USDC", amount: "1" }));

  // 3. Withdraw 1 USDC back to the wallet. Exactly 1 arrives; the fee comes on top.
  console.log(await cloak.withdraw({ to: wallet.address, token: "USDC", amount: "1" }));

  console.log("Balance after:", await cloak.balance());
} catch (e) {
  // Every error names its code, whether a retry can work, and where the money is.
  console.error(isCloakCallError(e) ? String(e) : e);
  process.exitCode = 1;
}
```

## Run it

```bash theme={null}
export SOLANA_RPC_URL="https://your-mainnet-rpc"
export KEYPAIR_PATH="$HOME/.config/solana/id.json"
npx tsx index.ts
```

## Expected output

The account and quote lines below were observed on mainnet on 2026-11-30 with a fresh key. Your
address differs, and the fee follows the pool's live config.

```
Cloak address: cloak_2CSg3VMq7Snz91UdkKurr5rqH8enSwvwUJs3ZFoT4ggfTtYLVy1CNXQmVb1F7Dupsig3D4wwHhGPFtRsgvxYg5LLrwfQJV
Balance before: { USDC: '0', USDT: '0', SOL: '0', BRS: '0' }
Quote: { arrives: { token: 'USDC', atLeast: '1', network: 'solana' }, fee: '…' }
{ status: 'shielded', token: 'USDC', amount: '2', fee: '0', txs: { solana: '…' }, notes: { spent: 0, created: 1 } }
{ status: 'sent', token: 'USDC', amount: '1', fee: '0', txs: { solana: '…' }, notes: { spent: 1, created: 2 } }
{ status: 'withdrawn', token: 'USDC', amount: '1', fee: '…', txs: { solana: '…' }, notes: { … } }
Balance after: { USDC: '…', USDT: '0', SOL: '0', BRS: '0' }
```

The first shield also downloads and verifies the circuit files, so it takes longer than the rest.
With nothing shielded yet, a withdrawal fails fast with `CloakError [balance.insufficient]` and
moves nothing.

## Next

* [Backend orchestration](https://docs.cloak.ag/examples/backend-orchestration.md): the same calls as a service.
* [createCloak](https://docs.cloak.ag/sdk/create-cloak.md): every call and option.


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