Generating media

Generate images and video with the TypeScript client, run the image tools, upload local files, and read the result classes.

image.generate() and video.generate() send one request body, wait for the task, and return the finished result. The body is the REST body, with the same fields. Generate images and generate video are the field references.

Images#

import Mynth from "@mynthio/sdk";

const mynth = new Mynth();

const task = await mynth.image.generate({
  model: "black-forest-labs/flux.2-pro",
  prompt: "A lighthouse at dusk, film grain",
  size: "landscape",
  count: 2,
  metadata: { orderId: "ord_42" },
});

for (const image of task.getImages()) {
  console.log(image.url ?? image.mynth_url, image.size);
}

The value is an ImageGenerationResult:

MemberHolds
id, statusThe task id and status
resultThe raw result: model, images, magic_prompt
urlsurl of each successful image, skipping any whose url is null
getImages()Successful images: id, url, mynth_url, size, format, rating
getImages({ includeFailed: true })Every entry, including { status: "failed", error }
getMetadata()The metadata you sent, typed from the request
dataThe whole task record

urls and getImages() skip failed images, and urls also skips images whose destination upload failed. A task can complete with a short list. Compare getImages().length with the count you sent, and pass { includeFailed: true } to see why.

A task that settles as failed is not returned: generate() throws TaskAsyncTaskFailedError. See errors.

Pass a model id on every call. Omitting model makes the API use auto, which is experimental. See the model field. The SDK has no image.estimate(). Price an image body with POST /image/generate/estimate. See estimate cost.

Local files#

In inputs, a string is a URL, and a File or Blob is uploaded first. All files of one call go up in a single upload, which accepts at most 10 files.

const task = await mynth.image.generate({
  model: "black-forest-labs/flux.2-pro",
  prompt: "The same lighthouse, at noon",
  inputs: [file],
});

image.upload(file) or image.upload([a, b]) runs that upload on its own and returns { urls }. Uploaded URLs are served for 1 day. Image to image covers input roles.

Video#

const task = await mynth.video.generate({
  model: "bytedance/seedance-2.0-mini",
  prompt: "A lighthouse beam sweeping over waves",
  duration: 5,
  resolution: "720p",
});

console.log(task.urls[0]);

The result is a VideoGenerationResult with urls, getVideos(), and getMetadata(). There is no .videos property. The raw array is result.videos, and getVideos() skips failures the same way getImages() does. generate() waits up to an hour.

video.estimate() prices the same body without creating a task. A File in inputs is uploaded first, as on generate().

const quote = await mynth.video.estimate({
  model: "bytedance/seedance-2.0-mini",
  prompt: "A lighthouse beam sweeping over waves",
  duration: 5,
});

console.log(quote.estimatedCost, quote.estimateKind); // "0.405", "exact"

AVAILABLE_VIDEO_MODELS lists each video model's resolutions, durations, and frame inputs. Video models has the same table.

Rating, alt text, review, background removal#

These take no model, and each takes a url or a local file. Each method waits, and each has an Async twin.

const rating = await mynth.image.rate({ url });
const alt = await mynth.image.alt({ url });
const review = await mynth.image.review({ url, effort: "low" });
const cutout = await mynth.image.removeBackground({ file, output: { format: "png" } });
MethodResult members
rate()taskId, cost, level
alt()taskId, cost, alt
review()taskId, cost, score, summary, findings, strengths
removeBackground()taskId, cost, image, metadata

The task id is taskId on these, not id. removeBackground() also takes destination, webhook, and metadata. The guides cover each tool: rate, alt text, review, and remove background.

Next steps#

  • Tasks and polling: generateAsync(), the browser token, and poll intervals.
  • Errors: a rejected request and a failed task.
  • Tasks: how long the files last.