InputFile class represents a file to be uploaded to Telegram. It can handle local file paths, URLs, buffers, streams, and various other file sources.
Constructor
Creates a new InputFile instance from various file sources.string | Blob | URL | Uint8Array | ReadableStream | ...
required
The file data source. Can be:
- String: Local file path (Deno only) or identifier
- Blob: File blob object
- URL: URL object pointing to a file
- Uint8Array: Raw file bytes
- ReadableStream: Stream of file data
- Iterable/AsyncIterable: Iterator yielding
Uint8Arraychunks - Function: Supplier function returning any of the above
new URL(...) or pass file IDs directly to API methods.string
Optional filename. If omitted, grammY attempts to guess it from the file source (file paths and URLs).
Examples
Properties
string | undefined
The filename of the file. May be
undefined if not specified and couldn’t be guessed from the source.Usage with Bot API Methods
You can useInputFile instances with any method that accepts file uploads.
Sending Photos
Sending Documents
Sending Audio
Sending Video
Sending Stickers
Sending Voice Messages
Sending Video Notes
Media Groups
You can useInputFile in media groups to send albums.
Advanced Usage
Lazy Loading Files
Use a supplier function to load files only when needed:Streaming Large Files
For large files, use streams to avoid loading the entire file into memory:Fetching from URLs
Working with Blobs (Browser/Deno)
Setting Custom Thumbnails
Many file types accept custom thumbnails:File Identifiers vs InputFile
Telegram provides file identifiers for uploaded files. You can reuse these without creating newInputFile instances:
Platform-Specific Notes
Deno
In Deno, you can pass file paths directly:Node.js
In Node.js, usefs module for file operations:
Browser
In browsers, work withBlob or File objects:
Error Handling
Important Notes
- File Reuse:
InputFileinstances cannot be reused. Create a new instance for each upload operation. - File Paths: Only work in Deno environments. Use streams in Node.js.
- HTTP URLs: Don’t pass HTTP URLs as strings. Use
new URL(...)instead. - File IDs: If you have a file_id from Telegram, pass it directly to API methods without wrapping in
InputFile. - File Size Limits: Telegram has file size limits (20 MB for photos, 50 MB for other files via bot API).
Complete Example
See Also
- Files Guide - Complete file handling documentation
- Telegram Bot API: Sending Files
- InputMedia Types