← Home

Generate the image and return it as base64 encoded string:

const openai = new OpenAI({ apiKey: env.OPENAI_API_KEY });
const response = await openai.images.generate({
  model: "dall-e-2",
  prompt: `Create an image of a beautiful landscape.`,
  n: 1,
  size: "1024x1024", // supports 1024x1024, 1024x1792 or 1792x1024 pixels
  response_format: "b64_json",
});
if (!response.data[0].b64_json) {
  throw new Error("Unable to generate image");
}
const base64 = response.data[0].b64_json;

Create a Blob from the base64 string:

const buffer = Buffer.from(imageBase64, "base64");
const blob = new Blob([buffer], {
  type: "image/jpeg",
});

Store the image somewhere or POST it to a server as FormData:

const formData = new FormData();
formData.append("image", blob, "image.png");
await fetch("/api/upload-image", {
  method: "POST",
  body: formData,
});
← Home

Erben Systems GmbH

Watterstrasse 81, c/o Sarbach Treuhand AG, 8105 Regensdorf, Switzerland

CHE-174.268.027 MwSt