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 调用中引用:
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 上传的文件将保留,直到使用 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 计费。您只能下载由技能或代码执行工具创建的文件。
在测试期间:
Was this page helpful?