Was this page helpful?
Files API 讓您可以上傳和管理檔案,以便在 Claude API 中使用,而無需在每次請求時重新上傳內容。這在使用程式碼執行工具提供輸入(例如資料集和文件)並下載輸出(例如圖表)時特別有用。您也可以使用 Files API 來避免在多次 API 呼叫中持續重新上傳常用的文件和圖片。除了本指南外,您還可以直接探索 API 參考。
Files API 目前處於測試版。請透過我們的意見回饋表單分享您使用 Files API 的體驗。
This feature is not eligible for Zero Data Retention (ZDR). Data is retained according to the feature's standard retention policy.
在 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 上傳的檔案將保留,直到使用 DELETE /v1/files/{file_id} 端點明確刪除為止。檔案會被儲存以供多次 API 請求重複使用。
有關所有功能的 ZDR 資格,請參閱 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"
}
}Files API 操作是免費的:
在 Messages 請求中使用的檔案內容將按輸入 token 計費。您只能下載由技能或程式碼執行工具建立的檔案。
在測試期間:
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-opus-4-6",
"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-opus-4-6",
"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