Skip to main content

Overview

Uploads an image to Sublay storage. The server processes the image, generates named variants according to the specified mode, and returns the File with signed links to the original and every variant. The links expire at the File’s linksExpireAt (File links), so store the file’s id, not a link. The options are sent as one JSON field, file.options: the same options a record’s own images take (for example avatarFile on useUser). Works in both browser and React Native environments. Provides upload progress tracking. The uploaded file isn’t attached to anything. A file that belongs to a record is sent with that record’s own hook (for example, the images of useCreateEntity).
Requires an authenticated user. The hook uses the private axios instance and will throw if no session is active.

Usage Example

Parameters

uploadImage({ file, ...options }) takes one object: the file, plus the processing configuration (UploadImageOptions) and the call’s own onProgress and metadata.
BrowserFile | RNFile
required
The image to upload. In the browser, pass a standard File object. In React Native, pass { uri, name, type? }. Not part of file.options.
string
required
The processing mode. See Processing Modes below. Every requested size is 1–10000 px, and dimensions, widths, heights and sizes take at most 30 entries (one variant each). Unknown keys in the options, a key of another mode included, are refused with 400 storage/invalid-body. A variant name uses only letters, digits, _, - and ., is at most 64 characters, can’t contain .., and can’t be original.

Common Options (all modes)

number
JPEG/WebP quality, 1–100. Defaults to 85.
"webp" | "jpeg" | "png" | "original"
Output format for all variants. Defaults to "webp". original keeps the upload’s format when it is JPEG, PNG or WebP, and stores an image whose content is HEIC or AVIF (sent under a JPEG, PNG or WebP type) as WebP.
boolean
Whether to remove EXIF metadata from the output. Defaults to true.
string
How each variant fits its requested size. Defaults to "inside". The values each mode takes are listed under Processing Modes.
number
The longest side, in px, the stored original is shrunk to (50–10000). Defaults to 3840. An original already within it keeps its dimensions. originalSize, originalWidth and originalHeight describe the stored original. A larger original takes more storage, which counts toward the project’s file-storage limit.
string[]
Folder segments for the stored image. The new file’s id is always added after them (<pathParts>/<fileId>/<filename>, the file name taking the stored format’s extension). Defaults to ["images"]. A folder that is, or contains, . or .. (also written %2e%2e), or that contains ?, # or an encoded slash (%2f, %5c), is refused with a 400.
(progress: number) => void
Called with upload percentage (0–100) during the multipart upload. Not sent to the server.
Record<string, any>
Custom key-value data stored on the file, sent as its own field (not inside file.options). The server adds originalFilename, originalFormat and originalDimensions (the uploaded image’s own name, format and size) alongside, replacing any values of yours under those keys.

Processing Modes

exact-dimensions

Each variant has an explicit pixel width and height.

aspect-ratio-width-based

All variants share a fixed aspect ratio. You specify widths; heights are computed.

aspect-ratio-height-based

All variants share a fixed aspect ratio. You specify heights; widths are computed.

original-aspect

Preserves the original image’s aspect ratio. You specify the longest-edge size for each variant.

multi-aspect-ratio

Picks the ratio in aspectRatios closest to the image and generates one variant per sizes entry.

Returns

({ file, ...options }: UploadImageProps) => Promise<File>
Async function that performs the upload and resolves with the File on success.
boolean
true while the upload is in progress.
number
Upload percentage (0–100). Resets to 0 when the upload finishes.

Return Value Shape (File)

The File, the same shape Fetch File returns, its image data included: