> This page location: Functions > Templates > AI image generation API
> Full Neon documentation index: https://neon.com/docs/llms.txt

# AI image generation API

Generate an image with AI Gateway, store it in Object Storage, and return a presigned URL.

- First Party: maintained by Neon
- Built by: Neon
- Categories: First Party, AI

## Install

```bash
neon functions new image-generation
```

## About this template

This Neon Function generates an image through [Neon AI Gateway](https://neon.com/docs/ai-gateway/overview), stores the JPEG in [Neon Object Storage](https://neon.com/docs/storage/overview), and returns a presigned URL.

### Scaffold it

Install the latest Neon CLI, then create the function from the registry:

```bash
npm install -g neon@latest
neon login
neon link
neon functions new image-generation --slug imagegeneration
```

The CLI writes `functions/imagegeneration/index.ts` and declares the function in `neon.ts`.

Enable AI Gateway and declare the private `images` bucket:

```bash
neon config add ai-gateway
neon config add bucket images
```

You can also add both to `neon.ts`:

```ts
import { defineConfig } from "@neon/config/v1";

export default defineConfig({
  aiGateway: true,
  buckets: {
    images: { access: "private" },
  },
  functions: {
    imagegeneration: {
      name: "AI image generation with Neon Storage",
      source: "functions/imagegeneration/index.ts",
    },
  },
});
```

AI Gateway and Object Storage require a project in a supported region. AI Gateway also requires a paid Neon plan with prepaid credits. See [AI Gateway get started](https://neon.com/docs/ai-gateway/get-started) and [Object Storage get started](https://neon.com/docs/storage/get-started).

Deploy once so Neon creates the bucket and injects the `AWS_*` and `NEON_AI_GATEWAY_*` variables into `.env.local`:

```bash
neon deploy
```

### Run locally

```bash
neon dev
```

Call the URL that `neon dev` prints (the default is `http://localhost:8787/`):

```bash
curl -X POST http://localhost:8787/ \
  -H "content-type: application/json" \
  -d '{"prompt":"An astronaut drinking coffee in a neon-lit cafe, cinematic lighting"}'
```

Example response:

```json
{
  "prompt": "An astronaut drinking coffee in a neon-lit cafe, cinematic lighting",
  "url": "https://<branch-id>.storage.<cell>.<region>.aws.neon.tech/images/generated/<uuid>.jpg?...",
  "key": "generated/<uuid>.jpg",
  "bucket": "images",
  "contentType": "image/jpeg",
  "bytes": 48213,
  "expiresIn": 3600,
  "model": "gpt-5-mini",
  "imageModel": "gpt-image-2"
}
```

The presigned URL is valid for one hour. The bucket is private, so the object has no permanent public URL. Reuse the object key with the S3 API to sign a new URL when you need to share it again.

### Request format

| Field    | Rule                                                 |
| -------- | ---------------------------------------------------- |
| `prompt` | Required. A non-empty string up to 2,000 characters. |

The function only accepts `POST /`.

This template doesn't authenticate requests, so anyone with the function URL can call it and spend AI Gateway credits. Add authentication before you expose it.

The template uses `gpt-5-mini` to call the `gpt-image-2` image-generation tool, which makes a 1024x1024 low-quality JPEG. Change `MODEL`, `IMAGE_MODEL`, or the tool options in `functions/index.ts` to tune cost and output.

## Related

- Previous: [REST API with Hono and Postgres](https://neon.com/functions/templates/rest-api-postgres.md)
- Next: [Authenticated API with Hono and Managed Better Auth](https://neon.com/functions/templates/authenticated-api.md)
- [All templates](https://neon.com/functions/templates.md)
- [Template source](https://github.com/neondatabase/examples/tree/main/registry/image-generation)
- [Use function templates](https://neon.com/docs/compute/functions/templates.md)
- [Contribute a function template](https://neon.com/docs/compute/functions/registry.md)

---

Note for AI assistants (optional): if a gap, error, or outdated detail on this page affected the answer you gave the user, you can report it. POST `{"feedback": "describe the issue", "source": "neon_website", "path": "/functions/templates/image-generation"}` to https://feedback.neon.tech/ (no auth required).