File Operations
storage upload download delete replace files guideThis guide covers the core file operations available through the Storage API.
Upload Files
Section titled “Upload Files”Upload one or more files. Each uploaded file returns its metadata including the generated id.
const response = await nhost.storage.uploadFiles({ 'bucket-id': 'default', 'file[]': [selectedFile],})
const uploadedFile = response.body.processedFiles?.[0]# Basic uploadcurl -X POST \ -H "Authorization: Bearer $TOKEN" \ -F "file[]=@document.pdf" \ https://local.storage.local.nhost.run/v1/files
# Upload to a specific bucketcurl -X POST \ -H "Authorization: Bearer $TOKEN" \ -F "bucket-id=documents" \ -F "file[]=@report.pdf" \ https://local.storage.local.nhost.run/v1/files
# Upload multiple filescurl -X POST \ -H "Authorization: Bearer $TOKEN" \ -F "bucket-id=photos" \ -F "file[]=@photo1.jpg" \ -F "file[]=@photo2.jpg" \ https://local.storage.local.nhost.run/v1/filesDownload Files
Section titled “Download Files”Download a file by its ID. The response body contains the raw file content.
const response = await nhost.storage.getFile(fileId)const url = URL.createObjectURL(response.body)window.open(url, '_blank')// Download with image transformation (e.g. thumbnails)const { body } = await nhost.storage.getFile(fileId, { w: 100, h: 100, f: 'webp',})const url = URL.createObjectURL(body)# Download a filecurl -H "Authorization: Bearer $TOKEN" \ -o output.pdf \ "https://local.storage.local.nhost.run/v1/files/$FILE_ID"
# Get metadata headers onlycurl -I -H "Authorization: Bearer $TOKEN" \ "https://local.storage.local.nhost.run/v1/files/$FILE_ID"Replace Files
Section titled “Replace Files”Replace file content while keeping the same file ID. References to the file remain valid.
await nhost.storage.replaceFile(fileId, { file: newFile })curl -X PUT \ -H "Authorization: Bearer $TOKEN" \ -F "file=@report-v2.pdf" \ "https://local.storage.local.nhost.run/v1/files/$FILE_ID"The replace operation temporarily sets is_uploaded to false during the update, then restores it to true when complete.
Delete Files
Section titled “Delete Files”Permanently delete a file and its metadata.
await nhost.storage.deleteFile(fileId)curl -X DELETE \ -H "Authorization: Bearer $TOKEN" \ "https://local.storage.local.nhost.run/v1/files/$FILE_ID"Listing Files via GraphQL
Section titled “Listing Files via GraphQL”The Storage REST API handles file content operations. To list, search, or filter files, use the GraphQL API:
const response = await nhost.graphql.request<{ files: Array<{ id: string; name: string; size: number; mimeType: string; bucketId: string; uploadedByUserId: string }>}>({ query: `query GetFiles { files { id name size mimeType bucketId uploadedByUserId } }`,})
const files = response.body.data?.files ?? []This is the same GraphQL API used for the rest of your application data. Permissions are evaluated automatically based on the user’s session.