App file storage
Apps built on the API builder can store files: a store's product photos, a blog's covers, members-only PDFs, or files customers upload themselves. Files belong to one API, live at /_api/<apiId>/files, and are tied to that API's sign-in.
POST /_api/<apiId>/files?name=&access= upload (raw body)
GET /_api/<apiId>/files[?mine=1] list
GET /_api/<apiId>/files/<name> read one file
DELETE /_api/<apiId>/files/<name> delete one fileThe host is https://devmachinist.com, or a published site's own address. API ids look like mhz0k4w0x7q2rd. Like the rest of /_api, these routes are open to any origin and use no cookies: send the app's sign-in token as Authorization: Bearer <token> (see tokens).
Before you start #
- Sign-in must be on for the API (API → Auth). Without it, uploading, listing and deleting answer
403 Turn on sign-in for this API to store files. Files already stored stay readable. - Who can upload is set in API → Auth → Who can upload files (
api.auth.uploads):
| Setting | Who may upload |
|---|---|
admin (default) | Accounts with role admin. |
user | Every signed-in account. |
a custom role, such as editor | Accounts with that role, and admins. |
Admins can always upload.
Upload a file #
Send the file's bytes as the request body. This is not a multipart form: the whole body is the file.
| Query | Notes |
|---|---|
name | The original file name, kept for listings and downloads (up to 200 characters). Optional. |
access | public (default), private, users, or one of the API's custom roles. |
curl -X POST 'https://devmachinist.com/_api/mhz0k4w0x7q2rd/files?name=mug.jpg&access=public' \
-H 'Authorization: Bearer eyJhbGciOi...' \
-H 'Content-Type: image/jpeg' \
--data-binary @mug.jpg{
"ok": true,
"name": "3f9a1c0b7d2e4f6a8b1c.jpg",
"url": "https://devmachinist.com/_api/mhz0k4w0x7q2rd/files/3f9a1c0b7d2e4f6a8b1c.jpg",
"size": 184213,
"type": "image/jpeg",
"access": "public"
}Save url in a text or url field of your model (a product's image, a post's cover). Never store files as base64 data URLs in your rows.
Types and sizes #
The type comes from the file's first bytes, never from its name or the Content-Type header:
| Kind | Types |
|---|---|
| Pictures | PNG, JPEG, GIF, WebP, AVIF |
| Documents | PDF, ZIP |
| Video | MP4, MOV, WebM |
| Audio | MP3, WAV, Ogg |
SVG, HTML and anything else is refused with 415. A ZIP can't be public: store it as private, users or a role. Each file can be up to 25 MB.
Space #
Every Devmachinist account has 1 GB of upload space by default, shared by its asset store and the files of all its apps. Files your app's visitors upload count against the account that owns the API, not against the visitors. Admins see used and quota (bytes) when they list files; quota is null when there is no limit.
Errors #
| Status | When |
|---|---|
| 400 | access isn't one of the allowed words, for example access must be public, private, users, editor. |
| 401 | No token, or the token is invalid or expired: Authentication required. |
| 403 | Sign-in is off for the API, Admins only, or Your account can’t upload files here. |
| 413 | The file is over 25 MB, or the account's space is full (the body then has "quota": true). |
| 415 | Not a supported type, or a public ZIP. |
| 429 | More than 120 uploads and deletes from one IP in 10 minutes. |
Every error body carries an error message, for example { "ok": false, "error": "Files can be up to 25.0 MB." }.
Access levels #
Each file has one access level, chosen when it is uploaded. It decides both the file's name and who can read it.
| Access | Who can read it | File name |
|---|---|---|
public | Anyone with the URL, no token. | First 20 hex characters of the file's SHA-256, then the extension, such as 3f9a1c0b7d2e4f6a8b1c.jpg. |
private | The account that uploaded it, and admins. | p- and 20 random hex characters, such as p-8e41b07c2d9a5f13c6e0.pdf. |
users | Any signed-in account. | p-… as above. |
a custom role, such as editor | Accounts with that role, admins, and the uploader. | p-… as above. |
Every file's URL has the same form:
https://devmachinist.com/_api/<apiId>/files/<name>Public names come from the content, so uploading the same picture twice gives the same URL and stores it once. Non-public names are random and unguessable, but the name alone never grants access: reading one needs a token.
Read a file #
GET /files/<name>. A public file needs nothing:
curl -O https://devmachinist.com/_api/mhz0k4w0x7q2rd/files/3f9a1c0b7d2e4f6a8b1c.jpgA non-public file (p-…) needs the reader's token:
curl -O https://devmachinist.com/_api/mhz0k4w0x7q2rd/files/p-8e41b07c2d9a5f13c6e0.pdf \
-H 'Authorization: Bearer eyJhbGciOi...'Without a token that is 401 Authentication required; with a token that isn't allowed, 403 Not allowed. An unknown name is 404 with an empty body.
Because a browser doesn't send the token for <img src>, <video src> or a plain link, non-public files can't be shown that way. Fetch them with the token and use an object URL, which is what useFileUrl and <FileImage> do for you:
const res = await fetch(url, { headers: { Authorization: 'Bearer ' + localStorage.getItem('pb_auth_mhz0k4w0x7q2rd') } })
if (!res.ok) throw new Error(res.status === 401 ? 'Sign in to see this file' : 'Not allowed')
const objectUrl = URL.createObjectURL(await res.blob())Response headers #
| Header | Value |
|---|---|
Content-Type | From the stored type, such as image/jpeg. |
Cache-Control | Public files: public, max-age=31536000, immutable. Others: private, max-age=600. |
X-Content-Type-Options | nosniff |
Content-Security-Policy | default-src 'none'; sandbox |
Cross-Origin-Resource-Policy | cross-origin, so other sites can embed public pictures. |
Content-Disposition | PDF and ZIP only: attachment with the original name when it has the right extension, else file.pdf or file.zip. |
Pictures, video and audio are served inline; PDFs and ZIPs download.
List files #
GET /files needs a signed-in account. Admins get every file of the API; everyone else gets only the files they uploaded. Admins can add ?mine=1 to see just their own. Files come newest first, at most 200.
curl https://devmachinist.com/_api/mhz0k4w0x7q2rd/files -H 'Authorization: Bearer eyJhbGciOi...'{
"ok": true,
"files": [{
"name": "p-8e41b07c2d9a5f13c6e0.pdf",
"url": "https://devmachinist.com/_api/mhz0k4w0x7q2rd/files/p-8e41b07c2d9a5f13c6e0.pdf",
"access": "private",
"type": "application/pdf",
"size": 482211,
"original": "invoice-0042.pdf",
"uploader": "mhz1a9c2b4k7mn",
"at": "2026-11-13T10:41:07Z"
}],
"used": 52428800,
"quota": 1073741824,
"maxFile": 26214400,
"uploads": "admin"
}uploader is the account id, or null for files stored from the builder. used and quota appear for admins only. uploads is the API's upload setting.
Delete a file #
DELETE /files/<name> answers { "ok": true }. Admins can delete any file; other accounts only files they uploaded (403 Only an admin or the account that uploaded it can delete this file).
curl -X DELETE https://devmachinist.com/_api/mhz0k4w0x7q2rd/files/p-8e41b07c2d9a5f13c6e0.pdf \
-H 'Authorization: Bearer eyJhbGciOi...'A public file is shared by everyone who uploaded the same bytes, and it is recorded under whoever uploaded it first: only that account or an admin can delete it. Deleting the API in the builder deletes all its files.
In React #
The generated auth client (src/api/<slug>-auth.jsx, see the client) has helpers that send the token for you.
| Helper | What it does |
|---|---|
uploadFile(file, { access }) | Uploads a File or Blob and returns { ok, url, name, size, type, access }. access defaults to public. Throws with the server's message on failure. |
useFileUrl(url) | Returns { url, loading, error }: the URL as is for public files, or an object URL fetched with the token for p-… files. |
<FileImage src={url} /> | An <img> that works for any app file. Other props go to the <img>. |
downloadFile(url, filename) | Saves a file, fetching non-public ones with the token. |
isPrivateFile(url) | true for p-… file URLs. |
files.list({ mine }) | The list response above (it doesn't throw; check ok). |
files.remove(nameOrUrl) | Deletes a file by name or full URL. |
Upload a product photo #
import { useState } from 'react'
import { uploadFile } from '../api/shop-auth.jsx'
import { Products } from '../api/shop.js'
export default function PhotoField({ productId }) {
const [busy, setBusy] = useState(false)
const [error, setError] = useState('')
const onPick = async (e) => {
const file = e.target.files[0]
if (!file) return
setBusy(true); setError('')
try {
const { url } = await uploadFile(file, { access: 'public' })
await Products.update(productId, { image: url })
} catch (err) { setError(err.message) } finally { setBusy(false) }
}
return (
<label className="block">
<input type="file" accept="image/*" onChange={onPick} disabled={busy} />
{error && <p className="text-sm text-red-600">{error}</p>}
</label>
)
}Show a members-only file #
import { Auth, FileImage, useFileUrl, downloadFile } from '../api/blog-auth.jsx'
function Bonus({ post }) {
const pdf = useFileUrl(post.bonus_pdf)
return (
<Auth>
<FileImage src={post.cover} alt="" className="rounded-xl" />
{pdf.url && <iframe src={pdf.url} title="Bonus" className="h-96 w-full" />}
<button onClick={() => downloadFile(post.bonus_pdf, 'bonus.pdf')}>Download</button>
</Auth>
)
}A customer's own uploads #
import { useEffect, useState } from 'react'
import { files, uploadFile } from '../api/shop-auth.jsx'
function MyFiles() {
const [list, setList] = useState([])
const load = () => files.list({ mine: true }).then(r => setList(r.files || []))
useEffect(() => { load() }, [])
return (
<div>
<input type="file" onChange={async e => { await uploadFile(e.target.files[0], { access: 'private' }); load() }} />
{list.map(f => (
<p key={f.name}>{f.original || f.name} <button onClick={() => files.remove(f.name).then(load)}>Delete</button></p>
))}
</div>
)
}For a customer to upload like this, set Who can upload files to every signed-in account (or a role they have).
From the builder #
You can also store a file that is already in the project (for example one dropped into the AI chat, in public/media/ or uploads/):
curl -X POST https://devmachinist.com/api/projects/<projectId>/app-files \
-H 'Authorization: Bearer dm_pat_...' -H 'Content-Type: application/json' \
-d '{"path":"public/media/mug.jpg","access":"public"}'It takes path (inside the project), access and an optional api_id when the project has more than one API, and answers like an upload with uploader set to null. GET /api/projects/<projectId>/app-files[?api_id=&access=] lists the files with the API's upload setting, access words, used and quota. These routes use your Devmachinist session or a personal access token, not the app's sign-in. In the AI chat the same actions are the app_files and store_app_file tools.
Related #
- App sign-in: tokens, roles and admins.
- The data API: saving file URLs in your models.
- The generated client: everything in
src/api/. - API builder: the Auth panel and its upload setting.