Node.js backend
How to use the SDK in a Node.js server environment with worker threads and per-request isolation.
Steps
1. Install packages
npm install @zama-fhe/sdk viem2. Create the config with a node() relayer
node() relayerimport { createConfig } from "@zama-fhe/sdk/viem";
import { ZamaSDK, memoryStorage } from "@zama-fhe/sdk";
import { node } from "@zama-fhe/sdk/node";
import { sepolia, type FheChain } from "@zama-fhe/sdk/chains";
import { createPublicClient, createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { sepolia as sepoliaViem } from "viem/chains";
const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
const publicClient = createPublicClient({ chain: sepoliaViem, transport: http() });
const walletClient = createWalletClient({ account, chain: sepoliaViem, transport: http() });
const mySepolia = {
...sepolia,
network: "https://sepolia.infura.io/v3/YOUR_KEY",
auth: { __type: "ApiKeyHeader" as const, value: process.env.RELAYER_API_KEY! },
} as const satisfies FheChain;
const config = createConfig({
chains: [mySepolia],
publicClient,
walletClient,
storage: memoryStorage,
relayers: { [mySepolia.id]: node({ poolSize: 4 }) },
});
const sdk = new ZamaSDK(config);3. Choose a storage backend
4. Isolate per-request state with asyncLocalStorage
asyncLocalStorage5. Create tokens and operate
6. Use direct API key auth
Mode
Shape
Use it when
7. Clean up on shutdown
8. (Optional) Use a custom signer
Next steps
Last updated