Skip to main content
The Files API handles file uploads and downloads for records with file columns, providing seamless integration with object storage backends. Base Path: /api/records/v1

Upload Files with Records

Files are uploaded as part of record creation or updates using multipart form data.

Create Record with File

POST /api/records/v1/{name}

Path Parameters

string
required
Record API name

Request Format

Use multipart/form-data content type with both data fields and file fields:

File Column Types

TrailBase supports two file column types:
  1. Single File (FileUpload): One file per field
  2. Multiple Files (FileUploads): Array of files per field

Response

The file metadata is automatically stored in the record’s file column.

Update Record with File

PATCH /api/records/v1/{name}/{record}

File Replacement

When updating a file column:
  1. Old file is marked for deletion
  2. New file is uploaded
  3. Record is updated with new file metadata
  4. Old file is deleted asynchronously
If the update fails, newly uploaded files are automatically cleaned up to prevent orphaned files in storage.

Download Single File

Download a file from a single-file column.
GET /api/records/v1/{name}/{record}/file/{column_name}

Path Parameters

string
required
Record API name
string
required
Record ID
string
required
Name of the file column

Response

Returns the file content with appropriate headers:

Download File from Array

Download a specific file from a multi-file column.
GET /api/records/v1/{name}/{record}/files/{column_name}/{file_name}

Path Parameters

string
required
Record API name
string
required
Record ID
string
required
Name of the file array column
string
required
Original filename from the file metadata

File Metadata

When you create a record with files, the file column stores metadata:

File Column Schema

Single File Column

Multiple Files Column

Storage Configuration

TrailBase supports multiple storage backends:

Local Filesystem

Amazon S3

Google Cloud Storage

Azure Blob Storage

File Upload Constraints

File Size Limits

Configure maximum file size in your server settings:

Allowed File Types

Implement CHECK constraints or access rules to restrict file types:

Access Control

File downloads respect record-level access control:
Example:

File Lifecycle

Upload Flow

  1. Client sends multipart request with file(s)
  2. TrailBase validates file metadata
  3. File uploaded to object storage
  4. Record created/updated with file metadata
  5. On success, file is committed
  6. On failure, file is cleaned up

Delete Flow

  1. Record deleted (or file column updated)
  2. File marked for deletion in _file_deletions table
  3. Asynchronous cleanup process deletes file from storage
  4. Deletion retried up to 10 times on failure
  5. Abandoned after 10 failed attempts (logged as error)

Orphaned File Prevention

TrailBase uses transactional file management:
  • Files uploaded during failed operations are cleaned up
  • Database triggers track files for deletion
  • Background jobs handle async file cleanup
  • Failed deletions are retried automatically

JavaScript/TypeScript Client

Upload File

Upload Multiple Files

Download File

Update File

React File Upload Component

Image Optimization

For image files, consider implementing client-side compression:

Direct File URLs

File download URLs are deterministic based on the record ID and column name:
You can construct URLs client-side without additional API calls:
Never expose auth tokens in public URLs. Use cookie-based auth for public file access or implement signed URLs.

File Metadata API

File metadata is stored directly in the record. Fetch it via the standard read endpoint:

Best Practices

  1. Validate files client-side before upload to improve UX
  2. Compress images before upload to reduce bandwidth and storage
  3. Use appropriate content types for accurate file handling
  4. Implement progress indicators for large file uploads
  5. Handle upload failures gracefully with retry logic
  6. Clean up blob URLs after use to prevent memory leaks
  7. Set reasonable file size limits based on your use case
  8. Use signed URLs for public file access instead of embedding tokens
  9. Consider CDN integration for frequently accessed files
  10. Monitor storage usage and implement cleanup policies

Troubleshooting

Upload Fails with 413 Payload Too Large

Increase the server’s max upload size:

Files Not Deleted After Record Deletion

  1. Check _file_deletions table for pending deletions
  2. Verify object storage credentials
  3. Review server logs for deletion errors
  4. Files are retried up to 10 times before being abandoned

Cannot Download File (403 Forbidden)

  1. Verify user has read access to the record
  2. Check row-level access rules
  3. Confirm file column is not excluded from API
  4. Ensure auth token is valid and not expired

File Upload Succeeds but Record Creation Fails

Files are automatically cleaned up when the transaction fails. This is expected behavior to prevent orphaned files.

Error Responses

Bad Request
Invalid file format, missing required fields, or constraint violation
Forbidden
Access denied by ACL or row-level access rule
Not Found
Record, column, or file not found
Method Not Allowed
API doesn’t support file operations or wrong HTTP method
Payload Too Large
File exceeds maximum upload size
Internal Server Error
Storage backend error or unexpected server failure