Authentication
How to authenticate with the relayer using a backend proxy or a direct API key.
Steps
1. Understand the two options
Strategy
Use when
API key location
2. Set up a backend proxy
RELAYER_API_KEY=your-api-keyimport express from "express";
import { mainnet, sepolia } from "@zama-fhe/sdk/chains";
const app = express();
app.use(express.json());
// Map chain IDs to their network config
const Configs: Record<number, typeof mainnet> = { [mainnet.id]: mainnet, [sepolia.id]: sepolia };
app.use("/api/relayer/:chainId", async (req, res) => {
const config = Configs[Number(req.params.chainId)];
if (!config) {
res.status(400).send("Unsupported chain");
return;
}
const url = new URL(req.url, config.relayerUrl);
const body = ["GET", "HEAD"].includes(req.method) ? undefined : JSON.stringify(req.body);
const response = await fetch(url, {
method: req.method,
headers: { "content-type": "application/json", "x-api-key": process.env.RELAYER_API_KEY! },
body,
// @ts-expect-error: required by the relayer
duplex: "half",
});
res.status(response.status).send(await response.text());
});
app.listen(3001);3. Configure the SDK to use your proxy
4. (Alternative) Use a direct API key for server-side apps
5. Auth methods reference
Method
How it's sent
Use it when
Next steps
Last updated