Python 88.3%
TypeScript 7.6%
Shell 4.1%
1/**2 * Files API with the Node SDK: upload a 1x1 PNG (toFile), reference it as an image {type:"file", file_id},3 * retrieve metadata, delete. No beta header needed (GA since 2026-08-19).4 * STATUS: LIVE_VERIFIED 2026-09-18 (claude-haiku-4-5-20251001): upload 200 image/png, answer "Red", delete file_deleted (~$0.0001).5 * Run: node --env-file=.env --experimental-strip-types examples/anthropic/files/files_image_reference.ts6 */7import Anthropic, { toFile } from "@anthropic-ai/sdk";8import { appendFileSync } from "node:fs";910const MODEL = process.env.ANTHROPIC_MODEL ?? "claude-haiku-4-5-20251001";11const client = new Anthropic({ maxRetries: 1 });12const log = (method: string, path: string, status: number, note: string, cost = 0) =>13 appendFileSync("reports/live-requests.jsonl", JSON.stringify({ ts: new Date().toISOString(), provider: "anthropic", method, path, status, est_cost_usd: cost, note }) + "\n");1415// 1x1 red PNG (base64)16const PNG_B64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC";17const file = await client.files.upload({ file: await toFile(Buffer.from(PNG_B64, "base64"), "pixel.png", { type: "image/png" }) });18console.log("uploaded:", file.id, file.mime_type, file.size_bytes, "downloadable=", file.downloadable);19log("POST", "/v1/files", 200, "example files/files_image_reference.ts upload");20try {21 const msg = await client.messages.create({22 model: MODEL, max_tokens: 10,23 messages: [{ role: "user", content: [{ type: "image", source: { type: "file", file_id: file.id } }, { type: "text", text: "What color is this pixel? One word." }] }],24 });25 console.log("answer:", msg.content[0].type === "text" ? msg.content[0].text : msg.content[0].type, "input_tokens:", msg.usage.input_tokens);26 log("POST", "/v1/messages", 200, `example files/files_image_reference.ts message ${MODEL}`, 0.0001);27 const meta = await client.files.retrieveMetadata(file.id);28 console.log("metadata:", meta.filename, meta.created_at);29 log("GET", "/v1/files/{file_id}", 200, "example files/files_image_reference.ts retrieve");30} finally {31 const del = await client.files.delete(file.id);32 console.log("deleted:", del.id, del.type);33 log("DELETE", "/v1/files/{file_id}", 200, "example files/files_image_reference.ts delete");34}35