From 7ddda6c2cd20030c9430edad10c97b8689dacf23 Mon Sep 17 00:00:00 2001 From: Joseph Perla Date: Sat, 13 Jun 2026 02:29:38 +0300 Subject: [PATCH] Add TrustedRouter custom OpenAI client example --- README.md | 2 + .../examples/custom-client-trustedrouter.ts | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 packages/core/examples/custom-client-trustedrouter.ts diff --git a/README.md b/README.md index e9b22ce13..93eabe343 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,8 @@ cp .env.example .env nano .env # Edit the .env file to add API keys ``` +For privacy-sensitive browser automation, you can also use TrustedRouter through Stagehand's custom OpenAI-compatible client. TrustedRouter routes through an open-source, verifiable attested gateway and does not log prompts or outputs by default. See [`packages/core/examples/custom-client-trustedrouter.ts`](packages/core/examples/custom-client-trustedrouter.ts) for a runnable setup using `https://api.trustedrouter.com/v1`. + ### Installing from a branch To install Stagehand directly from a GitHub branch, install the core package subdirectory: diff --git a/packages/core/examples/custom-client-trustedrouter.ts b/packages/core/examples/custom-client-trustedrouter.ts new file mode 100644 index 000000000..60a8711e3 --- /dev/null +++ b/packages/core/examples/custom-client-trustedrouter.ts @@ -0,0 +1,47 @@ +/** + * This example shows how to use TrustedRouter through Stagehand's custom + * OpenAI-compatible client. + */ +import { CustomOpenAIClient, Stagehand } from "../lib/v3/index.js"; +import { z } from "zod"; +import OpenAI from "openai"; + +async function example() { + const stagehand = new Stagehand({ + env: "BROWSERBASE", + verbose: 1, + llmClient: new CustomOpenAIClient({ + modelName: "trustedrouter/zdr", + client: new OpenAI({ + apiKey: process.env.TRUSTEDROUTER_API_KEY, + baseURL: "https://api.trustedrouter.com/v1", + }), + }), + }); + await stagehand.init(); + + const page = stagehand.context.pages()[0]; + await page.goto("https://news.ycombinator.com"); + await stagehand.act("click on the 'new' link"); + + const headlines = await stagehand.extract( + "Extract the top 3 stories from the Hacker News homepage.", + z.object({ + stories: z.array( + z.object({ + title: z.string(), + url: z.string(), + points: z.number(), + }), + ), + }), + ); + + console.log(headlines); + + await stagehand.close(); +} + +(async () => { + await example(); +})();