Files API
The GraphQL Files API covers uploading and querying media library files: images, video, PDFs, anything you’d reference from a record’s image or file field. An upload hands back a URL to send the bytes straight to storage, so binary bytes never touch the GraphQL endpoint.
For the broader media-library overview see Media & Files.
Note:
filesis a scope-gated namespace on the data client — the gate is the scope, not the key’s kind, and a publishable key may hold the file scopes. What each one puts on the client:files:readaddsfile(id:)and thefilesconnection, andfiles:writeadds the upload mutation. A key holding none of them does not see the namespace, and a root field it cannot reach is pruned from that caller’s schema variant. Its members are fixed by the platform, not by your project, and are listed in full under Namespace operations. The op name and the GraphQL root field name are the same string for every namespace exceptoperations.
Uploading
An upload takes three steps:
- Start the upload with
uploadFile. The platform answers with anuploadId, a presigneduploadUrlfor the bytes, andlanded: a token minted for this upload alone and the route to send it to. - PUT the bytes straight to
uploadUrlfrom your client or server. - Say the bytes have landed by POSTing to
landed.endpointwithlanded.tokenin theX-Foir-Tokenheader. The platform reads the bytes from storage itself, checks them, and answers with the file.
uploadFile needs the files:write scope. The third step needs only the upload’s own token, which is good for that upload and nothing else, until expiresAt.
Step 1: uploadFile
mutation StartUpload {
uploadFile(
filename: "hero.jpg"
mimeType: "image/jpeg"
size: 482910
folder: "campaigns/spring-26"
metadata: { alt: "Field of barley at sunrise", source: "drew@bobscountrybunker.com" }
) {
uploadId
uploadUrl
expiresAt
landed {
token
endpoint
}
}
}| Argument | Type | Required | Description |
|---|---|---|---|
filename | String! | yes | Filename to store. Used for the canonical URL and as the default display name. |
mimeType | String! | yes | Content-Type: image/jpeg, video/mp4, application/pdf and so on. |
size | Int! | yes | Exact size in bytes, greater than zero; an empty file is refused. The PUT body must have this length, and its Content-Type must match mimeType; both are signed into the upload URL. When the bytes land, the size the bucket stored is checked against the workspace’s media storage allowance, which is a pool shared by every project unless a project cap fences one project’s share (scope). There is no per-file size limit on this flow. |
folder | String | no | Folder path. Slashes form nesting (campaigns/spring-26). Created on demand. |
visibility | Visibility | no | PUBLIC or PRIVATE. Omitted, it follows who owns the file. See Visibility. |
metadata | JSON | no | Arbitrary key-value blob attached to the file (alt text, source, copyright, etc.). |
Response:
{
"uploadId": "upl_abc123",
"uploadUrl": "https://<account>.r2.cloudflarestorage.com/<bucket>/<key>?X-Amz-Algorithm=...&X-Amz-Signature=...",
"expiresAt": "2026-09-20T13:00:00Z",
"landed": {
"token": "eyJhbGciOiJFZERTQSIs...",
"endpoint": "https://api.foir.io/v1/uploads/upl_abc123/landed"
}
}uploadUrl is a presigned PUT against Cloudflare R2, signed with AWS SigV4.
The query string is the X-Amz-* set, and the host is R2’s, not Foir’s. If the
upload runs behind a Content-Security-Policy or an egress allowlist, allow
*.r2.cloudflarestorage.com in connect-src. Treat the URL as opaque: it is
short-lived and nothing in it is stable enough to parse.
expiresAt is when that URL stops accepting the PUT, and when landed.token
stops being accepted. Read it rather than hardcoding the window: it is the
deadline the platform signed. After it passes the uploadId is spent and not
renewable; call uploadFile again.
The same request through the data client:
const upload = (await foir.ns.files.uploadFile({
filename: 'hero.jpg',
mimeType: 'image/jpeg',
size: 482910,
folder: 'campaigns/spring-26',
metadata: { alt: 'Field of barley at sunrise', source: 'drew@bobscountrybunker.com' },
})) as { uploadId: string; uploadUrl: string; landed: { token: string; endpoint: string } };The assertion is not decoration. A namespace op’s generated result type is unknown, because the manifest lists root field names and carries no result shape; see namespace results are unknown. The op name itself is checked: misspell uploadFile and it is a compile error, which is what this call buys over foir.raw().
The uploadUrl accepts a PUT of exactly size bytes with mimeType as its Content-Type, for one hour. It can be used again until it expires, so treat it and landed.token as credentials: don’t log or persist them. After the URL expires, whatever it wrote and was never settled is deleted; a settled file is kept.
Step 2: PUT to uploadUrl
curl -X PUT "$UPLOAD_URL" \
-H "Content-Type: image/jpeg" \
--data-binary @hero.jpgawait fetch(upload.uploadUrl, {
method: 'PUT',
headers: { 'Content-Type': 'image/jpeg' },
body: fileBlob,
});The storage service verifies the signature, the content length, and the content type against what uploadFile declared. Mismatches are refused.
Step 3: say the bytes have landed
curl -X POST "$LANDED_ENDPOINT" -H "X-Foir-Token: $LANDED_TOKEN"const res = await fetch(upload.landed.endpoint, {
method: 'POST',
headers: { 'X-Foir-Token': upload.landed.token },
});
const file = (await res.json()) as { id: string; filename: string; url: string };The answer is the file, in the platform’s JSON form (size is a string, because it is a 64-bit number). The file is now live in the media library and referenceable from any record’s image or file field by its id. A refusal is a JSON body with an error that names the rule: the key’s allowed_file_types against the bytes that arrived, or the storage allowance.
The platform takes nothing from this request but which upload it is. It reads the bytes from storage itself, keeps the size the bucket stored rather than the size that was declared, and keeps the file with the authority the upload was started with: the same project, owner and key.
For images, width, height, and blurhash are filled in before the answer, so your storefront can show a real placeholder immediately rather than wait for a background job.
Reading
Fetch a file by ID
query GetFile {
file(id: "file_abc123") {
id
filename
mimeType
size
url
width
height
blurhash
alt
createdAt
}
}The same read through the data client:
const file = (await foir.ns.files.file({ id: 'file_abc123' })) as {
id: string;
filename: string;
url: string;
alt: string | null;
};Requires the files:read scope.
List files
query ListFiles {
files(first: 20, mimeType: "image/", folder: "campaigns/spring-26") {
edges {
node {
id
filename
mimeType
url
width
height
createdAt
}
cursor
}
pageInfo {
hasNextPage
endCursor
}
totalCount
}
}The same listing through the data client:
const files = (await foir.ns.files.files({
first: 20,
mimeType: 'image/',
folder: 'campaigns/spring-26',
})) as {
edges: Array<{ cursor: string; node: { id: string; filename: string; url: string } }>;
pageInfo: { hasNextPage: boolean; endCursor: string | null };
totalCount: number;
};Requires the files:read scope. Results are a cursor-paginated connection — page forward with first + after.
| Argument | Type | Description |
|---|---|---|
first | Int | Page size (default 50). |
after | String | Cursor from a prior page’s pageInfo.endCursor. |
folder | String | Limit to an exact folder path. |
mimeType | String | Limit to a Content-Type prefix — image/, video/, application/pdf. |
filename | String | Limit to filenames containing this substring. |
What a key can list depends on how it’s authenticated. Every file is either owned by the user who uploaded it, or has no user owner: shared library media uploaded through the console or an admin key (your CMS/media library), and an organization’s files, uploaded with no user behind the upload by a credential standing in that organization. The three cases:
- A request with a signed-in user (see User Authentication) lists the union of that user’s own files and the shared library media — i.e. own + global. It never includes files owned by a different user. So two users browsing the same project both see the full shared library, plus only their own uploads.
- A key without a user session lists the files no user owns, plus any user’s file whose access has been set to public. It never includes a user’s private file, even one whose bytes are served publicly: how the bytes are served and who may list the file are separate settings. This is the right default for a publishable key embedded in a browser bundle.
- To list every file across all users in one query — a manager or back-office view — use an admin key holding the
files:read.allscope. Because it crosses user boundaries,files:read.allcannot be granted to a publishable or a server key.
The same boundary applies to file(id): a key without files:read.all can fetch a file no user owns, a user’s file whose access has been set to public, and, for a signed-in user, that user’s own files. Another user’s private file resolves as if it doesn’t exist (null).
The File type
type File {
id: String!
filename: String!
mimeType: String
size: Int
url: String
width: Int
height: Int
blurhash: String
alt: String
folder: String
visibility: Visibility
createdAt: DateTime!
deletedAt: DateTime
}| Field | Notes |
|---|---|
id | Stable identifier you store on a record’s image/file field. |
filename | Canonical filename. May differ from the upload filename if it was sanitised. |
mimeType | Content-Type detected at upload. |
size | Bytes. |
url | Where the bytes are served from. For a PUBLIC file this is a direct, CDN-cacheable URL. For a PRIVATE one it carries a short-lived signed access token, so it is not a durable link — re-read the file rather than storing the URL. For image transforms see Media API. |
width / height | Image dimensions in pixels. null for non-images. |
blurhash | A BlurHash string for placeholder rendering. null for non-images. |
alt | Accessibility text. Editable via foir files update-metadata. |
folder | The folder the file is filed under; null at the project root. The files connection filters on this, so reading it is what makes the folder tree browsable rather than something you have to know in advance. |
visibility | PUBLIC or PRIVATE. See Visibility. Without it the only signal that a file is private is a 403 from the CDN, which looks the same as a bad URL, a missing scope or a deleted object. |
createdAt | When the file’s upload settled. |
deletedAt | When the file was deleted, or null. A deleted file keeps its id and can be undeleted. |
Visibility
Every file is served one of two ways, and uploadFile takes an optional visibility argument to choose:
| Value | How the bytes are served |
|---|---|
PUBLIC | A direct URL the CDN can cache. Anyone with the link can fetch it. |
PRIVATE | The URL carries a short-lived signed access token. Without a valid token the CDN refuses the request. |
Omit it and the default follows ownership. A file a user owns is stored PRIVATE: a signed-in user’s own upload, or an upload made by an operation that user triggered. A file no user owns is stored PUBLIC: the console or an admin key filling a media library, or an upload with no user behind it made while standing in an organization.
If you know what the file is for, say so. An explicit visibility at uploadFile always wins over the default:
mutation {
uploadFile(
filename: "logo.svg"
mimeType: "image/svg+xml"
size: 4821
visibility: PUBLIC
) { uploadId uploadUrl expiresAt landed { token endpoint } }
}The same request through the data client:
const upload = (await foir.ns.files.uploadFile({
filename: 'logo.svg',
mimeType: 'image/svg+xml',
size: 4821,
visibility: 'PUBLIC',
})) as { uploadId: string; uploadUrl: string; expiresAt: string | null; landed: { token: string; endpoint: string } };A PRIVATE file’s url is not a durable link. The token in it expires, so store the file id and read the file back when you need to serve it, rather than caching the URL.
To change a file’s visibility after it is stored, use setFileVisibility, below.
Lifecycle
Five mutations act on a file that already exists. Deletion is two-stage: deleteFile can be undone, purgeFile cannot.
| Mutation | Scope | What it does |
|---|---|---|
updateFile | files:write | Edits the file’s metadata — alt text, filename, folder. Authoring, not destruction, so it costs the same scope as creating one. |
setFileVisibility | files:write | Moves a stored file between PUBLIC and PRIVATE. |
deleteFile | files:delete | Moves the file to the trash. It keeps its id, stops being served, and deletedAt is set. |
undeleteFile | files:delete | Brings a file back from the trash. Priced as files:delete because it is the other half of that operation, not a write. |
purgeFile | files:delete | Removes the object from storage. There is no undo, and any record still pointing at the id is left pointing at nothing. |
files:write does not imply files:delete anywhere in the grammar, and it does not here: a key that can fill a media library cannot empty one unless it was given the delete scope as well.
Browser-direct uploads
Because uploadUrl is a signed URL and landed carries its own token, a signed-in user’s browser can run the whole flow itself, on your publishable key and their own bearer, with no server of yours in between. It needs files:write from the user’s role, and the file is theirs:
// The user's own client: your publishable key and their bearer.
const upload = (await foir.ns.files.uploadFile({
filename,
mimeType,
size,
})) as { uploadUrl: string; landed: { token: string; endpoint: string } };
// PUT the bytes directly to storage
await fetch(upload.uploadUrl, {
method: 'PUT',
headers: { 'Content-Type': file.type },
body: file,
});
// Say they have landed, and get the file
const created = await (
await fetch(upload.landed.endpoint, { method: 'POST', headers: { 'X-Foir-Token': upload.landed.token } })
).json();The bytes go straight from the browser to storage, never through Foir’s API or a server of yours.
Limits & validation
- Size:
sizemust be the exact byte length, greater than zero, and the upload URL refuses a PUT of any other length. Zero-byte files are refused atuploadFile: the signed exact length is what stops a file’s bytes being replaced after it lands, and an empty object carries no media. The workspace’s media storage allowance, and any project or organization cap within it, is checked against the stored size when the bytes land; an upload over it is refused, and its bytes are deleted. - MIME type: must be declared up front, and the PUT must send it as its Content-Type, because the upload URL signs it. Script and document types that would execute when served (HTML, JavaScript, XML) are refused at
uploadFile. A key confined byallowed_file_typesis also checked against the uploaded bytes when they land. - Filename: stored as sent.
- TTL on
uploadUrlandlanded.token: one hour fromuploadFile, and the bytes must land within that hour. After that, start a new upload. - Landing twice: an upload lands once. A second POST for the same upload after the first succeeded answers
404; read the file withfile(id)instead. One refused while another for the same upload is still running can be retried.
Related
- Media & Files — feature overview.
- Media API — image transforms (resize, crop, format conversion) applied to
File.url. foir media upload: the CLI runs all three steps in one command.foir files— CLI for managing files after upload (rename, tag, move, delete).