package anthropic

  1. Overview
  2. Docs

Module Beta.FilesSource

Files API for uploading and managing files (beta).

This API enables uploading files that can be referenced in messages. Requires beta header "files-api-2025-04-14".

Sourcetype t = {
  1. id : string;
    (*

    Unique file identifier.

    *)
  2. type_ : string;
    (*

    The type of the file.

    *)
  3. filename : string;
    (*

    Original filename.

    *)
  4. mime_type : string;
    (*

    MIME type of the file.

    *)
  5. size_bytes : int;
    (*

    File size in bytes.

    *)
  6. created_at : string;
    (*

    ISO 8601 creation timestamp.

    *)
  7. downloadable : bool option;
    (*

    Whether the file can be downloaded.

    *)
}

t represents metadata about an uploaded file.

Files are stored securely and can be referenced in messages using the File content block type.

Sourcetype deleted = {
  1. id : string;
}

Confirmation of file deletion.

Sourceval upload : client -> filename:string -> media_type:string -> content:Eio.Flow.source_ty Eio.Resource.t -> unit -> (t, error) result

upload client ~filename ~media_type ~content () uploads a file.

  • parameter filename

    The name to display for the file.

  • parameter media_type

    MIME type (e.g., "application/pdf", "text/plain").

  • parameter content

    An Eio flow containing the file data.

  • raises Api_error

    if the file is too large or has an unsupported type.

Example: Uploads a text file.

  let content = Eio.Flow.string_source "Hello, world!" in
  match
    Beta.Files.upload client ~filename:"greeting.txt"
      ~media_type:"text/plain" ~content ()
  with
  | Ok file -> Printf.printf "Uploaded file %s\n" file.id
  | Error e -> Printf.eprintf "Upload failed: %s\n" (string_of_error e)
Sourceval get_metadata : client -> file_id:string -> unit -> (t, error) result

get_metadata client ~file_id () retrieves file metadata.

  • parameter file_id

    The file identifier to query.

  • raises Api_error

    if the file doesn't exist or isn't accessible.

Sourceval list : client -> ?limit:int -> ?after_id:string -> unit -> (t page, error) result

list client ?limit ?after_id () retrieves a paginated list of files.

Lists files in reverse chronological order (newest first).

  • parameter limit

    Maximum files per page.

  • parameter after_id

    Pagination cursor for subsequent pages.

Sourceval delete : client -> file_id:string -> unit -> (deleted, error) result

delete client ~file_id () permanently deletes a file.

Deleted files cannot be recovered. Any messages referencing the file will fail.

  • parameter file_id

    The file to delete.

Sourceval download : client -> file_id:string -> unit -> (Cohttp.Response.t * Cohttp_eio.Body.t, error) result

download client ~file_id () retrieves file content.

Returns the raw HTTP response and body for flexible handling.

Example: Downloads and saves a file.

  match Beta.Files.download client ~file_id () with
  | Ok (resp, body) ->
      let content_type =
        Cohttp.Header.get (Cohttp.Response.headers resp) "content-type"
      in
      Printf.printf "Content type: %s\n"
        (Option.value content_type ~default:"unknown")
      (* Process body stream *)
  | Error e ->
      Printf.eprintf "Download failed: %s\n" (string_of_error e)
OCaml

Innovation. Community. Security.