For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to page URLs.
Skip to content

Node.js backend

How to use the SDK in a Node.js server environment with per-request storage isolation.

The SDK works in Node.js 22+ with the same API as in the browser. The main difference is storage isolation for concurrent requests, which you handle with asyncLocalStorage.

The auth / RELAYER_API_KEY shown below is for the Zama-hosted mainnet relayer. The Sepolia testnet relayer needs no key — omit auth on testnet.

Steps

1. Install packages

npm install @zama-fhe/sdk viem

2. Create the config with a node() relayer

The node() transport runs FHE operations through @fhevm/sdk. Chain data comes from the preset, so a bare call is all you need; it also accepts an optional @fhevm/sdk options object (batchRpcCalls, fheEncryptionKey) for per-client tuning.

import { 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() },
});

const sdk = new ZamaSDK(config);

3. Choose a storage backend

For scripts and single-user CLIs, memoryStorage is the simplest option (shown above).

For servers handling multiple users concurrently, use asyncLocalStorage instead — see the next step.

4. Isolate per-request state with asyncLocalStorage

On a server where each HTTP request belongs to a different user, you need per-request transport key pair isolation. asyncLocalStorage wraps Node.js AsyncLocalStorage to scope storage to the current async context.

Each call to asyncLocalStorage.run() creates a fresh storage scope. Concurrent requests never share transport key pair state.

5. Create tokens and operate

The token API is identical to the browser SDK:

See the Token Operations reference for the full API.

6. Use direct API key auth

In a server environment, you can authenticate with the relayer directly — there is no browser to leak the key to. Pass auth on the chain definition:

The auth field supports two modes. For the Zama-hosted relayer, use ApiKeyHeader — it's the only mode the hosted endpoint accepts. BearerToken is for self-hosted relayers or proxied setups where you control the auth layer (see the Authentication guide).

Mode
Shape
Use it when

API key header

{ __type: "ApiKeyHeader", value: "your-key" }

Zama-hosted relayer (required), or default

Bearer token

{ __type: "BearerToken", token: "your-token" }

Self-hosted relayer with a bearer auth layer

Behind your own proxy, ApiKeyHeader also takes an optional header naming the header your proxy reads to authenticate the SDK.

7. Clean up on shutdown

Release the SDK's resources when your process exits:

8. (Optional) Use a custom signer

If you are using a transaction relayer (e.g. OpenZeppelin Defender) instead of a local wallet, implement the GenericSigner and GenericProvider interfaces and use the generic createConfig from @zama-fhe/sdk:

The signer handles signTypedData and writeContract; the provider handles readContract, waitForTransactionReceipt, getChainId, and getBlockTimestamp. See GenericSigner for the full interface.

Next steps

Last updated