# 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](https://mynth.io/docs/guides/generate-images.md) and
[generate video](https://mynth.io/docs/guides/generate-video.md) are the field references.

## Images

```ts
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`:

| Member                               | Holds                                                                   |
| ------------------------------------ | ----------------------------------------------------------------------- |
| `id`, `status`                       | The task id and status                                                  |
| `result`                             | The raw result: `model`, `images`, `magic_prompt`                       |
| `urls`                               | `url` 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                         |
| `data`                               | The whole task record                                                   |

> **Warning**
>
> `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](https://mynth.io/docs/sdks/typescript/errors.md).

Pass a model id on every call. Omitting `model` makes the API use `auto`,
which is experimental. See [the model field](https://mynth.io/docs/models.md#auto-is-experimental).
The SDK has no `image.estimate()`. Price an image body with
`POST /image/generate/estimate`. See [estimate cost](https://mynth.io/docs/guides/estimate-cost.md).

## 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.

```ts
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](https://mynth.io/docs/guides/image-to-image.md) covers input roles.

## Video

```ts
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()`.

```ts
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](https://mynth.io/docs/models/video.md) 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.

```ts
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" } });
```

| Method               | Result 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](https://mynth.io/docs/guides/rate-images.md), [alt text](https://mynth.io/docs/guides/alt-text.md),
[review](https://mynth.io/docs/guides/review-images.md), and
[remove background](https://mynth.io/docs/guides/remove-background.md).

## Next steps

- [Tasks and polling](https://mynth.io/docs/sdks/typescript/tasks.md): `generateAsync()`, the browser token, and poll intervals.
- [Errors](https://mynth.io/docs/sdks/typescript/errors.md): a rejected request and a failed task.
- [Tasks](https://mynth.io/docs/concepts/tasks.md#file-lifetimes): how long the files last.
