Files API 讓您上傳和管理檔案以供 Claude API 使用,無需在每次請求時重新上傳內容。這在使用程式碼執行工具提供輸入(例如資料集和文件)然後下載輸出(例如圖表)時特別有用。您也可以使用 Files API 來避免在多個 API 呼叫中持續重新上傳常用的文件和圖像。您可以直接探索 API 參考,除了本指南外。
Files API 目前處於測試版。請透過我們的意見回饋表單分享您對 Files API 的體驗。
在 Messages 請求中參考 file_id 在所有支援給定檔案類型的模型中都受支援。例如,圖像在所有 Claude 3+ 模型中受支援,PDF 在所有 Claude 3.5+ 模型中受支援,以及各種其他檔案類型用於 Claude Haiku 4.5 及所有 Claude 3.7+ 模型中的程式碼執行工具。
Files API 目前在 Amazon Bedrock 或 Google Vertex AI 上不受支援。
Files API 提供了一個簡單的一次上傳、多次使用的方法來處理檔案:
file_idfile_id 而不是重新上傳內容若要使用 Files API,您需要包含測試版功能標頭:anthropic-beta: files-api-2025-04-14。
上傳檔案以在未來的 API 呼叫中參考:
上傳檔案的回應將包括:
{
"id": "file_011CNha8iCJcU1wXNR6q4V8w",
"type": "file",
"filename": "document.pdf",
"mime_type": "application/pdf",
"size_bytes": 1024000,
"created_at": "2025-01-01T00:00:00Z",
"downloadable": false
}上傳後,使用其 file_id 參考檔案:
Files API 支援對應於不同內容區塊類型的不同檔案類型:
| 檔案類型 | MIME 類型 | 內容區塊類型 | 使用案例 |
|---|---|---|---|
application/pdf | document | 文字分析、文件處理 | |
| 純文字 | text/plain | document | 文字分析、處理 |
| 圖像 | image/jpeg, image/png, image/gif, image/webp | image | 圖像分析、視覺任務 |
| 資料集、其他 | 變動 |
對於不支援作為 document 區塊的檔案類型(.csv、.txt、.md、.docx、.xlsx),將檔案轉換為純文字,並將內容直接包含在您的訊息中:
對於包含圖像的 .docx 檔案,請先將其轉換為 PDF 格式,然後使用 PDF 支援來利用內建的圖像解析。這允許使用 PDF 文件中的引用。
對於 PDF 和文字檔案,使用 document 內容區塊:
{
"type": "document",
"source": {
"type": "file",
"file_id": "file_011CNha8iCJcU1wXNR6q4V8w"
},
"title": "Document Title", // 選用
"context": "Context about the document", // 選用
"citations": {"enabled": true} // 選用,啟用引用
}對於圖像,使用 image 內容區塊:
{
"type": "image",
"source": {
"type": "file",
"file_id": "file_011CPMxVD3fHLUhvTqtsQA5w"
}
}檢索您上傳的檔案列表:
curl https://api.anthropic.com/v1/files \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: files-api-2025-04-14"檢索特定檔案的資訊:
curl https://api.anthropic.com/v1/files/file_011CNha8iCJcU1wXNR6q4V8w \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: files-api-2025-04-14"從您的工作區移除檔案:
curl -X DELETE https://api.anthropic.com/v1/files/file_011CNha8iCJcU1wXNR6q4V8w \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: files-api-2025-04-14"下載由技能或程式碼執行工具建立的檔案:
Messages API 呼叫和相關的工具使用中持續存在使用 Files API 時的常見錯誤包括:
file_id 不存在或您無法存取它/v1/messages 請求中使用 500 MB 純文字檔案)<、>、:、"、|、?、*、\、/ 或 unicode 字元 0-31){
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "File not found: file_011CNha8iCJcU1wXNR6q4V8w"
}
}File API 操作是免費的:
在 Messages 請求中使用的檔案內容按輸入令牌計費。您只能下載由技能或程式碼執行工具建立的檔案。
在測試版期間:
curl -X POST https://api.anthropic.com/v1/files \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: files-api-2025-04-14" \
-F "file=@/path/to/document.pdf"curl -X POST https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: files-api-2025-04-14" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Please summarize this document for me."
},
{
"type": "document",
"source": {
"type": "file",
"file_id": "file_011CNha8iCJcU1wXNR6q4V8w"
}
}
]
}
]
}'container_upload |
| 分析資料、建立視覺化 |
# 範例:讀取文字檔案並將其作為純文字發送
# 注意:對於包含特殊字元的檔案,請考慮 base64 編碼
TEXT_CONTENT=$(cat document.txt | jq -Rs .)
curl https://api.anthropic.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d @- <<EOF
{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Here's the document content:\n\n${TEXT_CONTENT}\n\nPlease summarize this document."
}
]
}
]
}
EOFcurl -X GET "https://api.anthropic.com/v1/files/file_011CNha8iCJcU1wXNR6q4V8w/content" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: files-api-2025-04-14" \
--output downloaded_file.txt