AI Endpoints

MoonDB includes built-in AI endpoints powered by Cloudflare Workers AI. Define endpoints in your schema, call them via the REST API, and pay with credits.

Defining AI endpoints

Add an ai_endpoints section to your schema:

{
  "tables": { ... },
  "ai_endpoints": {
    "summarize": {
      "model": "gemma",
      "prompt": "Summarize this text in 2 sentences: {{text}}",
      "access": "auth"
    },
    "generate_avatar": {
      "model": "flux-schnell",
      "prompt": "A minimal avatar for a user named {{name}}, flat design",
      "access": "auth"
    }
  }
}

Template parameters ({{name}}) are filled from the request body at call time.

Available models

AliasTypeCreditsDescription
gemmaText + Vision1/2 per 1K in/outFast & cheap. 256K context. Supports image analysis.
gpt-ossText2/4 per 1K in/outMost intelligent. Complex reasoning, code gen.
flux-schnellImage gen10 per imageFast image generation for prototyping.
flux-devImage gen100 per imageHigh quality, photorealistic images.

Calling an endpoint

POST /p/{project_id}/ai/{endpoint_name}
Authorization: Bearer {token}
Content-Type: application/json

{
  "text": "MoonDB is a DBaaS built for coding agents..."
}

Vision (image analysis)

gemma supports image input. Add an image field to the request body as a base64 data URI or an https URL:

POST /p/{id}/ai/describe_photo
{
  "instruction": "What objects are in this image?",
  "image": "data:image/png;base64,iVBOR..."
}
// or
{
  "instruction": "Describe this photo",
  "image": "https://example.com/photo.jpg"
}

The image field is a reserved parameter — it doesn't need a {{image}} placeholder in the prompt template. It's automatically attached as a vision input. Same credits as text — no extra charge for image analysis.

Response

Text models (including vision) return:

{ "data": { "result": "...", "model": "gemma", "credits_used": 3 } }

Image generation models return base64-encoded data:

{ "data": { "image_base64": "iVBOR...", "content_type": "image/png", "model": "flux-schnell", "credits_used": 10 } }

Access control

Each endpoint has an access field:

Credits

Each plan includes free credits per month. When exhausted, buy more via POST /v1/billing/ai-credits. Check your balance in Dashboard → AI.