Skip to main content

Storing Generated Files

Files can be uploaded to Convex from a client or stored after they've been fetched or generated in actions and HTTP actions. For example you might call a third-party API to generate an image based on a user prompt and then store that image in Convex.

Example: Dall-E Storage & Action

Storing files in actions

Storing files in actions is similar to uploading a file via an HTTP action.

The actions takes these steps:

  1. Fetch or generate an image.
  2. Store the image using storage.store().
  3. Save the storage ID into your data model via a mutation.
convex/actions/sendDallE.js
  // Download the image
const imageResponse = await fetch(dallEImageUrl);
if (!imageResponse.ok) {
throw new Error(`failed to download: ${imageResponse.statusText}`);
}

// Store the image to Convex storage.
const image = await imageResponse.blob();
const storageId = await storage.store(image);

// Write storageId as the body of the message to the Convex database.
await runMutation("sendMessage:sendDallEMessage", {
body: storageId,
author,
prompt,
});