Getting started

If you have not yet set up Trigger.dev in your project, go to the quick start guide.

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:

npm install @trigger.dev/replicate@latest

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.

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.

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
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 NameDescription
collections.getGets a collection.
collections.listReturns the first page of all collections. Use with pagination helper.

Deployments

Function NameDescription
deployments.predictions.createCreates a new prediction with a deployment.
deployments.predictions.createAndAwaitCreates and waits for a new prediction with a deployment.

Models

Function NameDescription
models.getGets a model.
models.versionsGets a model version.
models.versionsGets all model versions.

Predictions

Function NameDescription
predictions.cancelCancels a prediction.
predictions.createCreates a prediction.
predictions.createAndAwaitCreates and waits for a prediction.
predictions.getGets a prediction.
predictions.listReturns the first page of all predictions. Use with pagination helper.

Trainings

Function NameDescription
trainings.cancelCancels a training.
trainings.createCreates a training.
trainings.createAndAwaitCreates and waits for a training.
trainings.getGets a training.
trainings.listReturns the first page of all trainings. Use with pagination helper.

Misc

Function NameDescription
getAllPagination helper that returns an array of results.
paginatePagination helper that returns an async generator.
requestSends authenticated requests to the Replicate API.
runCreates and waits for a prediction.

Example jobs

Code examples

Check out pre-built jobs using Replicate in our API section.