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 呼叫中引用:
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"上傳檔案的回應將包含:
{
"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 引用檔案:
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"
}
}
]
}
]
}'Files API 支援不同的檔案類型,對應不同的內容區塊類型:
| 檔案類型 | MIME 類型 | 內容區塊類型 | 使用案例 |
|---|---|---|---|
application/pdf | document | 文字分析、文件處理 | |
| 純文字 | text/plain | document | 文字分析、處理 |
| 圖片 | image/jpeg、image/png、image/gif、image/webp | image | 圖片分析、視覺任務 |
| 資料集、其他 | 各異 | container_upload | 分析資料、建立視覺化圖表 |
對於不支援作為 document 區塊的檔案類型(.csv、.txt、.md、.docx、.xlsx),請將檔案轉換為純文字,並直接在訊息中包含內容:
# 範例:讀取文字檔案並以純文字傳送
# 注意:對於包含特殊字元的檔案,請考慮使用 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."
}
]
}
]
}
EOF對於包含圖片的 .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"下載由技能或程式碼執行工具建立的檔案:
curl -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.txtMessages 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 請求中使用的檔案內容按輸入 token 計費。您只能下載由技能或程式碼執行工具建立的檔案。
在測試版期間:
Was this page helpful?