> ## Documentation Index
> Fetch the complete documentation index at: https://trigger-v3-trigger-api-redesign.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Replicate

> Run machine learning tasks easily at scale

<Snippet file="integration-getting-started.mdx" />

## Installation

To get started with the Replicate integration on Trigger.dev, you need to install the `@trigger.dev/replicate` package.
You can do this using npm, pnpm, or yarn:

<CodeGroup>
  ```bash npm
  npm install @trigger.dev/replicate@latest
  ```

  ```bash pnpm
  pnpm add @trigger.dev/replicate@latest
  ```

  ```bash yarn
  yarn add @trigger.dev/replicate@latest
  ```
</CodeGroup>

## Authentication

To use the Replicate API with Trigger.dev, you have to provide an API Key.

### API Key

You can create an API Key in your [Account Settings](https://replicate.com/account/api-tokens).

```ts
import { Replicate } from "@trigger.dev/replicate";

//this will use the passed in API key (defined in your environment variables)
const replicate = new Replicate({
  id: "replicate",
  apiKey: process.env["REPLICATE_API_KEY"],
});
```

## Usage

Include the Replicate integration in your Trigger.dev job.

```ts
client.defineJob({
  id: "replicate-cinematic-prompt",
  name: "Replicate - Cinematic Prompt",
  version: "0.1.0",
  integrations: { replicate },
  trigger: eventTrigger({
    name: "replicate.cinematic",
    schema: z.object({
      prompt: z.string().default("rick astley riding a harley through post-apocalyptic miami"),
      version: z
        .string()
        .default("af1a68a271597604546c09c64aabcd7782c114a63539a4a8d14d1eeda5630c33"),
    }),
  }),
  run: async (payload, io, ctx) => {
    //wait for prediction completion (uses remote callbacks internally)
    const prediction = await io.replicate.predictions.createAndAwait("await-prediction", {
      version: payload.version,
      input: {
        prompt: `${payload.prompt}, cinematic, 70mm, anamorphic, bokeh`,
        width: 1280,
        height: 720,
      },
    });
    return prediction.output;
  },
});
```

### Pagination

You can paginate responses:

* Using the `getAll` helper
* Using the `paginate` helper

```ts
client.defineJob({
  id: "replicate-pagination",
  name: "Replicate Pagination",
  version: "0.1.0",
  integrations: {
    replicate,
  },
  trigger: eventTrigger({
    name: "replicate.paginate",
  }),
  run: async (payload, io, ctx) => {
    // getAll - returns an array of all results (uses paginate internally)
    const all = await io.replicate.getAll(io.replicate.predictions.list, "get-all");

    // paginate - returns an async generator, useful to process one page at a time
    for await (const predictions of io.replicate.paginate(
      io.replicate.predictions.list,
      "paginate-all"
    )) {
      await io.logger.info("stats", {
        total: predictions.length,
        versions: predictions.map((p) => p.version),
      });
    }

    return { count: all.length };
  },
});
```

## Tasks

### Collections

| Function Name      | Description                                                            |
| ------------------ | ---------------------------------------------------------------------- |
| `collections.get`  | Gets a collection.                                                     |
| `collections.list` | Returns the first page of all collections. Use with pagination helper. |

### Deployments

| Function Name                            | Description                                               |
| ---------------------------------------- | --------------------------------------------------------- |
| `deployments.predictions.create`         | Creates a new prediction with a deployment.               |
| `deployments.predictions.createAndAwait` | Creates and waits for a new prediction with a deployment. |

### Models

| Function Name     | Description              |
| ----------------- | ------------------------ |
| `models.get`      | Gets a model.            |
| `models.versions` | Gets a model version.    |
| `models.versions` | Gets all model versions. |

### Predictions

| Function Name                | Description                                                            |
| ---------------------------- | ---------------------------------------------------------------------- |
| `predictions.cancel`         | Cancels a prediction.                                                  |
| `predictions.create`         | Creates a prediction.                                                  |
| `predictions.createAndAwait` | Creates and waits for a prediction.                                    |
| `predictions.get`            | Gets a prediction.                                                     |
| `predictions.list`           | Returns the first page of all predictions. Use with pagination helper. |

### Trainings

| Function Name              | Description                                                          |
| -------------------------- | -------------------------------------------------------------------- |
| `trainings.cancel`         | Cancels a training.                                                  |
| `trainings.create`         | Creates a training.                                                  |
| `trainings.createAndAwait` | Creates and waits for a training.                                    |
| `trainings.get`            | Gets a training.                                                     |
| `trainings.list`           | Returns the first page of all trainings. Use with pagination helper. |

### Misc

| Function Name | Description                                         |
| ------------- | --------------------------------------------------- |
| `getAll`      | Pagination helper that returns an array of results. |
| `paginate`    | Pagination helper that returns an async generator.  |
| `request`     | Sends authenticated requests to the Replicate API.  |
| `run`         | Creates and waits for a prediction.                 |

## Example jobs

<Card title="Code examples" icon="code" href="https://trigger.dev/apis/replicate">
  Check out pre-built jobs using Replicate in our API section.
</Card>
