← nopea.tech
ScaneaConsenteaMaskeaBundlesPricing
Frameworks

Cloudflare Workers fetch handler

Nopea is pure JS with no Node-only APIs in its call path other than local storage (which reads/writes relative to process.cwd() -- skip logging()'s file persistence on Workers and use format: "silent" or "json" piped to your own sink instead.

← All recipes
import { Maskea, retries, secrets } from "@nopeatech/maskea";
import OpenAI from "openai";

export default {
  async fetch(request: Request, env: { OPENAI_API_KEY: string }) {
    const openai = new OpenAI({ apiKey: env.OPENAI_API_KEY });
    const maskea = new Maskea().use(retries({ max: 2 })).use(secrets({ mode: "mask" }));
    const chat = maskea.wrap(openai.chat.completions.create.bind(openai.chat.completions));

    const { messages } = await request.json<{ messages: unknown }>();
    const result = await chat({ model: "gpt-4o-mini", messages });
    return Response.json(result);
  },
};

Checked against the shipped @nopeatech/maskea API. Full docs at nopea.tech/maskea/docs.

More in Frameworks

Express route handler →Fastify route handler →NestJS injectable service →Next.js App Router API route →