# Generating media

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

`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.images) {
  console.log(image.url ?? image.mynthUrl, image.width, image.height);
}
```

The value is an `ImageGenerationResult`:

| Member                 | Holds                                                                                                                        |
| ---------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `taskId`, `cost`       | The task ID and final USD charge                                                                                             |
| `model`, `magicPrompt` | The resolved model and rewritten prompt                                                                                      |
| `urls`                 | `url` of each successful image, skipping any whose `url` is `null`                                                           |
| `images`               | Successful images with `id`, `url`, `mynthUrl`, `width`, `height`, `size`, `format`, `mimeType`, `destination`, and `rating` |
| `failures`             | Each failed image's `{ code, message? }`                                                                                     |
| `metadata`             | The metadata you sent, typed from the request                                                                                |
| `raw`                  | The API's request and result before mapping                                                                                  |

> **Warning**
>
> `urls` and `images` skip failed images, and `urls` also skips images whose destination upload
> failed. A task can complete with a short list. Compare `images.length` with the `count` you sent,
> and read `failures` 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,
  size: "9:16_720p",
});

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

The result is a plain `VideoGenerationResult` with `taskId`, `cost`, `model`,
`videos`, `urls`, `failures`, `metadata`, and `raw`. `videos` holds successful
outputs, and `failures` lists the errors for failed outputs. Each video uses
`mynthUrl` for Mynth's copy. `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.estimated_cost, quote.estimate_kind); // "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, upscaling

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" } });
const upscaled = await mynth.image.upscale({ file, size: "2x", effort: "low" });
```

| Method               | Result members                                                |
| -------------------- | ------------------------------------------------------------- |
| `rate()`             | `taskId`, `cost`, `level`                                     |
| `alt()`              | `taskId`, `cost`, `alt`                                       |
| `review()`           | `taskId`, `cost`, `score`, `summary`, `findings`, `strengths` |
| `removeBackground()` | `taskId`, `cost`, `image`, `metadata`                         |
| `upscale()`          | `taskId`, `cost`, `image`, `metadata`                         |

Every finished result uses `taskId` and includes `raw`. `removeBackground()` and
`upscale()` return the same image fields as generation and also take
`destination`, `webhook`, and `metadata`. `upscale()`
requires `size` and `effort`. 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),
[remove background](https://mynth.io/docs/guides/remove-background.md), and
[upscale](https://mynth.io/docs/guides/upscale-images.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.
