Files
chrome-use/examples/demo/app/api/browse/route.ts
T
Chris Tateandctate cc3c70dc86 next.js example (#694)
* next.js guide

* better

* shadcn

* fixes

* fix: correct screenshot test assertion to check path instead of base64

The daemon returns { path: savePath } for screenshot commands, not base64.

* fix: cross-platform Chrome detection and gitignore hardening

- Replace hardcoded macOS Chrome path with findLocalChrome() that
  searches common paths on macOS, Linux, and WSL, with a clear error
  message when no Chrome is found.
- Add .env and .env*.local to .gitignore to prevent accidental
  secret commits.

* fix: correct Vercel deploy button repo URL to vercel-labs/agent-browser

* clean up

* demo

* next page

---------

Co-authored-by: ctate <366502+ctate@users.noreply.github.com>
2026-03-09 15:24:44 -05:00

45 lines
1.2 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import * as ab from "@/lib/agent-browser";
/**
* POST /api/browse
*
* Programmatic API route for browser automation.
* Uses @sparticuz/chromium + puppeteer-core directly in the function.
*
* Body: { "action": "screenshot", "url": "https://example.com" }
* Or: { "action": "snapshot", "url": "https://example.com" }
*/
export async function POST(req: NextRequest) {
try {
const body = await req.json();
const url = body.url;
if (!url) {
return NextResponse.json({ error: "Provide a 'url'" }, { status: 400 });
}
if (body.action === "screenshot") {
const result = await ab.screenshotUrl(url, {
fullPage: body.fullPage,
});
return NextResponse.json(result);
}
if (body.action === "snapshot") {
const result = await ab.snapshotUrl(url, {
selector: body.selector,
});
return NextResponse.json(result);
}
return NextResponse.json(
{ error: "Provide 'action' as 'screenshot' or 'snapshot'" },
{ status: 400 },
);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
return NextResponse.json({ error: message }, { status: 502 });
}
}