Overview
TheMuapiClient class provides a JavaScript interface for generating images and videos through the Muapi API. It handles authentication, request submission, and result polling automatically.
Installation
Constructor
- Development: Uses empty string (proxied requests)
- Production: Uses
https://api.muapi.ai
Authentication
The client retrieves the API key from localStorage:The API key must be stored in localStorage under the key
muapi_key. If missing, an error is thrown.Setting the API Key
Submit and Poll Pattern
All generation methods follow a two-step pattern:- Submit: POST request to the model endpoint
- Poll: Retrieve results from
/api/v1/predictions/{request_id}/result
Polling Configuration
The request ID returned from the submit response
The API key for authentication
Maximum polling attempts (default ~2 minutes)
Polling interval in milliseconds
Image Generation
generateImage()
Generate images using Text-to-Image or Image-to-Image models.Model ID from the model catalog (e.g.,
flux-dev, midjourney-v7-text-to-image)Text description of the desired image
Aspect ratio (e.g.,
1:1, 16:9, 9:16)Resolution option (e.g.,
1k, 2k, 4k) - varies by modelQuality setting (e.g.,
basic, high) - varies by modelRandom seed for reproducible results (-1 for random)
For Image-to-Image: URL of the reference image
For Image-to-Image: Transformation strength (0-1)
The generated image URL
Array of output URLs (first item is the primary result)
Generation status (
completed, succeeded, success)Example: Text-to-Image
Example: Image-to-Image
Video Generation
generateVideo()
Generate videos using Text-to-Video models.Video model ID (e.g.,
kling-v2.6-pro-t2v, runway-text-to-video)Text description of the desired video
Video aspect ratio (e.g.,
16:9, 9:16, 1:1)Video duration in seconds (varies by model)
Video resolution (e.g.,
480p, 720p, 1080p)Quality setting (e.g.,
basic, high, medium)Video generation uses longer polling: 120 attempts at 2-second intervals (~4 minutes)
Image-to-Image Generation
generateI2I()
Generate images using dedicated Image-to-Image models with custom image field handling.I2I model ID from the catalog
URL of the uploaded reference image
Array of image URLs for models that support multiple images
Optional text prompt (depends on model support)
Output aspect ratio
Output resolution
Output quality
The
imageField property from the model definition determines which payload key receives the image URL(s).
Common fields: image_url, images_list, model_image_urlMulti-Image Support
Image-to-Video Generation
generateI2V()
Generate videos from a starting frame image.I2V model ID from the catalog
URL of the starting frame image
Optional motion description
Video aspect ratio
Video duration in seconds
Video resolution
Video quality
File Upload
uploadFile()
Upload an image file to Muapi’s hosting service.The image file to upload (browser File object)
The hosted URL of the uploaded file
Upload Workflow
- User selects file from input
- Upload file to get hosted URL
- Use URL in generation requests
The upload endpoint returns JSON with a
url, file_url, or data.url field containing the hosted URL.Error Handling
Common Errors
Request Headers
All requests include:Utility Methods
getDimensionsFromAR()
Convert aspect ratio string to width/height dimensions (1024px base).[width, height] in pixels1:1→[1024, 1024]16:9→[1280, 720]9:16→[720, 1280]4:3→[1152, 864]3:2→[1216, 832]21:9→[1536, 640]
