Learn how to use the File Uploads API to securely collect files from users without authentication.
The File Uploads API allows you to create pre-signed URLs that users can use to upload files directly to Cortex without requiring an API key. This eliminates the need to implement authentication proxies for file uploads, making it easier to collect files from end-users securely.
import { Cortex } from '@cortex-ai/sdk';const cortex = new Cortex({apiKey: process.env.CORTEX_API_KEY,});const uploadInfo = await cortex.apps.files.uploads.create();// Share uploadInfo.upload_url with your users// Store uploadInfo.future_file_id to reference the file later
The response includes:
upload_url: URL where users can upload files
future_file_id: ID that will be assigned to the file once uploaded
Once you have the upload URL, you upload files to it:
Copy
Ask AI
const formData = new FormData();formData.append('file', fileObject); // fileObject from input[type="file"]// Upload the fileawait fetch(uploadInfo.upload_url, {method: 'POST',body: formData});// Upload a file from a URLawait fetch(uploadInfo.upload_url, {method: 'POST',body: JSON.stringify({ file_url: 'https://example.com/files/document.pdf' })});
After a file is uploaded, The uploaded file is now available using the future_file_id and can be referenced wherever the upload URL was created, such as when running a workflow that requires a file input, for example:
Copy
Ask AI
import { Cortex } from '@cortex-ai/sdk';const cortex = new Cortex({apiKey: process.env.CORTEX_API_KEY,});// Run a workflow with the uploaded fileconst run = await cortex.apps.workflows.runs.create('YOUR_WORKFLOW_ID', {input: {documents: [uploadInfo.future_file_id]}});