The Files API lets you upload and manage files to use with the Claude API without re-uploading content with each request. This is particularly useful when using the code execution tool to provide inputs (for example, datasets and documents) and then download outputs (for example, charts). You can explore the API reference directly, in addition to this guide.
Referencing a file_id in a Messages request is supported on all models that support the given file type. Images are supported on all current Claude models. For PDFs and other file types with the code execution tool, see the linked pages for model support.
The Files API provides a create-once, use-many-times approach for working with files:
file_idfile_id instead of re-uploading contentUpload a file to be referenced in future API calls:
uploaded = client.files.upload(
file=("document.pdf", open("/path/to/document.pdf", "rb"), "application/pdf"),
)
file_id = uploaded.id
print(file_id)The response from uploading a file includes:
{
"id": "file_011CNha8iCJcU1wXNR6q4V8w",
"type": "file",
"filename": "document.pdf",
"mime_type": "application/pdf",
"size_bytes": 1024000,
"created_at": "2025-01-01T00:00:00Z",
"downloadable": false,
"expires_at": null
}downloadable is false for files you upload. Only files created by skills or the code execution tool can be downloaded. See Downloading a file.
Once uploaded, reference the file by passing the id from the upload response as file_id:
response = client.messages.create(
model="claude-opus-5",
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,
},
},
],
}
],
)
print(response)The Files API supports different file types that correspond to different content block types:
| File type | MIME type | Content block type | Use case |
|---|---|---|---|
application/pdf | document | Text analysis, document processing | |
| Plain text | text/plain | document | Text analysis, processing |
| Images | image/jpeg, image/png, image/gif, image/webp | image | Image analysis, visual tasks |
| Datasets, others | Varies | container_upload | Analyze data, create visualizations |
For PDFs and text files, use the document content block:
{
"type": "document",
"source": {
"type": "file",
"file_id": "file_011CNha8iCJcU1wXNR6q4V8w"
},
"title": "Document Title", // Optional
"context": "Context about the document", // Optional
"citations": { "enabled": true } // Optional, enables citations
}For images, use the image content block:
{
"type": "image",
"source": {
"type": "file",
"file_id": "file_011CPMxVD3fHLUhvTqtsQA5w"
}
}To send a file to the code execution tool, use the container_upload content block:
{
"type": "container_upload",
"file_id": "file_011CNha8iCJcU1wXNR6q4V8w"
}For file types that the document block doesn't support (for example, .docx and .xlsx), convert the files to plain text and include the content directly in your message. Files that are already plain text, such as .csv and .md files, can either be read in this way or uploaded through the Files API with an explicit text/plain content type. To analyze datasets instead of reading them as text, upload them for the code execution tool using a container_upload block.
The following examples read a text file and send its contents as plain text:
client = anthropic.Anthropic()
# Read the text file
with open("document.txt") as f:
text_content = f.read()
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": f"Here's the document content:\n\n{text_content}\n\nPlease summarize this document.",
}
],
}
],
)
for block in response.content:
if block.type == "text":
print(block.text)Retrieve a list of your uploaded files. The endpoint is paginated: each request returns up to limit files (20 by default, and at most 1,000), and the response's next_page cursor fetches the next page when passed back as the page parameter. Files are ordered newest first. See the List Files API reference. The SDKs return the first page and provide auto-pagination helpers. The CLI example bounds the total with --max-items:
client = anthropic.Anthropic()
files = client.files.list()
print(files)To check a known set of files in one request instead of paging, pass up to 100 file IDs as ids[] query parameters. An ids[] request always returns a single page (next_page is null), and any ID that does not resolve to a file in your workspace is silently omitted from data; compare the returned IDs against the requested IDs to detect misses. ids[] cannot be combined with page or limit.
The page parameter, the next_page cursor, and the ids[] filter apply to requests sent without the anthropic-beta: files-api-2025-04-14 header. Requests that send the header receive the earlier list format described in the note under How to use the Files API.
Retrieve information about a specific file:
file = client.files.retrieve_metadata(file_id)
print(file)Remove a file from your workspace:
client.files.delete(file_id)Download files that were created by skills or the code execution tool. Files you upload cannot be downloaded. The file_id of a generated file appears in the bash_code_execution_tool_result content block of the Messages response that created it:
file_content = client.files.download(file_id)
file_content.write_to_file("downloaded_file.txt")DELETE /v1/files/{file_id} endpoint or they reach their expires_atTo have a file expire automatically, include an expires_in_seconds form field when you upload it. The value is an integer number of seconds between 3,600 (1 hour) and 7,776,000 (90 days). The resulting expires_at timestamp (RFC 3339) appears on every file response and is null for files uploaded without an expiration. Expiration is set once at upload and cannot be changed.
When a file reaches its expires_at:
GET /v1/files/{file_id}/content) returns a 404 errorGET /v1/files/{file_id}) remains readable for up to 30 days, with expires_at in the pastexpires_at to the current time to filter expired filesDeleting an expired file with DELETE /v1/files/{file_id} removes its metadata immediately instead of waiting for the 30-day window to elapse.
If your organization has the Compliance API enabled, its Activity Feed records Files API operations made with a Claude API key or from the Claude Console: each upload (POST /v1/files), content download (GET /v1/files/{file_id}/content), and deletion (DELETE /v1/files/{file_id}) appears as a platform_file_uploaded, platform_file_content_downloaded, or platform_file_deleted activity. Listing files and retrieving file metadata are not recorded. Operations that occur while the Compliance API is off are not recorded and cannot be recovered later, so set up the Compliance API before you rely on this audit trail. On Claude Platform on AWS, audit file operations with AWS CloudTrail data events instead.
Common errors when using the Files API include:
file_id doesn't exist or you don't have access to it"downloadable": false and cannot be downloaded. Only files created by skills or the code execution tool can be downloaded/v1/messages request)<, >, :, ", |, ?, *, \, /, or Unicode characters 0-31){
"type": "error",
"error": {
"type": "not_found_error",
"message": "File `file_011CNha8iCJcU1wXNR6q4V8w` not found."
},
"request_id": "req_011CQFYcrRp7mCHLDsAYT8Qt"
}Files API operations are free:
File content used in Messages requests is priced as input tokens.
File-related API calls are limited to approximately 500 requests per minute. To request a higher limit, contact sales.
Process PDFs with Claude. Extract text, analyze charts, and understand visual content from your documents.
Run Python and bash code in a sandboxed container to analyze data, generate files, and iterate on solutions.
Process and analyze visual input and generate text and code from images.
| Supported platforms |
|
|---|
Was this page helpful?