Endpoint
Overview
Thecabina-vision edge function handles the complete AI image generation pipeline:
- Load balances API keys from pool
- Uploads user photo to CDN
- Fetches AI prompt for selected style
- Creates Kie.ai generation task
- Polls for completion (up to 45 seconds)
- Stores result in Supabase Storage
- Records generation in database
- Deducts credits (event or user)
- Sends notifications (push/email/WhatsApp)
This function is critical for both B2C and B2B (event) workflows.
Request
Headers
Bearer token with Supabase anon key
Must be
application/jsonBody Parameters
Base64-encoded image or public URLFormat:
data:image/png;base64,iVBORw0KG... or https://...Max size: ~5MB (before encoding)Style identifier from
styles_metadata tableExamples: pb_a, suit_b, jhonw_cOutput image aspect ratioOptions:
1:1, 4:3, 3:4, 16:9, 9:16Authenticated user ID (for B2C mode)Mutually exclusive with
event_idEvent ID (for B2B/event mode)Mutually exclusive with
user_idTemporary guest identifier for event modeUsed for: Storage path and analytics
Email address to send notification (optional)
Phone number for WhatsApp notification (optional)
Operation typeOptions:
generate- Create new generationcheck- Check status of existing task
Kie.ai task ID (required when
action=check)Response
Success Response (Immediate)
Always
true for successful generationsPublic URL to the generated image
Polling Response (Still Processing)
Kie.ai task identifier for status checks
Current task state:
waiting, processing, success, or failError Response
Code Examples
Generate Image (Event Mode)
Generate Image (B2C Mode)
Check Task Status
Implementation Details
API Key Load Balancing
The function selects the least-recently-used active API key:supabase/functions/cabina-vision/index.ts:32-38
Credit Deduction (Event Mode)
Credits are decremented atomically via RPC:supabase/functions/cabina-vision/index.ts:69-83
Photo Upload Pipeline
Two-tier upload strategy:- Primary: Kie.ai CDN upload
- Fallback: Supabase Storage bucket
supabase/functions/cabina-vision/index.ts:90-135
AI Generation Request
Uses Kie.ai Nano Banana Pro model:supabase/functions/cabina-vision/index.ts:152-165
Internal Polling
Function polls for up to 45 seconds before returning task ID:supabase/functions/cabina-vision/index.ts:185-216
Error Codes
Photo upload failed on both Kie.ai and SupabaseSolution: Check image size and format
Insufficient AI credits in Kie.ai accountSolution: Top up Kie.ai account or add backup API key to pool
Invalid Kie.ai API credentialsSolution: Verify
BANANA_API_KEY secret is correctEvent has no remaining creditsSolution: Allocate more credits to the event
AI generation task failedSolution: Check Kie.ai dashboard for details, retry with different photo
Environment Variables
Auto-injected by Supabase
Auto-injected by Supabase
Kie.ai API key (fallback if pool is empty)Set via:
supabase secrets set BANANA_API_KEY=your-keyPerformance
Typical Response Times:- Upload + Task creation: 2-5 seconds
- AI generation: 10-30 seconds
- Storage + DB write: 1-2 seconds
- Total: 15-40 seconds
- Use multiple API keys in pool to distribute load
- Pre-compress images client-side to reduce upload time
- Consider client-side polling instead of server-side for faster response
Next Steps
Payment Webhook
Mercado Pago integration
AI Model Integration
Deep dive into AI pipeline