Was this page helpful?
檔案 API 讓您上傳和管理檔案以供 Claude API 使用,無需在每次請求時重新上傳內容。這在使用程式碼執行工具提供輸入(例如資料集和文件)然後下載輸出(例如圖表)時特別有用。您也可以使用檔案 API 來避免在多個 API 呼叫中持續重新上傳常用的文件和圖像。您可以直接探索 API 參考,除了本指南外。
檔案 API 處於測試版。請透過意見回饋表單分享您對檔案 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+ 模型中用於程式碼執行工具。
檔案 API 目前在 Amazon Bedrock 或 Google Vertex AI 上不受支援。
檔案 API 提供了一個簡單的一次上傳、多次使用的方法來處理檔案:
file_idfile_id 而不是重新上傳內容要使用檔案 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 參考檔案:
檔案 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"
}
}檢索您上傳的檔案列表:
檢索特定檔案的資訊:
從您的工作區中移除檔案:
下載由技能或程式碼執行工具建立的檔案:
Messages API 呼叫和相關工具使用中持續存在透過檔案 API 上傳的檔案會保留,直到使用 DELETE /v1/files/{file_id} 端點明確刪除為止。檔案會儲存以供在多個 API 請求中重複使用。
如需所有功能的 ZDR 資格,請參閱 API 和資料保留。
使用檔案 API 時的常見錯誤包括:
file_id 不存在或您無法存取它/v1/messages 請求中使用 500 MB 純文字檔案)<、>、:、"、|、?、*、\、/ 或 unicode 字元 0-31){
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "File not found: file_011CNha8iCJcU1wXNR6q4V8w"
}
}檔案 API 操作免費:
在 Messages 請求中使用的檔案內容按輸入令牌計費。您只能下載由技能或程式碼執行工具建立的檔案。
在測試版期間:
uploaded = client.beta.files.upload(
file=("document.pdf", open("/path/to/document.pdf", "rb"), "application/pdf"),
)response = client.beta.messages.create(
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_id,
},
},
],
}
],
betas=["files-api-2025-04-14"],
)
print(response)container_upload |
| 分析資料、建立視覺化 |
import pandas as pd
# 範例:讀取 CSV 檔案
df = pd.read_csv("data.csv")
csv_content = df.to_string()
# 在訊息中作為純文字發送
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": f"Here's the CSV data:\n\n{csv_content}\n\nPlease analyze this data.",
}
],
}
],
)
print(response.content[0].text)client = anthropic.Anthropic()
files = client.beta.files.list()file = client.beta.files.retrieve_metadata(file_id)result = client.beta.files.delete(file_id)file_content = client.beta.files.download(file_id)
# Save to file
file_content.write_to_file("downloaded_file.txt")