How rohitaryal/imagefx-api works
Project type and purpose
• This is an open-source, Node.js/TypeScript SDK + CLI wrapper (“imagefx”) for Google’s private ImageFX (Imagen) API.
• It exposes both a programmatic interface (ImageFX class, Prompt, Image, etc.) and a command-line interface (CLI entrypoint src/cli.ts).
File structure
• Top level:
• src/ – core TS source files
– index.ts → exports public API (ImageFX)
– cli.ts → CLI entrypoint, command parsing with Commander/Yargs
– Account.ts → handles cookie/auth management
– Prompt.ts → builds generation requests (seed, model, size)
– Image.ts, ImageFX.ts → models Image instances and core client logic
– Constants.ts, Types.ts → enum definitions and shared types
• examples/ – usage examples of both module and CLI
• tests/ – unit tests mapped to src modules
• .github/workflows – CI/CD (release, tests)
• package.json, tsconfig.json – build & packaging config
Key insights from README
• Exposes three major capabilities:
a) generate images from text
b) generate captions from existing images
c) fetch previously generated images by media ID
• Authentication via environment variable GOOGLE_COOKIE
• Offers both CLI commands (imagefx generate, caption, fetch) and JS/TS module API (new ImageFX(cookie), .generateImage(), .caption(), etc.).
Main components & their interactions
a. CLI Layer
– Parses user commands/flags (prompts, model, count, size).
– Instantiates ImageFX core client.
– Calls appropriate method (generateImage, caption, fetchMedia).
b. Core Library Layer
– ImageFX class
• Holds auth cookie
• Exposes async methods: generateImage(promptOrString), caption(imagePath), fetch(mediaId)
– Account module (Account.ts)
• Manages cookie header, token refresh if needed
– Prompt module (Prompt.ts)
• Builds typed request objects (seed, count, model, aspect ratio)
– Image and ImageFX model classes
• Wrap response data: media ID, binary data, metadata
• Provide .save(dir) to write files to disk
c. HTTP Client / Network Layer
– Under the hood, ImageFX methods issue HTTP requests to labs.google/fx endpoints.
– Handles retries, error handling.
d. External Service
– Google ImageFX API (private endpoints)
e. Filesystem
– Local disk for saving images.
Architectural patterns/design principles
• Single-process, modular design
• Separation of concerns:
• CLI parsing vs. core logic
• Request-building (Prompt) vs. network I/O (ImageFX) vs. persistence (Image.save)
• Typed interfaces and models (Types.ts, Constants.ts for enums)
• Layered architecture: CLI → Core client → HTTP → External API → File I/O