Skip to main content

Endpoint

GET /api/getImages

Description

This function retrieves a list of all images stored in the Azure Blob Storage “images” container. It returns URLs with SAS tokens for secure access and sorts the images by timestamp in descending order (newest first).

Request

No parameters required. This is a simple GET request.

Response

imageUrls
array
An array of image objects sorted by creation time (newest first).
url
string
The complete URL to access the image, including the SAS token for authentication.Format: https://{accountName}.blob.core.windows.net/images/{blobName}?{sasToken}
name
string
The blob name in the format {prompt}_{timestamp}.png

Behavior

  1. Connects to Azure Blob Storage using shared key credentials
  2. Lists all blobs in the “images” container
  3. Generates a fresh SAS token for secure access
  4. Constructs full URLs with SAS tokens for each image
  5. Sorts images by timestamp in descending order (newest first)
  6. Returns the sorted list as JSON

Example request

const response = await fetch('/api/getImages', {
  method: 'GET'
});

const data = await response.json();
console.log(data.imageUrls);
// [
//   {
//     url: "https://account.blob.core.windows.net/images/mountain_1234567890.png?sv=2021...",
//     name: "mountain_1234567890.png"
//   },
//   {
//     url: "https://account.blob.core.windows.net/images/ocean_1234567880.png?sv=2021...",
//     name: "ocean_1234567880.png"
//   }
// ]

Implementation details

The function extracts timestamps from filenames using this parsing logic:
const sortedImageUrls = imageUrls.sort((a, b) => {
  const aName = a.name.split("_").pop().toString().split(".").shift();
  const bName = b.name.split("_").pop().toString().split(".").shift();
  return bName - aName; 
})
This ensures images are displayed in reverse chronological order, with the most recently generated images appearing first.

Build docs developers (and LLMs) love