View Source Anthropic.Files (anthropic_community v0.5.0)

The Files resource: upload a file once and reference it by file_id in a later message instead of inlining base64 data.

This is a beta API. Every request this module sends automatically carries the anthropic-beta: files-api-2025-04-14 header it currently requires — no client configuration needed, and this doesn't affect any other resource's requests.

Referencing an uploaded file's file_id back in a Messages.create/2 call (a %{type: "file", file_id: ...} content-block source) is also beta and not yet modeled by Anthropic.Messages.Content.Image/Document — pass it as a raw map, and add the same beta header to that call via :default_headers on the Client (or a one-off header if your Finch adapter supports per-request headers):

{:ok, file} = Anthropic.Files.create(client, "/path/to/report.pdf")

Anthropic.Messages.create(client,
  model: "claude-opus-4-8",
  max_tokens: 1024,
  messages: [
    %{role: "user", content: [%{type: "document", source: %{type: "file", file_id: file.id}}]}
  ]
)

Summary

Functions

Uploads a local file (by path). :content_type is guessed from the file extension via MIME.from_path/1 when not given.

Uploads raw binary data with an explicit filename (:content_type defaults to "application/octet-stream").

Deletes a file.

Downloads a file's raw content.

Lists uploaded files, most recently created first.

Like list/2, but returns a lazy Stream of individual files that transparently fetches subsequent pages as it's consumed, instead of one page at a time.

Retrieves a file's metadata.

Types

@type file_metadata() :: %{
  id: String.t(),
  type: String.t(),
  filename: String.t(),
  mime_type: String.t(),
  size_bytes: non_neg_integer(),
  created_at: String.t(),
  downloadable: boolean() | nil,
  scope: String.t() | nil
}

Functions

Link to this function

create(client, path, opts \\ [])

View Source
@spec create(Anthropic.Client.t(), path :: String.t(), [{:content_type, String.t()}]) ::
  {:ok, file_metadata()} | {:error, Anthropic.Error.t()}

Uploads a local file (by path). :content_type is guessed from the file extension via MIME.from_path/1 when not given.

Link to this function

create_from_binary(client, data, filename, opts \\ [])

View Source
@spec create_from_binary(Anthropic.Client.t(), binary(), String.t(), [
  {:content_type, String.t()}
]) ::
  {:ok, file_metadata()} | {:error, Anthropic.Error.t()}

Uploads raw binary data with an explicit filename (:content_type defaults to "application/octet-stream").

@spec delete(Anthropic.Client.t(), file_id :: String.t()) ::
  {:ok, %{id: String.t(), type: String.t()}} | {:error, Anthropic.Error.t()}

Deletes a file.

Link to this function

download(client, file_id)

View Source
@spec download(Anthropic.Client.t(), file_id :: String.t()) ::
  {:ok, binary()} | {:error, Anthropic.Error.t()}

Downloads a file's raw content.

Link to this function

list(client, opts \\ [])

View Source
@spec list(Anthropic.Client.t(),
  before_id: String.t(),
  after_id: String.t(),
  limit: pos_integer(),
  scope_id: String.t()
) ::
  {:ok,
   %{
     data: [file_metadata()],
     has_more: boolean(),
     first_id: String.t() | nil,
     last_id: String.t() | nil
   }}
  | {:error, Anthropic.Error.t()}

Lists uploaded files, most recently created first.

Link to this function

list_all(client, opts \\ [])

View Source
@spec list_all(
  Anthropic.Client.t(),
  keyword()
) :: Enumerable.t()

Like list/2, but returns a lazy Stream of individual files that transparently fetches subsequent pages as it's consumed, instead of one page at a time.

Link to this function

retrieve(client, file_id)

View Source
@spec retrieve(Anthropic.Client.t(), file_id :: String.t()) ::
  {:ok, file_metadata()} | {:error, Anthropic.Error.t()}

Retrieves a file's metadata.