This Neon Function generates an image through Neon AI Gateway, stores the JPEG in Neon Object Storage, and returns a presigned URL.
Scaffold it
Install the latest Neon CLI, then create the function from the registry:
npm install -g neon@latest
neon login
neon link
neon functions new image-generation --slug imagegenerationThe CLI writes functions/imagegeneration/index.ts and declares the function in neon.ts.
Enable AI Gateway and declare the private images bucket:
neon config add ai-gateway
neon config add bucket imagesYou can also add both to neon.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 and Object Storage get started.
Deploy once so Neon creates the bucket and injects the AWS_* and NEON_AI_GATEWAY_* variables into .env.local:
neon deployRun locally
neon devCall the URL that neon dev prints (the default is http://localhost:8787/):
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:
{
"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.








