#!/usr/bin/env bash # Files API lifecycle with curl (GA, no beta header): upload a text file (multipart), list, retrieve metadata, # reference it as a document {type:"file", file_id} in a Messages call, try to download (400 for uploads), delete. # STATUS: LIVE_VERIFIED 2026-09-18 (claude-haiku-4-5-20251001): upload 200, list 200, retrieve 200, message 200, # content 400 "not downloadable", delete 200 (~$0.0001). # Run: bash examples/anthropic/files/files_lifecycle.sh set -eo pipefail cd "$(dirname "$0")/../../.." && source scripts/lib.sh OUTF="$(mktemp)"; trap 'rm -f "$OUTF"' EXIT # `ant` sets ANT_STATUS; calling it inside $(...) would lose it, so write the body to a file instead. call() { ant "$@" > "$OUTF"; OUT="$(cat "$OUTF")"; } MODEL="${ANTHROPIC_MODEL:-claude-haiku-4-5-20251001}" TMP="$(mktemp -d)"; printf 'The sky is blue. Grass is green. Atlas Files API example.' > "$TMP/atlas.txt" # Upload: multipart part "file" (Content-Type on the part optional), optional expires_in_seconds (3600..7776000). UP="$(curl -sS -o "$TMP/up.json" -w '%{http_code}' https://api.anthropic.com/v1/files \ -H "x-api-key: $ANTHROPIC_API_KEY" -H "anthropic-version: $ANTHROPIC_VERSION" \ -F "file=@$TMP/atlas.txt;type=text/plain" -F "expires_in_seconds=3600")" FID="$(jq -r .id "$TMP/up.json")"; echo "upload HTTP $UP: $(jq -c '{id,mime_type,size_bytes,downloadable,expires_at}' "$TMP/up.json")" log_request anthropic POST /v1/files "$UP" 0 "example files/files_lifecycle.sh upload" call 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" call GET "/v1/files/$FID"; echo "$OUT" | jq -c '{id,filename}'; log_request anthropic GET "/v1/files/{file_id}" "$ANT_STATUS" 0 "example files retrieve" call POST /v1/messages -d "$(jq -n --arg m "$MODEL" --arg f "$FID" '{model:$m, max_tokens:20, messages:[{role:"user", content:[ {type:"document", source:{type:"file", file_id:$f}}, {type:"text", text:"What color is grass? One word."}]}]}')" echo "message HTTP $ANT_STATUS: $(echo "$OUT" | jq -c '{text: .content[0].text, input_tokens: .usage.input_tokens}')" log_request anthropic POST /v1/messages "$ANT_STATUS" 0.0001 "example files/files_lifecycle.sh message with file_id $MODEL" call GET "/v1/files/$FID/content"; echo "$OUT" | head -c 200; echo; echo "content HTTP $ANT_STATUS (400 expected for uploaded files)" log_request anthropic GET "/v1/files/{file_id}/content" "$ANT_STATUS" 0 "example files download (uploaded file)" call DELETE "/v1/files/$FID"; echo "$OUT" | jq -c .; log_request anthropic DELETE "/v1/files/{file_id}" "$ANT_STATUS" 0 "example files delete" rm -rf "$TMP"