Media & Files
Foir includes a built-in media system for managing images, videos, and documents. Upload assets, apply on-the-fly image transformations, and serve optimized media through a CDN.
Overview
The media system supports images, videos, and general file uploads. Key features include:
- Drag-and-drop uploads in the console
- CLI uploads for scripting and automation
- Presigned URL uploads via the API for direct-to-storage transfers
- On-the-fly image transformations (resize, reformat, adjust quality)
- Video processing with thumbnail extraction and HLS streaming
- Rich metadata including blurhash, dimensions, and alt text
- Folder organization for media library management
Upload Flow
Uploads use a presigned URL flow that keeps large files off the GraphQL endpoint:
- Start the upload: call
uploadFileto get an upload ID, a presigned upload URL, and the upload’s own token - Upload directly to storage: PUT the file to the presigned URL
- Say the bytes have landed: POST to the upload’s route with its token; the answer is the file
When the bytes land, the platform reads them from storage itself and extracts dimensions, blurhash, and other metadata.
File Metadata
The public File type exposes:
| Field | Description |
|---|---|
id | File identifier |
filename | Original filename |
mimeType | MIME type |
size | Size in bytes |
url | Where the bytes are served from. A direct, CDN-cacheable URL for a PUBLIC file; a short-lived signed URL for a PRIVATE one |
width / height | Dimensions (images and videos) |
blurhash | Low-resolution placeholder for progressive loading |
alt | Alt text for accessibility |
folder | The folder the file is filed under; null at the project root. The files connection filters on it |
visibility | PUBLIC or PRIVATE — see Visibility |
createdAt | Upload timestamp |
deletedAt | Soft-delete timestamp (null when active) |
Files and organizations
In a project that uses organizations, files carry the organization axis exactly as records do. A file uploaded by a caller standing in an organization belongs to that organization: the database stamps the organization onto the file row from the caller’s own claim, and no upload call takes an organization argument.
Files are confined by the same rule as every other row. A caller standing in an organization reads that organization’s files, the files of every organization nested under it, and the project-shared files; it changes and deletes only its own organization’s files. A project key standing in no organization reads the project-shared files only. A file’s organization cannot be changed by a caller standing in an organization. See Organization Trees for the rule in full.
An upload is also checked against the media_storage cap of the organization it lands in and of every organization above it, as well as against the project’s and the plan’s limits. Deleting an organization removes its files from storage when its deletion window closes.
In the console
Uploading files
- Go to the Files section
- Drag and drop files onto the upload area, or click to browse
- Files are uploaded and processed automatically
- Optionally set alt text, a caption, a description and tags on the file’s page
Managing the media library
- Search by filename
- Filter by file type (images, videos, documents) and by processing status
- Switch between Library and Trash
- View this project’s storage usage on the Files page; the workspace-wide breakdown is on your workspace’s Billing page in the platform console. Media storage is one pool for the whole workspace, and a project draws on it unless a project cap fences its share.
Folders are not browsed in the console. File a file under one with foir media update --folder or the updateFile mutation, and filter the files connection on it.
Serving a PRIVATE file counts toward the plan’s private media served allowance, because private files are fetched per reader and never cached. At the ceiling, private files pause until the period resets; public files keep serving.
Via the CLI
Upload a file
foir media upload ./images/hero-banner.jpgUpload to a specific folder:
foir media upload ./images/hero-banner.jpg --folder bannersList files
foir media listFilter by folder or MIME type:
foir media list --folder banners --mime-type imageGet file details
foir media get file_abc123Update a file
foir media update file_abc123 --folder heroes --tags "marketing,homepage"Update file metadata
foir media update-metadata file_abc123 --alt-text "A scenic mountain landscape"Check storage usage
foir media usageDelete a file
foir media delete file_abc123Use --permanent for a hard delete that cannot be restored:
foir media delete file_abc123 --permanentRestore a deleted file
foir media restore file_abc123Via the API
Note: On the data client these are
filesnamespace operations —foir.ns.files.<field>(), named after the GraphQL field. Your key only has the namespace if it holds the file scopes, and a namespace result comes back verbatim, soincludeandomitdo not apply.
Upload flow
Step 1: Start the upload
uploadFile takes scalar arguments and returns where the bytes go, and how to say they have landed.
mutation {
uploadFile(
filename: "hero-banner.jpg"
mimeType: "image/jpeg"
size: 2048576
folder: "banners"
) {
uploadId
uploadUrl
landed {
token
endpoint
}
}
}folder and metadata (JSON) are optional.
The same call through the data client:
const upload = await foir.ns.files.uploadFile({
filename: 'hero-banner.jpg',
mimeType: 'image/jpeg',
size: 2048576,
folder: 'banners',
});Step 2: Upload the file
PUT the file directly to the presigned URL:
await fetch(upload.uploadUrl, {
method: 'PUT',
headers: { 'Content-Type': 'image/jpeg' },
body: fileBuffer,
});Step 3: Say the bytes have landed
POST to the upload’s own route with its token. The answer is the finished file.
const res = await fetch(upload.landed.endpoint, {
method: 'POST',
headers: { 'X-Foir-Token': upload.landed.token },
});
const file = await res.json();Queries
Get a file
query {
file(id: "file_abc123") {
id
filename
mimeType
size
url
width
height
blurhash
alt
createdAt
}
}The same read through the data client. A file is not one of your models, so it comes back whole — there is no selection set and no omit:
const file = await foir.ns.files.file({ id: 'file_abc123' });List files
files is a Relay cursor connection. Filter with folder, mimeType, and filename; paginate with first/after.
query {
files(first: 20, mimeType: "image/", folder: "banners") {
edges {
node {
id
filename
mimeType
size
url
blurhash
}
cursor
}
pageInfo {
hasNextPage
endCursor
}
totalCount
}
}The same list through the data client. The filters are the same arguments, and the result is the same connection:
const files = await foir.ns.files.files({
first: 20,
mimeType: 'image/',
folder: 'banners',
});Mutations
Delete a file
deleteFile soft-deletes the file and returns a boolean.
mutation {
deleteFile(id: "file_abc123")
}Through the data client:
await foir.ns.files.deleteFile({ id: 'file_abc123' });Bring a file back from the trash
mutation {
undeleteFile(id: "file_abc123") {
id
filename
deletedAt
}
}The same through the data client, which returns the whole File:
const file = await foir.ns.files.undeleteFile({ id: 'file_abc123' });Purge a file
mutation {
purgeFile(id: "file_abc123")
}And through the data client:
await foir.ns.files.purgeFile({ id: 'file_abc123' });Image Transformations
Image transforms are driven by an opaque token bound to each usage of an image. The platform emits a URL like:
https://cdn.foir.dev/file_abc123?t=eyJzIjoibWVkaXVtIiwiYyI6IjEwLDEwLDgwLDgwIn0The ?t= parameter is a base64url-encoded JSON object the platform produced when resolving the image. Decoded, it has up to four fields:
{ "s": "medium", "c": "10,10,80,80", "f": "0.5,0.4" }| Field | Meaning | Format |
|---|---|---|
s | Size preset | thumbnail (160 px) / small (640) / medium (1024) / large (1280) / xlarge (1920) / 2xl (2560) / 3xl (3840) / 4xl (5120) |
c | Crop rectangle (per-usage) | x,y,width,height as 0–100 percentages |
f | Focal point (used for smart crop fallback) | x,y as 0–1 floats |
fmt | Output format | webp / avif / jpeg / png |
Why a token, not free-form params
Earlier versions of the API accepted free-form ?width=800&height=600&fit=COVER&format=WEBP parameters. They no longer do — that grammar would let scrapers fan out arbitrary variants and burn through CDN-side image-resize bills. The token grammar is opaque enough that only URLs the platform emits will hit the transform pipeline; arbitrary client-side rewrites just return the original.
Per-usage crops and focal points
When the same image appears in multiple places (a hero on the homepage, a thumbnail on the listing, a portrait crop in a card), each usage can have its own crop and focal point baked into the URL. The platform stores these alongside the embed in the content field and writes a fresh token whenever the image is resolved.
You don’t construct these tokens by hand — the platform emits them. Frontend code passes the URL straight from the API response into an <img> tag.
Choosing the output format
Format is not negotiated: the Accept header is not consulted. The output format is carried inside the ?t= token, and a token that does not name one is served as WebP. One URL has exactly one representation, which is what lets the CDN cache a transform without varying on a header its standard cache ignores anyway.
Choose it when you ask for the image: variants(format: AVIF). See Image Transformations.
Responsive images example
An ImageValue exposes a variants object whose keys are the size presets (thumbnail, small, medium, large, xlarge, _2xl, _3xl, _4xl). Each is a separate resolution the platform has already tokenised, carrying its own url, width and height:
function ResponsiveImage({ image }: { image: ImageValue }) {
return (
<img
src={image.url}
srcSet={[
`${image.variants.small?.url} 640w`,
`${image.variants.medium?.url} 1024w`,
`${image.variants.large?.url} 1280w`,
`${image.variants.xlarge?.url} 1920w`,
].join(', ')}
alt={image.alt}
width={image.width}
height={image.height}
loading="lazy"
/>
);
}If you need a specific size for a custom layout, request it from the platform’s image-resolution helper rather than rewriting the URL — the platform will mint a token at the size you ask for.
Progressive loading with blurhash
Use the blurhash field to show a low-resolution placeholder while the full image loads:
import { Blurhash } from 'react-blurhash';
function ImageWithPlaceholder({ image }) {
const [loaded, setLoaded] = useState(false);
return (
<div style={{ position: 'relative' }}>
{!loaded && image.blurhash && (
<Blurhash hash={image.blurhash} width={400} height={300} />
)}
<img
src={image.url}
alt={image.alt}
width={image.width}
height={image.height}
onLoad={() => setLoaded(true)}
style={{ display: loaded ? 'block' : 'none' }}
/>
</div>
);
}Video Features
Uploaded videos are processed automatically:
- Thumbnail extraction — A poster frame is generated for display before playback
- Duration detection — Stored in file metadata
- HLS streaming: adaptive bitrate playback for smooth delivery (Pro and Enterprise plans)
Best Practices
- Always give images alt text. A file’s alt text is one string, shown in the file library; for alt text a record publishes, and for translations, use a
textfield beside the image field. - Use blurhash placeholders for a polished loading experience, especially on image-heavy pages.
- Organize files into folders and use tags for easy discovery in the media library.
- Set per-usage crops in the editor (drag the focal point or crop handles) rather than relying on a single source crop — the platform bakes the per-usage geometry into the URL token automatically.
- Pass image URLs straight through from the API response. Don’t try to rewrite the
?t=token client-side; unknown tokens just return the original image.