Uploading files for memories
Create a file memory, get a presigned upload URL, and upload the file from your server.
This guide walks through the full process of uploading a file to create a memory in Fency.ai. File memories can then be used as context in Explore memories.
The upload flow consists of three steps: creating an empty file memory, obtaining a presigned upload URL, and uploading the file directly to that URL. These calls are made from your server using your secret key.
Step 1: Create a memory
Call Create file memory (POST /v1/memories with sourceType: "FILE") to create an empty memory record. This returns a memoryId you will use in the next step.
Step 2: Create an upload for the memory
Call Create memory upload (POST /v1/memories/:id/uploads) with the file's fileName, fileSize, and mimeType. This returns a presigned S3 upload URL along with the required form fields.
Step 3: Upload the file
Post a FormData payload containing all the returned S3 fields plus the file itself to the presigned uploadUrl.
// Step 1: Create a memory with sourceType FILE
const createMemoryResponse = await fetch('https://api.fency.ai/v1/memories', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.FENCY_SECRET_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
memoryTypeId: 'mty_...',
sourceType: 'FILE',
title: 'My document',
}),
})
const { memoryId } = await createMemoryResponse.json()
// Step 2: Create an upload for the memory to get a presigned URL
const createUploadResponse = await fetch(
`https://api.fency.ai/v1/memories/${memoryId}/uploads`,
{
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.FENCY_SECRET_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
fileName: file.name,
fileSize: file.size,
mimeType: file.type,
}),
}
)
const upload = await createUploadResponse.json()
// Step 3: Upload the file directly to the presigned URL
const formData = new FormData()
formData.append('key', upload.key)
formData.append('policy', upload.policy)
formData.append('x-amz-algorithm', upload.xAmzAlgorithm)
formData.append('x-amz-credential', upload.xAmzCredential)
formData.append('x-amz-date', upload.xAmzDate)
formData.append('x-amz-signature', upload.xAmzSignature)
formData.append('x-amz-security-token', upload.sessionToken)
formData.append('file', file)
await fetch(upload.uploadUrl, {
method: 'POST',
body: formData,
})Knowing when the memory is ready
After the upload completes, Fency processes the file asynchronously. Once the memory is ready to use, a memory.updated webhook event is sent to your registered webhook endpoint. See Events in the API reference for the event payload.
For full details on the request parameters, see the API reference for Create file memory and Create memory upload.