Files API 允许您上传和管理文件,以便与 Claude API 配合使用,无需在每次请求时重新上传内容。这在使用代码执行工具提供输入(例如数据集和文档)然后下载输出(例如图表)时特别有用。您还可以使用 Files API 来避免在多次 API 调用中反复重新上传常用的文档和图像。除了本指南外,您还可以直接查看 API 参考。
Files API 目前处于 beta 阶段。请通过我们的反馈表单分享您使用 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,您需要包含 beta 功能头: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 计费。您只能下载由技能或代码执行工具创建的文件。
在 beta 期间:
Was this page helpful?