- 06 Oct 2026
- Print
- DarkLight
Build an AI Image Generation App with Backblaze B2
- Updated on 06 Oct 2026
- Print
- DarkLight
Image generation applications often transform a brief user request and optional reference images into a detailed prompt before generating one or more images. Iterative workflows can produce many prompts, reference images, generated images, and metadata records, requiring scalable object storage.
This guide shows you how to install and use the Backblaze Image Generation Prompt Flow repository. This reference application visualizes the stages used to analyze and expand a prompt, compares output from supported image-generation models, and stores reference and generated images in Backblaze B2.
Understand How the App Works
The application processes an image-generation request through the following flow:
.jpg)
Processing Steps
The application completes the following steps:
Accepts the input: Accepts a text description and optional reference images.
Stores and analyzes reference images: Uploads reference images to Backblaze B2 and uses Google Gemini to identify their visual elements, style, composition, colors, mood, subjects, and technical characteristics.
Analyzes the request: Uses Google Gemini to identify the intent, subjects, style, composition, mood, and technical requirements.
Develops the prompt: Displays the app's Thinking stage as it develops details such as the subject, action, environment, style, composition, lighting, and technical specifications.
Constructs the prompt: Combines the original request, request analysis, reference-image analysis, and developed details into an expanded prompt.
Generates images: Sends the expanded prompt to the selected image-generation models in parallel.
Stores the results: Uploads generated images to Backblaze B2 and records generation metadata, prompt history, and Backblaze B2 object keys in a local SQLite database.
Displays the results: Shows the prompt-processing stages in the center panel and the model outputs in the right panel.
Prerequisites
Before you begin, you must have the following items:
A Backblaze account with Backblaze B2 Cloud Storage enabled
A Backblaze B2 bucket, noting the endpoint host and region
A Backblaze B2 application key that is restricted to the bucket
Select Read and Write, and select Allow List All Bucket Names.
Save the keyID and matching applicationKey when you create it; Backblaze shows the applicationKey only once.
Node.js 24 or later
A Google AI API key, which the application requires for request analysis, reference-image analysis, prompt development, and prompt construction
To generate images with the Gemini image models, the key must have billing enabled. Free-tier keys return a quota error for image generation.An OpenAI API key with available credits if you want to generate images with OpenAI
The application uses presigned URLs to provide temporary access to images stored in Backblaze B2. You do not need to make the bucket public.
Clone and Install the Repository
Open a terminal.
Run the following commands:
git clone https://github.com/backblaze-b2-samples/image-generation-prompt-flow.git cd image-generation-prompt-flow npm install
Configure Environment Variables
The app needs to know where to store images and which AI models to call.
Copy the example environment file:
cp .env.example .envOpen
.envin your code editor.Enter the following values:
DATABASE_URL: The path to the SQLite database. Use./data/sqlite.dbfor the default local configuration.GOOGLE_AI_API_KEY: Your Google AI API key. The application requires this variable even if you generate images only with OpenAI.B2_APPLICATION_KEY_ID: Your Backblaze B2 application key ID.B2_APPLICATION_KEY: Your Backblaze B2 application key.B2_BUCKET_NAME: The name of your Backblaze B2 bucket.B2_REGION: The region of your Backblaze B2 bucket, such asus-west-004. Use the region shown for your bucket in the Backblaze web console.B2_PUBLIC_URL_BASE: The S3-compatible URL for your bucket, such ashttps://s3.us-west-004.backblazeb2.com/<bucket-name>. The region must matchB2_REGION. The value must be an HTTPS URL with no credentials, query string, or fragment.IMAGE_URL_TTL_SECONDS: The number of seconds that generated presigned image URLs remain valid. The default is900, and the supported range is 1 through 604800 seconds.OPENAI_API_KEY: Your OpenAI API key. This variable is optional if you do not plan to use OpenAI.
Initialize the Database
The application uses a local SQLite database to track prompt history, generation attempts, and image metadata. The application creates the database file and its tables the first time it starts, so no separate initialization step is required..
Run the initialization script:
./scripts/init-db.shAlternatively, run the following command to push the schema to the database:
npm run db:push
Run and Use the App
Start the development server:
npm run devIn a browser, open
http://localhost:3000.Enter an image-generation prompt, such as
A cat dressed in a space uniform on a starship.To include one or more reference images, click Upload Reference Images, and select the images.
Select at least one image-generation model. The repository currently includes the following models:
gemini-3-pro-imagegemini-3.1-flash-imagegpt-image-1gpt-image-2
If you did not configure
OPENAI_API_KEY, clear the gpt-image-1 and gpt-image-2 checkboxes. All four models are selected by default.Click Generate.
Review the User Request, Request Analysis, Thinking, Prompt Construction, and Image Generation stages in the center panel.
Compare the model outputs in the right panel.
To review an earlier request and its results, select it in the left panel.
Note
If a step fails with a
503or429error from Google or OpenAI, wait and try again, and check the plan, quota, and credits on the API key.
Customize Prompt Processing
The application defines its prompt-processing instructions in src/lib/flow/steps.ts. Modify the system prompts in this file to change how the application analyzes reference images, interprets requests, develops prompt details, or constructs the final expanded prompt.
For example, you can update the instructions to apply a consistent visual style or include specific photographic parameters in every expanded prompt. Test your changes locally before deploying the application.
Next Steps
Compare model outputs: Run the same prompt through multiple Google and OpenAI models to compare how each model interprets the expanded prompt. See the Gemini image-generation documentation and the OpenAI image-generation documentation for model capabilities and options.
Experiment with reference images: Upload one or more reference images to see how their visual characteristics influence the request analysis and expanded prompt. See Image understanding in the Gemini API documentation.
Review prompt history: Use the application history to inspect previous requests, generated images, and model-specific versions. See the repository README for more information about version history and the application’s architecture.
Customize prompt processing: Modify the system prompts in src/lib/flow/steps.ts to support the styles, constraints, or use cases required by your application.
Adjust image access duration: Change IMAGE_URL_TTL_SECONDS in your .env file. See .env.example for the default value.
Prepare the application for deployment: Follow the deployment guidance in the repository README. Configure persistent database storage before deploying to an environment with an ephemeral file system.
Explore more integrations: Learn about other Backblaze integrations and developer examples at Backblaze Labs.
Additional Resources
Help us improve this guide. If you find an error, notice outdated information, or have suggestions for improvement, email techpubs@backblaze.com.