Skip to main content
POST
AI Undress
This endpoint generates AI-transformed images using advanced image editing models. It supports two quality tiers with automatic provider selection and fallback logic.

Authentication

Requires user authentication and explicit consent acceptance.
Mandatory Consent: Users must accept the AI Undress policy (version 1) via /api/undress/consent before using this endpoint. Requests without accepted consent will be rejected with 403 status.

Credit Cost

Quality Tiers:
Credits are deducted upfront before generation starts. Automatic refunds are issued on generation failures.

Rate Limits

  • Daily Limit: 30 generations per user per 24-hour period
  • Concurrent Limit: 3 active generations per user
  • Reset: Daily limit resets at midnight UTC

Request

image
file
required
The image file to transform (multipart/form-data upload).Supported Formats:
  • JPEG (image/jpeg, image/jpg)
  • PNG (image/png)
  • WebP (image/webp)
Size Limit: 25MB maximum
quality
string
required
The quality tier to use:
  • low - Freepik Seedream 4.5 Edit (25 credits)
  • high - xAI Grok Image Pro (45 credits)

Request Example (multipart/form-data)

Response

success
boolean
Whether the generation succeeded.
resultUrl
string
The URL of the generated image (Supabase storage).
generationId
number
Database ID for the generation record.
quality
string
The quality tier used (low or high).
provider
string
The actual provider used:
  • freepik - Freepik Seedream 4.5 Edit
  • xai - xAI Grok Image API
processingTimeMs
number
Time taken for generation in milliseconds.
creditsCharged
number
Number of credits deducted for this generation.
creditsRemaining
number
User’s remaining credit balance after deduction.

Success Response

Error Responses

error
string
Error message describing what went wrong.
code
string
Error code for programmatic handling:
  • DAILY_LIMIT_REACHED - User hit 30/day limit
  • CONCURRENT_LIMIT_REACHED - User has 3 active generations
  • INSUFFICIENT_CREDITS - Not enough credits
  • PROVIDER_UNAVAILABLE - Both providers are down

Error Examples

Implementation Details

Provider Strategy

Low Quality (quality=low):
  1. Primary: Freepik Seedream 4.5 Edit (40s budget)
  2. Fallback: xAI Grok Image (on timeout/failure)
High Quality (quality=high):
  1. Primary: xAI Grok Image Pro (50s budget)
  2. Fallback: Freepik Seedream 4.5 Edit (on timeout/failure)
Configuration:
  • Set UNDRESS_PRIMARY_PROVIDER=freepik to prefer Freepik for high quality
  • Default: xAI is primary for high quality

Freepik Integration

API: Freepik Seedream v4.5 Edit (/v1/ai/text-to-image/seedream-v4-5-edit)
Model ID: freepik/seedream-v4-5-edit
Request Flow:
  1. Create task with enable_safety_checker: false
  2. Poll for completion (1s intervals, 55s timeout)
  3. Select highest-resolution output from generated URLs
Timeouts:
  • Task creation: 25s
  • Polling interval: 1s
  • Total poll timeout: 55s
  • Output selection: 15s
Retry Logic:
  • URL attempts fallback to base64 on fetch failures
  • Transient errors (429, 5xx) use exponential backoff
  • Safety checker rejections are non-retriable

xAI Integration

API: xAI Image Edits (/v1/images/edits)
Models:
  • Primary: grok-imagine-image-pro (configurable via XAI_UNDRESS_MODEL)
  • Fallback: grok-imagine-image
Request Parameters:
Retry Logic:
  • 4 transient retries (429, 5xx) with exponential backoff (500ms → 4s)
  • URL to base64 fallback on fetch failures
  • Model fallback (pro → standard) on persistent failures

Prompt Engineering

Default Freepik Prompt:
Default xAI Prompt:
Custom Prompts:
  • Admins can override via /api/undress/prompt/update
  • Stored per-user in users table
  • Max length: 4096 characters

Image Processing

Upload Flow:
  1. Original uploaded to users/{userId}/undress/original/
  2. Provider input prepared (aspect ratio normalized)
  3. Provider input uploaded to users/{userId}/undress/provider/
  4. Result saved to users/{userId}/undress/result/
Aspect Ratio Normalization (Freepik):
Provider Input Preparation:
  • Analyze dimensions with Sharp
  • Select nearest Freepik aspect ratio
  • Preserve metadata for xAI

Credit Management

Upfront Deduction:
Automatic Refund:
Refund Triggers:
  • Generation returns error
  • Provider times out
  • No output URL returned
  • Safety checker rejection (Freepik)

Database Tracking

Table: undress_generations

Usage Notes

Consent Policy: Users MUST accept the AI Undress consent policy before accessing this endpoint. Check /api/undress/consent/status to verify consent state.
Provider Status: Check /api/undress/status before initiating generations to avoid hitting unavailable providers. The endpoint returns detailed provider availability.
Quality Selection: Use low quality for faster results and lower cost. Reserve high quality for cases where superior output quality justifies the extra 20 credits.
Automatic Cleanup: Failed generations are automatically cleaned up after 24 hours. Stuck generations can be manually cleared via /api/undress/clear-stuck.