package anthropic
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Module Beta.Files
Source
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".
type t = {
id : string;
(*Unique file identifier.
*)type_ : string;
(*The type of the file.
*)filename : string;
(*Original filename.
*)mime_type : string;
(*MIME type of the file.
*)size_bytes : int;
(*File size in bytes.
*)created_at : string;
(*ISO 8601 creation timestamp.
*)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.
Confirmation of file deletion.
val 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.
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)
get_metadata client ~file_id ()
retrieves file metadata.
list client ?limit ?after_id ()
retrieves a paginated list of files.
Lists files in reverse chronological order (newest first).
delete client ~file_id ()
permanently deletes a file.
Deleted files cannot be recovered. Any messages referencing the file will fail.
val 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)