Give Claude a 'save_site' tool with your schema, send the current spec plus the request, and validate what comes back.
Tool use is how you get structured output from Claude. You describe a tool with a JSON schema; Claude 'calls' it with arguments that match. Here the tool is save_site and its argument is the whole new spec.
app/api/edit/route.ts
import Anthropic from "@anthropic-ai/sdk";
import { validate } from "@/lib/validate";
const client = new Anthropic(); // reads ANTHROPIC_API_KEY
const SAVE_SITE = {
name: "save_site",
description: "Save the complete updated website spec.",
input_schema: { type: "object" as const, properties: { spec: { type: "object" } }, required: ["spec"] },
};
export async function POST(req: Request) {
const { spec, request } = await req.json();
const res = await client.messages.create({
model: "claude-sonnet-5-5",
max_tokens: 4000,
system: "You edit small-business websites. Change only what the user asks. Always reply by calling save_site with the full spec.",
tools: [SAVE_SITE],
messages: [{ role: "user", content: `Current site:\n${JSON.stringify(spec)}\n\nRequest: ${request}` }],
});
const call = res.content.find((b) => b.type === "tool_use");
const next = call ? validate((call.input as any).spec) : null;
if (!next) return Response.json({ error: "Couldn't apply that. Nothing changed." }, { status: 422 });
return Response.json({ spec: next });
}
This lesson comes with a Velour plan
Every Academy lesson, code sample and Claude Code prompt is included with Publish and Growth, along with everything in the builder.