SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
14 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
2.6 KB · 30 lines shellscript
Raw Blame History
1#!/usr/bin/env bash2# Files API lifecycle with curl (GA, no beta header): upload a text file (multipart), list, retrieve metadata,3# reference it as a document {type:"file", file_id} in a Messages call, try to download (400 for uploads), delete.4# STATUS: LIVE_VERIFIED 2026-09-18 (claude-haiku-4-5-20251001): upload 200, list 200, retrieve 200, message 200,5# content 400 "not downloadable", delete 200 (~$0.0001).6# Run: bash examples/anthropic/files/files_lifecycle.sh7set -eo pipefail8cd "$(dirname "$0")/../../.." && source scripts/lib.sh9OUTF="$(mktemp)"; trap 'rm -f "$OUTF"' EXIT10# `ant` sets ANT_STATUS; calling it inside $(...) would lose it, so write the body to a file instead.11call() { ant "$@" > "$OUTF"; OUT="$(cat "$OUTF")"; }12MODEL="${ANTHROPIC_MODEL:-claude-haiku-4-5-20251001}"13TMP="$(mktemp -d)"; printf 'The sky is blue. Grass is green. Atlas Files API example.' > "$TMP/atlas.txt"14# Upload: multipart part "file" (Content-Type on the part optional), optional expires_in_seconds (3600..7776000).15UP="$(curl -sS -o "$TMP/up.json" -w '%{http_code}' https://api.anthropic.com/v1/files \16  -H "x-api-key: $ANTHROPIC_API_KEY" -H "anthropic-version: $ANTHROPIC_VERSION" \17  -F "file=@$TMP/atlas.txt;type=text/plain" -F "expires_in_seconds=3600")"18FID="$(jq -r .id "$TMP/up.json")"; echo "upload HTTP $UP: $(jq -c '{id,mime_type,size_bytes,downloadable,expires_at}' "$TMP/up.json")"19log_request anthropic POST /v1/files "$UP" 0 "example files/files_lifecycle.sh upload"20call GET "/v1/files?limit=2"; echo "$OUT" | jq -c '{n: (.data|length), next_page}'; log_request anthropic GET /v1/files "$ANT_STATUS" 0 "example files list"21call GET "/v1/files/$FID"; echo "$OUT" | jq -c '{id,filename}'; log_request anthropic GET "/v1/files/{file_id}" "$ANT_STATUS" 0 "example files retrieve"22call POST /v1/messages -d "$(jq -n --arg m "$MODEL" --arg f "$FID" '{model:$m, max_tokens:20, messages:[{role:"user", content:[23  {type:"document", source:{type:"file", file_id:$f}}, {type:"text", text:"What color is grass? One word."}]}]}')"24echo "message HTTP $ANT_STATUS: $(echo "$OUT" | jq -c '{text: .content[0].text, input_tokens: .usage.input_tokens}')"25log_request anthropic POST /v1/messages "$ANT_STATUS" 0.0001 "example files/files_lifecycle.sh message with file_id $MODEL"26call GET "/v1/files/$FID/content"; echo "$OUT" | head -c 200; echo; echo "content HTTP $ANT_STATUS (400 expected for uploaded files)"27log_request anthropic GET "/v1/files/{file_id}/content" "$ANT_STATUS" 0 "example files download (uploaded file)"28call DELETE "/v1/files/$FID"; echo "$OUT" | jq -c .; log_request anthropic DELETE "/v1/files/{file_id}" "$ANT_STATUS" 0 "example files delete"29rm -rf "$TMP"30