Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions node/image-generation-with-gemini/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Directory used by Appwrite CLI for local development
.appwrite
6 changes: 6 additions & 0 deletions node/image-generation-with-gemini/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true
}
108 changes: 108 additions & 0 deletions node/image-generation-with-gemini/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# ⚡ Image Generation with Gemini (Node)

Generate images using Google's Gemini API, save them locally, and automatically upload them to an Appwrite Storage bucket.

## 🧰 Usage

### CLI

- Generate and upload to Appwrite Storage

```powershell
node src/main.js "A cat running inside a stadium"
```



### Bucket setup helper (optional)

Ensures the bucket exists (creates it if missing):

```powershell
node src/setup.js
```
Or the upload function in utils.js does this itself.

Images are written to `./output` as `gemini-image-<n>.png`.

## ⚙️ Configuration

| Setting | Value |
| ----------------- | ---------------------------- |
| Runtime | Node (>=18) |
| Entrypoint | `src/main.js` |
| Output folder | `output/` |
| Upload provider | Appwrite Storage |
| Bucket (default) | `Generated_Images` |

> Note: If you plan to run this as an Appwrite Function, set Entrypoint to `src/main.js` and ensure the environment variables below are configured in Appwrite as well.

## 🔒 Environment Variables

Set these in a local `.env` file (or inject in your platform):

| Variable | Required | Sample Value | Notes |
| --------------------------------- | -------- | -------------------------------------------- | ---------------------------------------------------------------- |
| `GEMINI_API_KEY` | Yes | `AIza...` | Google Gemini API key |
| `GEMINI_MODEL` | No | `gemini-2.0-flash-preview-image-generation` | Defaults to the value shown |

| `APPWRITE_FUNCTION_API_ENDPOINT` | Yes\* | `https://cloud.appwrite.io/v1` | Appwrite endpoint |
| `APPWRITE_FUNCTION_PROJECT_ID` | Yes* | `YOUR_PROJECT_ID` | Appwrite project ID |
| `APPWRITE_FUNCTION_API_KEY` | Yes* | `YOUR_API_KEY` | Appwrite API key with Storage permissions |
| `APPWRITE_BUCKET_ID` | No | `Generated_Images` | Bucket ID used/created |

\* Required only when uploading to Appwrite.

Example `.env` (placeholder values):

```dotenv
GEMINI_API_KEY=YOUR_GEMINI_API_KEY
GEMINI_MODEL=gemini-2.0-flash-preview-image-generation


# Appwrite creds (needed when uploading)
APPWRITE_FUNCTION_API_ENDPOINT=https://cloud.appwrite.io/v1
APPWRITE_FUNCTION_PROJECT_ID=YOUR_PROJECT_ID
APPWRITE_FUNCTION_API_KEY=YOUR_API_KEY
APPWRITE_BUCKET_ID=Generated_Images
```


## ✅ Expected output

On success, you'll see logs like:

```text
Image saved as D:\...\output\gemini-image-10.png
✓ Image created at: D:\...\output\gemini-image-10.png
📤 Uploading to Appwrite...
✓ Image uploaded to Appwrite with file ID: <FILE_ID>
✓ Upload complete! File ID: <FILE_ID>
```


## 🧯 Troubleshooting


- "Cannot read properties of undefined (reading 'size')"
- Run `npm install` to ensure dependencies are present

- Use Node 18+ (the SDK relies on `fetch`, `File`, etc.)

- 401 / 403 errors when uploading
- Check `APPWRITE_FUNCTION_API_KEY` and `APPWRITE_FUNCTION_PROJECT_ID`
- Ensure the API key has Storage permissions


## 📦 Install

```powershell
npm install
```

Optionally, format code:

```powershell
npm run format
```

18 changes: 18 additions & 0 deletions node/image-generation-with-gemini/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
declare global {
namespace NodeJS {
interface ProcessEnv {
// Google Gemini
GEMINI_API_KEY: string;
GEMINI_MODEL?: string;

// Appwrite
APPWRITE_FUNCTION_API_ENDPOINT?: string;
APPWRITE_FUNCTION_PROJECT_ID?: string;
APPWRITE_FUNCTION_API_KEY?: string;
APPWRITE_BUCKET_ID?: string;

}
}
}

export {};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading