From cc95ee9c01d08795bfd401655e52159e0f3745f8 Mon Sep 17 00:00:00 2001 From: dylanwongtencent Date: Mon, 13 Apr 2026 19:01:39 -0700 Subject: [PATCH 1/3] Update README.md --- README.md | 957 ++++++++++++------------------------------------------ 1 file changed, 207 insertions(+), 750 deletions(-) diff --git a/README.md b/README.md index cf12865c..d10ed1c6 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,6 @@ # Replicate Node.js client -A Node.js client for [Replicate](https://replicate.com). -It lets you run models from your Node.js code, -and everything else you can do with -[Replicate's HTTP API](https://replicate.com/docs/reference/http). +A Node.js client for [Replicate](https://replicate.com). It lets you run models from your Node.js code, and everything else you can do with [Replicate's HTTP API](https://replicate.com/docs/reference/http). > [!IMPORTANT] > This library can't interact with Replicate's API directly from a browser. @@ -16,10 +13,7 @@ and everything else you can do with - [Bun](https://bun.sh) >= 1.0 - [Deno](https://deno.com) >= 1.28 -You can also use this client library on most serverless platforms, including -[Cloudflare Workers](https://developers.cloudflare.com/workers/), -[Vercel functions](https://vercel.com/docs/functions), and -[AWS Lambda](https://aws.amazon.com/lambda/). +You can also use this client library on most serverless platforms, including [Cloudflare Workers](https://developers.cloudflare.com/workers/), [Vercel functions](https://vercel.com/docs/functions), and [AWS Lambda](https://aws.amazon.com/lambda/). ## Installation @@ -46,7 +40,9 @@ Instantiate the client: ```js const replicate = new Replicate({ // get your token from https://replicate.com/account/api-tokens - auth: "my api token", // defaults to process.env.REPLICATE_API_TOKEN + auth: "my api token", + + // defaults to process.env.REPLICATE_API_TOKEN }); ``` @@ -58,19 +54,21 @@ const input = { prompt: "a 19th century portrait of a raccoon gentleman wearing a suit", }; const [output] = await replicate.run(model, { input }); + // FileOutput('https://replicate.delivery/pbxt/GtQb3Sgve42ZZyVnt8xjquFk9EX5LP0fF68NTIWlgBMUpguQA/out-0.png') +console.log(output.url()); +// 'https://replicate.delivery/pbxt/GtQb3Sgve42ZZyVnt8xjquFk9EX5LP0fF68NTIWlgBMUpguQA/out-0.png' -console.log(output.url()); // 'https://replicate.delivery/pbxt/GtQb3Sgve42ZZyVnt8xjquFk9EX5LP0fF68NTIWlgBMUpguQA/out-0.png' -console.log(output.blob()); // Blob +console.log(output.blob()); +// Blob ``` > [!NOTE] -> A model that outputs file data returns a `FileOutput` object by default. This is an implementation -> of `ReadableStream` that returns the file contents. It has a `.blob()` method for accessing a -> `Blob` representation and a `.url()` method that will return the underlying data-source. -> -> We recommend accessing file data directly either as readable stream or via `.blob()` as the -> `.url()` property may not always return a HTTP URL in future. +> A model that outputs file data returns a `FileOutput` object by default. + +This is an implementation of `ReadableStream` that returns the file contents. It has a `.blob()` method for accessing a `Blob` representation and a `.url()` method that will return the underlying data-source. + +We recommend accessing file data directly either as readable stream or via `.blob()` as the `.url()` property may not always return a HTTP URL in future. You can also run a model in the background: @@ -97,13 +95,10 @@ console.log(prediction.output); // ['https://replicate.delivery/pbxt/RoaxeXqhL0xaYyLm6w3bpGwF5RaNBjADukfFnMbhOyeoWBdhA/out-0.png'] ``` -To run a model that takes a file input you can pass either -a URL to a publicly accessible file on the Internet -or a handle to a file on your local device. +To run a model that takes a file input you can pass either a URL to a publicly accessible file on the Internet or a handle to a file on your local device. ```js const fs = require("node:fs/promises"); - // Or when using ESM. // import fs from "node:fs/promises"; @@ -112,6 +107,7 @@ const input = { image: await fs.readFile("path/to/image.png"), }; const [output] = await replicate.run(model, { input }); + // FileOutput('https://replicate.delivery/mgxm/e7b0e122-9daa-410e-8cde-006c7308ff4d/output.png') ``` @@ -123,49 +119,109 @@ const [output] = await replicate.run(model, { input }); > upload the file to your own storage provider > and pass a publicly accessible URL. +### More examples + +#### Tencent Hunyuan Image 3 + +```js +import { writeFile } from "node:fs/promises"; +import Replicate from "replicate"; + +const replicate = new Replicate(); +const input = { + prompt: + "An illustration of a pink and brown butterfly on a pink background, with the black text Hunyuan Image 3 on Replicate below.", +}; + +const output = await replicate.run("tencent/hunyuan-image-3", { input }); +await writeFile("hunyuan-image-3.png", output[0]); +``` + +#### Tencent Hunyuan Video + +```js +import { writeFile } from "node:fs/promises"; +import Replicate from "replicate"; + +const replicate = new Replicate(); +const input = { + prompt: "A cat walks on the grass, realistic style.", + width: 854, + height: 480, + video_length: 129, + infer_steps: 50, + seed: 42, +}; + +const output = await replicate.run("tencent/hunyuan-video", { input }); +await writeFile("hunyuan-video.mp4", output); +``` + +#### Tencent Hunyuan3D-2mv + +```js +import { readFile, writeFile } from "node:fs/promises"; +import Replicate from "replicate"; + +const replicate = new Replicate(); +const input = { + front_image: await readFile("./front.png"), + // Optional additional views for better reconstruction: + // back_image: await readFile("./back.png"), + // left_image: await readFile("./left.png"), + // right_image: await readFile("./right.png"), + file_type: "glb", + steps: 30, + guidance_scale: 5, + target_face_num: 10000, +}; + +const output = await replicate.run("tencent/hunyuan3d-2mv", { input }); +await writeFile("hunyuan3d-2mv.glb", output); +``` + ## TypeScript usage -This library exports TypeScript definitions. You can import them like this: +This library exports TypeScript definitions. + +You can import them like this: ```ts -import Replicate, { type Prediction } from 'replicate'; +import Replicate, { type Prediction } from "replicate"; ``` Here's an example that uses the `Prediction` type with a custom `onProgress` callback: ```ts -import Replicate, { type Prediction } from 'replicate'; +import Replicate, { type Prediction } from "replicate"; const replicate = new Replicate(); const model = "black-forest-labs/flux-schnell"; const prompt = "a 19th century portrait of a raccoon gentleman wearing a suit"; + function onProgress(prediction: Prediction) { console.log({ prediction }); } -const output = await replicate.run(model, { input: { prompt } }, onProgress) -console.log({ output }) +const output = await replicate.run(model, { input: { prompt } }, onProgress); +console.log({ output }); ``` See the full list of exported types in [index.d.ts](./index.d.ts). ### Webhooks -Webhooks provide real-time updates about your prediction. Specify an endpoint when you create a prediction, and Replicate will send HTTP POST requests to that URL when the prediction is created, updated, and finished. +Webhooks provide real-time updates about your prediction. Specify an endpoint when you create a prediction, and Replicate will send HTTP POST requests to that URL when the prediction is created, updated, and finished. It is possible to provide a URL to the `predictions.create()` function that will be requested by Replicate when the prediction status changes. This is an alternative to polling. To receive webhooks you’ll need a web server. -It is possible to provide a URL to the predictions.create() function that will be requested by Replicate when the prediction status changes. This is an alternative to polling. - -To receive webhooks you’ll need a web server. The following example uses Hono, a web standards based server, but this pattern applies to most frameworks. - -
- See example +The following example uses Hono, a web standards based server, but this pattern applies to most frameworks. ```js -import { serve } from '@hono/node-server'; -import { Hono } from 'hono'; +import { serve } from "@hono/node-server"; +import { Hono } from "hono"; const app = new Hono(); -app.get('/webhooks/replicate', async (c) => { + +app.get("/webhooks/replicate", async (c) => { // Get the prediction from the request. const prediction = await c.req.json(); console.log(prediction); @@ -173,80 +229,74 @@ app.get('/webhooks/replicate', async (c) => { // Acknowledge the webhook. c.status(200); - c.json({ok: true}); -})); + c.json({ ok: true }); +}); serve(app, (info) => { - console.log(`Listening on http://localhost:${info.port}`) + console.log(`Listening on http://localhost:${info.port}`); //=> Listening on http://localhost:3000 }); ``` -
- -Create the prediction passing in the webhook URL to `webhook` and specify which events you want to receive in `webhook_events_filter` out of "start", "output", ”logs” and "completed": +Create the prediction passing in the webhook URL to `webhook` and specify which events you want to receive in `webhook_events_filter` out of `start`, `output`, `logs`, and `completed`: ```js const Replicate = require("replicate"); const replicate = new Replicate(); const input = { - image: "https://replicate.delivery/pbxt/KWDkejqLfER3jrroDTUsSvBWFaHtapPxfg4xxZIqYmfh3zXm/Screenshot%202024-02-28%20at%2022.14.00.png", - denoising_strength: 0.5, - instant_id_strength: 0.8 + image: "https://replicate.delivery/pbxt/KWDkejqLfER3jrroDTUsSvBWFaHtapPxfg4xxZIqYmfh3zXm/Screenshot%202024-02-28%20at%2022.14.00.png", + denoising_strength: 0.5, + instant_id_strength: 0.8, }; const callbackURL = `https://my.app/webhooks/replicate`; + await replicate.predictions.create({ version: "19deaef633fd44776c82edf39fd60e95a7250b8ececf11a725229dc75a81f9ca", input: input, webhook: callbackURL, webhook_events_filter: ["completed"], }); - -// The server will now handle the event and log: -// => {"id": "xyz", "status": "successful", ... } ``` ## Verifying webhooks To prevent unauthorized requests, Replicate signs every webhook and its metadata with a unique key for each user or organization. You can use this signature to verify the webhook indeed comes from Replicate before you process it. -This client includes a `validateWebhook` convenience function that you can use to validate webhooks. - -To validate webhooks: +This client includes a `validateWebhook` convenience function that you can use to validate webhooks. To validate webhooks: 1. Check out the [webhooks guide](https://replicate.com/docs/webhooks) to get started. -1. [Retrieve your webhook signing secret](https://replicate.com/docs/webhooks#retrieving-the-webhook-signing-key) and store it in your enviroment. -1. Update your webhook handler to call `validateWebhook(request, secret)`, where `request` is an instance of a [web-standard `Request` object](https://developer.mozilla.org/en-US/docs/Web/API/object), and `secret` is the signing secret for your environment. +2. [Retrieve your webhook signing secret](https://replicate.com/docs/webhooks#retrieving-the-webhook-signing-key) and store it in your environment. +3. Update your webhook handler to call `validateWebhook(request, secret)`, where `request` is an instance of a [web-standard `Request` object](https://developer.mozilla.org/en-US/docs/Web/API/Request), and `secret` is the signing secret for your environment. Here's an example of how to validate webhooks using Next.js: ```js -import { NextResponse } from 'next/server'; -import { validateWebhook } from 'replicate'; +import { NextResponse } from "next/server"; +import { validateWebhook } from "replicate"; export async function POST(request) { const secret = process.env.REPLICATE_WEBHOOK_SIGNING_SECRET; if (!secret) { - console.log("Skipping webhook validation. To validate webhooks, set REPLICATE_WEBHOOK_SIGNING_SECRET") + console.log("Skipping webhook validation. To validate webhooks, set REPLICATE_WEBHOOK_SIGNING_SECRET"); const body = await request.json(); console.log(body); - return NextResponse.json({ detail: "Webhook received (but not validated)" }, { status: 200 }); + return NextResponse.json( + { detail: "Webhook received (but not validated)" }, + { status: 200 } + ); } const webhookIsValid = await validateWebhook(request.clone(), secret); - if (!webhookIsValid) { return NextResponse.json({ detail: "Webhook is invalid" }, { status: 401 }); } - // process validated webhook here... console.log("Webhook is valid!"); const body = await request.json(); console.log(body); - return NextResponse.json({ detail: "Webhook is valid" }, { status: 200 }); } ``` @@ -255,27 +305,31 @@ If your environment doesn't support `Request` objects, you can pass the required ```js const requestData = { - id: "123", // the `Webhook-Id` header + id: "123", // the `Webhook-Id` header timestamp: "0123456", // the `Webhook-Timestamp` header - signature: "xyz", // the `Webhook-Signature` header - body: "{...}", // the request body as a string, ArrayBuffer or ReadableStream - secret: "shhh", // the webhook secret, obtained from the `replicate.webhooks.defaul.secret` endpoint + signature: "xyz", // the `Webhook-Signature` header + body: "{...}", // the request body as a string, ArrayBuffer or ReadableStream + secret: "shhh", // the webhook secret, obtained from the `replicate.webhooks.default.secret` endpoint }; + const webhookIsValid = await validateWebhook(requestData); ``` > [!NOTE] -> The `validateWebhook` function uses the global `crypto` API available in most JavaScript runtimes. Node <= 18 does not provide this global so in this case you need to either call node with the `--no-experimental-global-webcrypto` or provide the `webcrypto` module manually. -> ```js -> const crypto = require("node:crypto").webcrypto; -> const webhookIsValid = await valdiateWebhook(requestData, crypto); -> ``` +> The `validateWebhook` function uses the global `crypto` API available in most JavaScript runtimes. + +Node <= 18 does not provide this global so in this case you need to either call node with the `--no-experimental-global-webcrypto` or provide the `webcrypto` module manually. + +```js +const crypto = require("node:crypto").webcrypto; +const webhookIsValid = await validateWebhook(requestData, crypto); +``` ## TypeScript The `Replicate` constructor and all `replicate.*` methods are fully typed. -Currently in order to support the module format used by `replicate` you'll need to set `esModuleInterop` to `true` in your tsconfig.json. +Currently in order to support the module format used by `replicate` you'll need to set `esModuleInterop` to `true` in your `tsconfig.json`. ## API @@ -285,30 +339,18 @@ Currently in order to support the module format used by `replicate` you'll need const replicate = new Replicate(options); ``` -| name | type | description | -| ------------------------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------- | -| `options.auth` | string | **Required**. API access token | -| `options.userAgent` | string | Identifier of your app. Defaults to `replicate-javascript/${packageJSON.version}` | -| `options.baseUrl` | string | Defaults to https://api.replicate.com/v1 | -| `options.fetch` | function | Fetch function to use. Defaults to `globalThis.fetch` | -| `options.fileEncodingStrategy` | string | Determines the file encoding strategy to use. Possible values: `"default"`, `"upload"`, or `"data-uri"`. Defaults to `"default"` | -| `options.useFileOutput` | boolean | Determines if the `replicate.run()` method should convert file output into `FileOutput` objects | - - -The client makes requests to Replicate's API using -[fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch). -By default, the `globalThis.fetch` function is used, -which is available on [Node.js 18](https://nodejs.org/en/blog/announcements/v18-release-announce#fetch-experimental) and later, -as well as -[Cloudflare Workers](https://developers.cloudflare.com/workers/runtime-apis/fetch/), -[Vercel Functions](https://vercel.com/docs/functions), -and other environments. - -On earlier versions of Node.js -and other environments where global fetch isn't available, -you can install a fetch function from an external package like -[cross-fetch](https://www.npmjs.com/package/cross-fetch) -and pass it to the `fetch` option in the constructor. +| name | type | description | +| --- | --- | --- | +| `options.auth` | string | **Required**. API access token | +| `options.userAgent` | string | Identifier of your app. Defaults to `replicate-javascript/${packageJSON.version}` | +| `options.baseUrl` | string | Defaults to `https://api.replicate.com/v1` | +| `options.fetch` | function | Fetch function to use. Defaults to `globalThis.fetch` | +| `options.fileEncodingStrategy` | string | Determines the file encoding strategy to use. Possible values: `"default"`, `"upload"`, or `"data-uri"`. Defaults to `"default"` | +| `options.useFileOutput` | boolean | Determines if the `replicate.run()` method should convert file output into `FileOutput` objects | + +The client makes requests to Replicate's API using [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch). By default, the `globalThis.fetch` function is used, which is available on [Node.js 18](https://nodejs.org/en/blog/announcements/v18-release-announce#fetch-experimental) and later, as well as [Cloudflare Workers](https://developers.cloudflare.com/workers/runtime-apis/fetch/), [Vercel Functions](https://vercel.com/docs/functions), and other environments. + +On earlier versions of Node.js and other environments where global fetch isn't available, you can install a fetch function from an external package like [cross-fetch](https://www.npmjs.com/package/cross-fetch) and pass it to the `fetch` option in the constructor. ```js const Replicate = require("replicate"); @@ -321,16 +363,13 @@ const fetch = require("fetch"); const replicate = new Replicate({ fetch }); ``` -You can also use the `fetch` option to add custom behavior to client requests, -such as injecting headers or adding log statements. +You can also use the `fetch` option to add custom behavior to client requests, such as injecting headers or adding log statements. ```js const customFetch = (url, options) => { const headers = options && options.headers ? { ...options.headers } : {}; headers["X-Custom-Header"] = "some value"; - console.log("fetch", { url, ...options, headers }); - return fetch(url, { ...options, headers }); }; @@ -339,33 +378,33 @@ const replicate = new Replicate({ fetch: customFetch }); ### `replicate.run` -Run a model and await the result. Unlike [`replicate.prediction.create`](#replicatepredictionscreate), this method returns only the prediction output rather than the entire prediction object. +Run a model and await the result. + +Unlike [`replicate.predictions.create`](#replicatepredictionscreate), this method returns only the prediction output rather than the entire prediction object. ```js const output = await replicate.run(identifier, options, progress); ``` -| name | type | description | -| ------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `identifier` | string | **Required**. The model version identifier in the format `{owner}/{name}:{version}`, for example `stability-ai/sdxl:8beff3369e81422112d93b89ca01426147de542cd4684c244b673b105188fe5f` | -| `options.input` | object | **Required**. An object with the model inputs. | -| `options.wait` | object | Options for waiting for the prediction to finish | -| `options.wait.mode` | `"poll" \| "block"` | `"block"` holds the request open, `"poll"` makes repeated requests to fetch the prediction. Defaults to `"block"` | -| `options.wait.interval` | number | Polling interval in milliseconds. Defaults to 500 | -| `options.wait.timeout` | number | In `"block"` mode determines how long the request will be held open until falling back to polling. Defaults to 60 | -| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output | -| `options.webhook_events_filter` | string[] | An array of events which should trigger [webhooks](https://replicate.com/docs/webhooks). Allowable values are `start`, `output`, `logs`, and `completed` | -| `options.signal` | object | An [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel the prediction | -| `progress` | function | Callback function that receives the prediction object as it's updated. The function is called when the prediction is created, each time it's updated while polling for completion, and when it's completed. | +| name | type | description | +| --- | --- | --- | +| `identifier` | string | **Required**. The model version identifier in the format `{owner}/{name}:{version}`, for example `stability-ai/sdxl:8beff3369e81422112d93b89ca01426147de542cd4684c244b673b105188fe5f` | +| `options.input` | object | **Required**. An object with the model inputs. | +| `options.wait` | object | Options for waiting for the prediction to finish | +| `options.wait.mode` | `"poll" \| "block"` | `"block"` holds the request open, `"poll"` makes repeated requests to fetch the prediction. Defaults to `"block"` | +| `options.wait.interval` | number | Polling interval in milliseconds. Defaults to 500 | +| `options.wait.timeout` | number | In `"block"` mode determines how long the request will be held open until falling back to polling. Defaults to 60 | +| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output | +| `options.webhook_events_filter` | string[] | An array of events which should trigger [webhooks](https://replicate.com/docs/webhooks). Allowable values are `start`, `output`, `logs`, and `completed` | +| `options.signal` | object | An [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel the prediction | +| `progress` | function | Callback function that receives the prediction object as it's updated. The function is called when the prediction is created, each time it's updated while polling for completion, and when it's completed. | Throws `Error` if the prediction failed. -Returns `Promise` which resolves with the output of running the model. +Returns `Promise` which resolves with the output of running the model. > [!NOTE] -> Currently the TypeScript return type of `replicate.run()` is `Promise` this is -> misleading as a model can return array types as well as primative types like strings, -> numbers and booleans. +> Currently the TypeScript return type of `replicate.run()` is `Promise`; this is misleading as a model can return array types as well as primitive types like strings, numbers and booleans. Example: @@ -380,39 +419,40 @@ Example that logs progress as the model is running: ```js const model = "stability-ai/sdxl:8beff3369e81422112d93b89ca01426147de542cd4684c244b673b105188fe5f"; const input = { prompt: "a 19th century portrait of a raccoon gentleman wearing a suit" }; + const onProgress = (prediction) => { - const last_log_line = prediction.logs.split("\n").pop() - console.log({id: prediction.id, log: last_log_line}) -} -const output = await replicate.run(model, { input }, onProgress) + const last_log_line = prediction.logs.split("\n").pop(); + console.log({ id: prediction.id, log: last_log_line }); +}; + +const output = await replicate.run(model, { input }, onProgress); ``` #### Sync vs. Async API (`"poll"` vs. `"block"`) -The `replicate.run()` API takes advantage of the [Replicate sync API](https://replicate.com/docs/topics/predictions/create-a-prediction) -which is optimized for low latency requests to file models like `black-forest-labs/flux-dev` and -`black-forest-labs/flux-schnell`. When creating a prediction this will hold a connection open to the -server and return a `FileObject` containing the generated file as quickly as possible. +The `replicate.run()` API takes advantage of the [Replicate sync API](https://replicate.com/docs/topics/predictions/create-a-prediction) which is optimized for low latency requests to file models like `black-forest-labs/flux-dev` and `black-forest-labs/flux-schnell`. When creating a prediction this will hold a connection open to the server and return a `FileObject` containing the generated file as quickly as possible. ### `replicate.stream` Run a model and stream its output. Unlike [`replicate.prediction.create`](#replicatepredictionscreate), this method returns only the prediction output rather than the entire prediction object. ```js -for await (const event of replicate.stream(identifier, options)) { /* ... */ } +for await (const event of replicate.stream(identifier, options)) { + /* ... */ +} ``` -| name | type | description | -| ------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `identifier` | string | **Required**. The model version identifier in the format `{owner}/{name}` or `{owner}/{name}:{version}`, for example `meta/llama-2-70b-chat` | -| `options.input` | object | **Required**. An object with the model inputs. | -| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output | +| name | type | description | +| --- | --- | --- | +| `identifier` | string | **Required**. The model version identifier in the format `{owner}/{name}` or `{owner}/{name}:{version}`, for example `meta/llama-2-70b-chat` | +| `options.input` | object | **Required**. An object with the model inputs. | +| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output | | `options.webhook_events_filter` | string[] | An array of events which should trigger [webhooks](https://replicate.com/docs/webhooks). Allowable values are `start`, `output`, `logs`, and `completed` | -| `options.signal` | object | An [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel the prediction | +| `options.signal` | object | An [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel the prediction | Throws `Error` if the prediction failed. -Returns `AsyncGenerator` which yields the events of running the model. +Returns `AsyncGenerator` which yields the events of running the model. Example: @@ -424,8 +464,8 @@ const options = { }, // webhook: "https://smee.io/dMUlmOMkzeyRGjW" // optional }; -const output = []; +const output = []; for await (const { event, data } of replicate.stream(model, options)) { if (event === "output") { output.push(data); @@ -439,256 +479,14 @@ console.log(output.join("").trim()); A stream generates server-sent events with the following properties: -| name | type | description | -| ------- | ------ | ---------------------------------------------------------------------------- | -| `event` | string | The type of event. Possible values are `output`, `logs`, `error`, and `done` | -| `data` | string | The event data | -| `id` | string | The event id | -| `retry` | number | The number of milliseconds to wait before reconnecting to the server | - -As the prediction runs, the generator yields `output` and `logs` events. If an error occurs, the generator yields an `error` event with a JSON object containing the error message set to the `data` property. When the prediction is done, the generator yields a `done` event with an empty JSON object set to the `data` property. - -Events with the `output` event type have their `toString()` method overridden to return the event data as a string. Other event types return an empty string. - -### `replicate.models.get` - -Get metadata for a public model or a private model that you own. - -```js -const response = await replicate.models.get(model_owner, model_name); -``` - -| name | type | description | -| ------------- | ------ | ----------------------------------------------------------------------- | -| `model_owner` | string | **Required**. The name of the user or organization that owns the model. | -| `model_name` | string | **Required**. The name of the model. | - -```jsonc -{ - "url": "https://replicate.com/replicate/hello-world", - "owner": "replicate", - "name": "hello-world", - "description": "A tiny model that says hello", - "visibility": "public", - "github_url": "https://github.com/replicate/cog-examples", - "paper_url": null, - "license_url": null, - "latest_version": { - /* ... */ - } -} -``` - -### `replicate.models.list` - -Get a paginated list of all public models. - -```js -const response = await replicate.models.list(); -``` - -```jsonc -{ - "next": null, - "previous": null, - "results": [ - { - "url": "https://replicate.com/replicate/hello-world", - "owner": "replicate", - "name": "hello-world", - "description": "A tiny model that says hello", - "visibility": "public", - "github_url": "https://github.com/replicate/cog-examples", - "paper_url": null, - "license_url": null, - "run_count": 5681081, - "cover_image_url": "...", - "default_example": { - /* ... */ - }, - "latest_version": { - /* ... */ - } - } - ] -} -``` - -### `replicate.models.search` - -Search for public models on Replicate. - -```js -const response = await replicate.models.search(query); -``` - -| name | type | description | -| ------- | ------ | -------------------------------------- | -| `query` | string | **Required**. The search query string. | - -### `replicate.models.create` - -Create a new public or private model. - -```js -const response = await replicate.models.create(model_owner, model_name, options); -``` - -| name | type | description | -| ------------------------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `model_owner` | string | **Required**. The name of the user or organization that will own the model. This must be the same as the user or organization that is making the API request. In other words, the API token used in the request must belong to this user or organization. | -| `model_name` | string | **Required**. The name of the model. This must be unique among all models owned by the user or organization. | -| `options.visibility` | string | **Required**. Whether the model should be public or private. A public model can be viewed and run by anyone, whereas a private model can be viewed and run only by the user or organization members that own the model. | -| `options.hardware` | string | **Required**. The SKU for the hardware used to run the model. Possible values can be found by calling [`replicate.hardware.list()`](#replicatehardwarelist). | -| `options.description` | string | A description of the model. | -| `options.github_url` | string | A URL for the model's source code on GitHub. | -| `options.paper_url` | string | A URL for the model's paper. | -| `options.license_url` | string | A URL for the model's license. | -| `options.cover_image_url` | string | A URL for the model's cover image. This should be an image file. | - -### `replicate.hardware.list` +| name | type | description | +| --- | --- | --- | +| `event` | string | The type of event. | +| `data` | string | The event data. | +| `id` | string | The event ID. | +| `retry` | number | The reconnection interval in milliseconds. | -List available hardware for running models on Replicate. - -```js -const response = await replicate.hardware.list() -``` - -```jsonc -[ - {"name": "CPU", "sku": "cpu" }, - {"name": "Nvidia T4 GPU", "sku": "gpu-t4" }, - {"name": "Nvidia A40 GPU", "sku": "gpu-a40-small" }, - {"name": "Nvidia A40 (Large) GPU", "sku": "gpu-a40-large" }, -] -``` - -### `replicate.models.versions.list` - -Get a list of all published versions of a model, including input and output schemas for each version. - -```js -const response = await replicate.models.versions.list(model_owner, model_name); -``` - -| name | type | description | -| ------------- | ------ | ----------------------------------------------------------------------- | -| `model_owner` | string | **Required**. The name of the user or organization that owns the model. | -| `model_name` | string | **Required**. The name of the model. | - -```jsonc -{ - "previous": null, - "next": null, - "results": [ - { - "id": "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", - "created_at": "2022-04-26T19:29:04.418669Z", - "cog_version": "0.3.0", - "openapi_schema": { - /* ... */ - } - }, - { - "id": "e2e8c39e0f77177381177ba8c4025421ec2d7e7d3c389a9b3d364f8de560024f", - "created_at": "2022-03-21T13:01:04.418669Z", - "cog_version": "0.3.0", - "openapi_schema": { - /* ... */ - } - } - ] -} -``` - -### `replicate.models.versions.get` - -Get metadata for a specific version of a model. - -```js -const response = await replicate.models.versions.get(model_owner, model_name, version_id); -``` - -| name | type | description | -| ------------- | ------ | ----------------------------------------------------------------------- | -| `model_owner` | string | **Required**. The name of the user or organization that owns the model. | -| `model_name` | string | **Required**. The name of the model. | -| `version_id` | string | **Required**. The model version | - -```jsonc -{ - "id": "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", - "created_at": "2022-04-26T19:29:04.418669Z", - "cog_version": "0.3.0", - "openapi_schema": { - /* ... */ - } -} -``` - -### `replicate.collections.get` - -Get a list of curated model collections. See [replicate.com/collections](https://replicate.com/collections). - -```js -const response = await replicate.collections.get(collection_slug); -``` - -| name | type | description | -| ----------------- | ------ | ------------------------------------------------------------------------------ | -| `collection_slug` | string | **Required**. The slug of the collection. See http://replicate.com/collections | - -### `replicate.predictions.create` - -Run a model with inputs you provide. - -```js -const response = await replicate.predictions.create(options); -``` - -| name | type | description | -| ------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------- | -| `options.input` | object | **Required**. An object with the model's inputs | -| `options.model` | string | The name of the model, e.g. `black-forest-labs/flux-schnell`. This is required if you're running an [official model](https://replicate.com/explore#official-models). | -| `options.version` | string | The 64-character [model version id](https://replicate.com/docs/topics/models/versions), e.g. `80537f9eead1a5bfa72d5ac6ea6414379be41d4d4f6679fd776e9535d1eb58bb`. This is required if you're not specifying a `model`. | -| `options.wait` | number | Wait up to 60s for the prediction to finish before returning. Disabled by default. See [Synchronous predictions](https://replicate.com/docs/topics/predictions/create-a-prediction#sync-mode) for more information. | -| `options.stream` | boolean | Requests a URL for streaming output output | -| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output | -| `options.webhook_events_filter` | string[] | You can change which events trigger webhook requests by specifying webhook events (`start` \| `output` \| `logs` \| `completed`) | - -```jsonc -{ - "id": "ufawqhfynnddngldkgtslldrkq", - "version": "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", - "status": "succeeded", - "input": { - "text": "Alice" - }, - "output": null, - "error": null, - "logs": null, - "metrics": {}, - "created_at": "2022-04-26T22:13:06.224088Z", - "started_at": null, - "completed_at": null, - "urls": { - "get": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq", - "cancel": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq/cancel", - "stream": "https://streaming.api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq" // Present only if `options.stream` is `true` - } -} -``` - -#### Streaming - -Specify the `stream` option when creating a prediction -to request a URL to receive streaming output using -[server-sent events (SSE)](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events). - -If the requested model version supports streaming, -then the returned prediction will have a `stream` entry in its `urls` property -with a URL that you can use to construct an -[`EventSource`](https://developer.mozilla.org/en-US/docs/Web/API/EventSource). +Predictions that are streamed receive a `urls.stream` field in the prediction object. This point to an SSE event stream that can be used to consume the model's output. ```js if (prediction && prediction.urls && prediction.urls.stream) { @@ -711,14 +509,13 @@ if (prediction && prediction.urls && prediction.urls.stream) { A prediction's event stream consists of the following event types: -| event | format | description | -| -------- | ---------- | ---------------------------------------------- | +| event | format | description | +| --- | --- | --- | | `output` | plain text | Emitted when the prediction returns new output | -| `error` | JSON | Emitted when the prediction returns an error | -| `done` | JSON | Emitted when the prediction finishes | +| `error` | JSON | Emitted when the prediction returns an error | +| `done` | JSON | Emitted when the prediction finishes | -A `done` event is emitted when a prediction finishes successfully, -is cancelled, or produces an error. +A `done` event is emitted when a prediction finishes successfully, is cancelled, or produces an error. ### `replicate.predictions.get` @@ -726,32 +523,6 @@ is cancelled, or produces an error. const response = await replicate.predictions.get(prediction_id); ``` -| name | type | description | -| --------------- | ------ | ------------------------------- | -| `prediction_id` | number | **Required**. The prediction id | - -```jsonc -{ - "id": "ufawqhfynnddngldkgtslldrkq", - "version": "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", - "urls": { - "get": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq", - "cancel": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq/cancel" - }, - "status": "starting", - "input": { - "text": "Alice" - }, - "output": null, - "error": null, - "logs": null, - "metrics": {}, - "created_at": "2022-04-26T22:13:06.224088Z", - "started_at": null, - "completed_at": null -} -``` - ### `replicate.predictions.cancel` Stop a running prediction before it finishes. @@ -760,32 +531,6 @@ Stop a running prediction before it finishes. const response = await replicate.predictions.cancel(prediction_id); ``` -| name | type | description | -| --------------- | ------ | ------------------------------- | -| `prediction_id` | number | **Required**. The prediction id | - -```jsonc -{ - "id": "ufawqhfynnddngldkgtslldrkq", - "version": "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", - "urls": { - "get": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq", - "cancel": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq/cancel" - }, - "status": "canceled", - "input": { - "text": "Alice" - }, - "output": null, - "error": null, - "logs": null, - "metrics": {}, - "created_at": "2022-04-26T22:13:06.224088Z", - "started_at": "2022-04-26T22:13:06.224088Z", - "completed_at": "2022-04-26T22:13:06.224088Z" -} -``` - ### `replicate.predictions.list` Get a paginated list of all the predictions you've created. @@ -796,74 +541,16 @@ const response = await replicate.predictions.list(); `replicate.predictions.list()` takes no arguments. -```jsonc -{ - "previous": null, - "next": "https://api.replicate.com/v1/predictions?cursor=cD0yMDIyLTAxLTIxKzIzJTNBMTglM0EyNC41MzAzNTclMkIwMCUzQTAw", - "results": [ - { - "id": "jpzd7hm5gfcapbfyt4mqytarku", - "version": "b21cbe271e65c1718f2999b038c18b45e21e4fba961181fbfae9342fc53b9e05", - "urls": { - "get": "https://api.replicate.com/v1/predictions/jpzd7hm5gfcapbfyt4mqytarku", - "cancel": "https://api.replicate.com/v1/predictions/jpzd7hm5gfcapbfyt4mqytarku/cancel" - }, - "source": "web", - "status": "succeeded", - "created_at": "2022-04-26T20:00:40.658234Z", - "started_at": "2022-04-26T20:00:84.583803Z", - "completed_at": "2022-04-26T20:02:27.648305Z" - } - /* ... */ - ] -} -``` - ### `replicate.trainings.create` -Use the [training API](https://replicate.com/docs/fine-tuning) to fine-tune language models -to make them better at a particular task. -To see what **language models** currently support fine-tuning, -check out Replicate's [collection of trainable language models](https://replicate.com/collections/trainable-language-models). +Use the [training API](https://replicate.com/docs/fine-tuning) to fine-tune language models to make them better at a particular task. To see what **language models** currently support fine-tuning, check out Replicate's [collection of trainable language models](https://replicate.com/collections/trainable-language-models). -If you're looking to fine-tune **image models**, -check out Replicate's [guide to fine-tuning image models](https://replicate.com/docs/guides/fine-tune-an-image-model). +If you're looking to fine-tune **image models**, check out Replicate's [guide to fine-tuning image models](https://replicate.com/docs/guides/fine-tune-an-image-model). ```js const response = await replicate.trainings.create(model_owner, model_name, version_id, options); ``` -| name | type | description | -| ------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------- | -| `model_owner` | string | **Required**. The name of the user or organization that owns the model. | -| `model_name` | string | **Required**. The name of the model. | -| `version` | string | **Required**. The model version | -| `options.destination` | string | **Required**. The destination for the trained version in the form `{username}/{model_name}` | -| `options.input` | object | **Required**. An object with the model's inputs | -| `options.webhook` | string | An HTTPS URL for receiving a webhook when the training has new output | -| `options.webhook_events_filter` | string[] | You can change which events trigger webhook requests by specifying webhook events (`start` \| `output` \| `logs` \| `completed`) | - -```jsonc -{ - "id": "zz4ibbonubfz7carwiefibzgga", - "version": "3ae0799123a1fe11f8c89fd99632f843fc5f7a761630160521c4253149754523", - "status": "starting", - "input": { - "text": "..." - }, - "output": null, - "error": null, - "logs": null, - "started_at": null, - "created_at": "2023-03-28T21:47:58.566434Z", - "completed_at": null -} -``` - -> **Warning** -> If you try to fine-tune a model that doesn't support training, -> you'll get a `400 Bad Request` response from the server. - ### `replicate.trainings.get` Get metadata and status of a training. @@ -872,31 +559,6 @@ Get metadata and status of a training. const response = await replicate.trainings.get(training_id); ``` -| name | type | description | -| ------------- | ------ | ----------------------------- | -| `training_id` | number | **Required**. The training id | - -```jsonc -{ - "id": "zz4ibbonubfz7carwiefibzgga", - "version": "3ae0799123a1fe11f8c89fd99632f843fc5f7a761630160521c4253149754523", - "status": "succeeded", - "input": { - "data": "..." - "param1": "..." - }, - "output": { - "version": "..." - }, - "error": null, - "logs": null, - "webhook_completed": null, - "started_at": "2023-03-28T21:48:02.402755Z", - "created_at": "2023-03-28T21:47:58.566434Z", - "completed_at": "2023-03-28T02:49:48.492023Z" -} -``` - ### `replicate.trainings.cancel` Stop a running training job before it finishes. @@ -905,31 +567,6 @@ Stop a running training job before it finishes. const response = await replicate.trainings.cancel(training_id); ``` -| name | type | description | -| ------------- | ------ | ----------------------------- | -| `training_id` | number | **Required**. The training id | - -```jsonc -{ - "id": "zz4ibbonubfz7carwiefibzgga", - "version": "3ae0799123a1fe11f8c89fd99632f843fc5f7a761630160521c4253149754523", - "status": "canceled", - "input": { - "data": "..." - "param1": "..." - }, - "output": { - "version": "..." - }, - "error": null, - "logs": null, - "webhook_completed": null, - "started_at": "2023-03-28T21:48:02.402755Z", - "created_at": "2023-03-28T21:47:58.566434Z", - "completed_at": "2023-03-28T02:49:48.492023Z" -} -``` - ### `replicate.trainings.list` Get a paginated list of all the trainings you've run. @@ -940,50 +577,18 @@ const response = await replicate.trainings.list(); `replicate.trainings.list()` takes no arguments. -```jsonc -{ - "previous": null, - "next": "https://api.replicate.com/v1/trainings?cursor=cD0yMDIyLTAxLTIxKzIzJTNBMTglM0EyNC41MzAzNTclMkIwMCUzQTAw", - "results": [ - { - "id": "jpzd7hm5gfcapbfyt4mqytarku", - "version": "b21cbe271e65c1718f2999b038c18b45e21e4fba961181fbfae9342fc53b9e05", - "urls": { - "get": "https://api.replicate.com/v1/trainings/jpzd7hm5gfcapbfyt4mqytarku", - "cancel": "https://api.replicate.com/v1/trainings/jpzd7hm5gfcapbfyt4mqytarku/cancel" - }, - "source": "web", - "status": "succeeded", - "created_at": "2022-04-26T20:00:40.658234Z", - "started_at": "2022-04-26T20:00:84.583803Z", - "completed_at": "2022-04-26T20:02:27.648305Z" - } - /* ... */ - ] -} -``` - ### `replicate.deployments.predictions.create` -Run a model using your own custom deployment. - -Deployments allow you to run a model with a private, fixed API endpoint. You can configure the version of the model, the hardware it runs on, and how it scales. See the [deployments guide](https://replicate.com/docs/deployments) to learn more and get started. +Run a model using your own custom deployment. Deployments allow you to run a model with a private, fixed API endpoint. You can configure the version of the model, the hardware it runs on, and how it scales. See the [deployments guide](https://replicate.com/docs/deployments) to learn more and get started. ```js -const response = await replicate.deployments.predictions.create(deployment_owner, deployment_name, options); +const response = await replicate.deployments.predictions.create( + deployment_owner, + deployment_name, + options +); ``` -| name | type | description | -| ------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------- | -| `deployment_owner` | string | **Required**. The name of the user or organization that owns the deployment | -| `deployment_name` | string | **Required**. The name of the deployment | -| `options.input` | object | **Required**. An object with the model's inputs | -| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output | -| `options.webhook_events_filter` | string[] | You can change which events trigger webhook requests by specifying webhook events (`start` \| `output` \| `logs` \| `completed`) | - -Use `replicate.wait` to wait for a prediction to finish, -or `replicate.predictions.cancel` to cancel a prediction before it finishes. - ### `replicate.deployments.list` List your deployments. @@ -992,21 +597,6 @@ List your deployments. const response = await replicate.deployments.list(); ``` -```jsonc -{ - "next": null, - "previous": null, - "results": [ - { - "owner": "acme", - "name": "my-app-image-generator", - "current_release": { /* ... */ } - } - /* ... */ - ] -} -``` - ### `replicate.deployments.create` Create a new deployment. @@ -1015,39 +605,6 @@ Create a new deployment. const response = await replicate.deployments.create(options); ``` -| name | type | description | -| ----------------------- | ------ | -------------------------------------------------------------------------------- | -| `options.name` | string | Required. Name of the new deployment | -| `options.model` | string | Required. Name of the model in the format `{username}/{model_name}` | -| `options.version` | string | Required. ID of the model version | -| `options.hardware` | string | Required. SKU of the hardware to run the deployment on (`cpu`, `gpu-a100`, etc.) | -| `options.min_instances` | number | Minimum number of instances to run. Defaults to 0 | -| `options.max_instances` | number | Maximum number of instances to scale up to based on traffic. Defaults to 1 | - -```jsonc -{ - "owner": "acme", - "name": "my-app-image-generator", - "current_release": { - "number": 1, - "model": "stability-ai/sdxl", - "version": "da77bc59ee60423279fd632efb4795ab731d9e3ca9705ef3341091fb989b7eaf", - "created_at": "2024-03-14T11:43:32.049157Z", - "created_by": { - "type": "organization", - "username": "acme", - "name": "Acme, Inc.", - "github_url": "https://github.com/replicate" - }, - "configuration": { - "hardware": "gpu-a100", - "min_instances": 1, - "max_instances": 0 - } - } -} -``` - ### `replicate.deployments.update` Update an existing deployment. @@ -1056,97 +613,25 @@ Update an existing deployment. const response = await replicate.deployments.update(deploymentOwner, deploymentName, options); ``` -| name | type | description | -| ----------------------- | ------ | -------------------------------------------------------------------------------- | -| `deploymentOwner` | string | Required. Owner of the deployment | -| `deploymentName` | string | Required. Name of the deployment to update | -| `options.model` | string | Name of the model in the format `{username}/{model_name}` | -| `options.version` | string | ID of the model version | -| `options.hardware` | string | Required. SKU of the hardware to run the deployment on (`cpu`, `gpu-a100`, etc.) | -| `options.min_instances` | number | Minimum number of instances to run | -| `options.max_instances` | number | Maximum number of instances to scale up to | - -```jsonc -{ - "owner": "acme", - "name": "my-app-image-generator", - "current_release": { - "number": 2, - "model": "stability-ai/sdxl", - "version": "39ed52f2a78e934b3ba6e2a89f5b1c712de7dfea535525255b1aa35c5565e08b", - "created_at": "2024-03-14T11:43:32.049157Z", - "created_by": { - "type": "organization", - "username": "acme", - "name": "Acme, Inc.", - "github_url": "https://github.com/replicate" - }, - "configuration": { - "hardware": "gpu-a100", - "min_instances": 1, - "max_instances": 0 - } - } -} -``` - ### `replicate.files.create` Upload a file to Replicate. > [!TIP] -> The client library calls this endpoint automatically to upload the contents of -> file handles provided as prediction and training inputs. +> The client library calls this endpoint automatically to upload the contents of file handles provided as prediction and training inputs. > You don't need to call this method directly unless you want more control. -> For example, you might want to reuse a file across multiple predictions -> without re-uploading it each time, +> For example, you might want to reuse a file across multiple predictions without re-uploading it each time, > or you may want to set custom metadata on the file resource. > -> You can configure how a client handles file handle inputs -> by setting the `fileEncodingStrategy` option in the -> [client constructor](#constructor). +> You can configure how a client handles file handle inputs by setting the `fileEncodingStrategy` option in the [client constructor](#constructor). ```js const response = await replicate.files.create(file, metadata); ``` -| name | type | description | -| ---------- | --------------------- | ---------------------------------------------------------- | -| `file` | Blob, File, or Buffer | **Required**. The file to upload. | -| `metadata` | object | Optional. User-provided metadata associated with the file. | - -```jsonc -{ - "id": "MTQzODcyMDct0YjZkLWE1ZGYtMmRjZTViNWIwOGEyNjNhNS0", - "name": "photo.webp", - "content_type": "image/webp", - "size": 96936, - "etag": "f211779ff7502705bbf42e9874a17ab3", - "checksums": { - "sha256": "7282eb6991fa4f38d80c312dc207d938c156d714c94681623aedac846488e7d3", - "md5": "f211779ff7502705bbf42e9874a17ab3" - }, - "metadata": { - "customer_reference_id": "123" - }, - "created_at": "2024-06-28T10:16:04.062Z", - "expires_at": "2024-06-29T10:16:04.062Z", - "urls": { - "get": "https://api.replicate.com/v1/files/MTQzODcyMDct0YjZkLWE1ZGYtMmRjZTViNWIwOGEyNjNhNS0" - } -} -``` - Files uploaded to Replicate using this endpoint expire after 24 hours. -Pass the `urls.get` property of a file resource -to use it as an input when running a model on Replicate. -The value of `urls.get` is opaque, -and shouldn't be inferred from other attributes. - -The contents of a file are only made accessible to a model running on Replicate, -and only when passed as a prediction or training input -by the user or organization who created the file. +Pass the `urls.get` property of a file resource to use it as an input when running a model on Replicate. The value of `urls.get` is opaque, and shouldn't be inferred from other attributes. The contents of a file are only made accessible to a model running on Replicate, and only when passed as a prediction or training input by the user or organization who created the file. ### `replicate.files.list` @@ -1164,33 +649,19 @@ Get metadata for a specific file. const response = await replicate.files.get(file_id); ``` -| name | type | description | -| --------- | ------ | --------------------------------- | -| `file_id` | string | **Required**. The ID of the file. | - ### `replicate.files.delete` -Delete a file. - -Files uploaded using the `replicate.files.create` method expire after 24 hours. -You can use this method to delete them sooner. +Delete a file. Files uploaded using the `replicate.files.create` method expire after 24 hours. You can use this method to delete them sooner. ```js const response = await replicate.files.delete(file_id); ``` -| name | type | description | -| --------- | ------ | --------------------------------- | -| `file_id` | string | **Required**. The ID of the file. | - ### `replicate.paginate` -Pass another method as an argument to iterate over results -that are spread across multiple pages. +Pass another method as an argument to iterate over results that are spread across multiple pages. -This method is implemented as an -[async generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator), -which you can use in a for loop or iterate over manually. +This method is implemented as an [async generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator), which you can use in a `for await` loop or iterate over manually. ```js // iterate over paginated results in a for loop @@ -1213,32 +684,18 @@ Low-level method used by the Replicate client to interact with API endpoints. const response = await replicate.request(route, parameters); ``` -| name | type | description | -| -------------------- | ------------------- | ----------- | -| `options.route` | `string` | Required. REST API endpoint path. -| `options.params` | `object` | URL query parameters for the given route. | -| `options.method` | `string` | HTTP method for the given route. | -| `options.headers` | `object` | Additional HTTP headers for the given route. | -| `options.data` | `object \| FormData` | Request body. | -| `options.signal` | `AbortSignal` | Optional `AbortSignal`. | - -The `replicate.request()` method is used by the other methods -to interact with the Replicate API. -You can call this method directly to make other requests to the API. - -The method accepts an `AbortSignal` which can be used to cancel the request in flight. - ### `FileOutput` `FileOutput` is a `ReadableStream` instance that represents a model file output. It can be used to stream file data to disk or as a `Response` body to an HTTP request. ```javascript -const [output] = await replicate.run("black-forest-labs/flux-schnell", { - input: { prompt: "astronaut riding a rocket like a horse" } +const [output] = await replicate.run("black-forest-labs/flux-schnell", { + input: { prompt: "astronaut riding a rocket like a horse" }, }); // To access the file URL: -console.log(output.url()); //=> "http://example.com" +console.log(output.url()); +//=> "http://example.com" // To write the file to disk: fs.writeFile("my-image.png", output); @@ -1258,10 +715,10 @@ You can opt out of FileOutput by passing `useFileOutput: false` to the `Replicat const replicate = new Replicate({ useFileOutput: false }); ``` -| method | returns | description | -| -------------------- | ------ | ------------------------------------------------------------ | -| `blob()` | object | A `Blob` instance containing the binary file | -| `url()` | string | A `URL` object pointing to the underlying data source. Please note that this may not always be an HTTP URL in future. | +| method | returns | description | +| --- | --- | --- | +| `blob()` | object | A `Blob` instance containing the binary file | +| `url()` | string | A `URL` object pointing to the underlying data source. Please note that this may not always be an HTTP URL in future. | ## Troubleshooting From dd872dd1b280a0f0ef72d1ba490e0c4add0a8e5f Mon Sep 17 00:00:00 2001 From: dylanwongtencent Date: Mon, 13 Apr 2026 19:03:39 -0700 Subject: [PATCH 2/3] Added Tencent Cloud Hunyuan model examples to README From b591a1c2a5b50d071497ddff0a3adf6011dbbb48 Mon Sep 17 00:00:00 2001 From: dylanwongtencent Date: Mon, 13 Apr 2026 19:08:42 -0700 Subject: [PATCH 3/3] Update README.md --- README.md | 897 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 751 insertions(+), 146 deletions(-) diff --git a/README.md b/README.md index d10ed1c6..0529f0c7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ # Replicate Node.js client -A Node.js client for [Replicate](https://replicate.com). It lets you run models from your Node.js code, and everything else you can do with [Replicate's HTTP API](https://replicate.com/docs/reference/http). +A Node.js client for [Replicate](https://replicate.com). +It lets you run models from your Node.js code, +and everything else you can do with +[Replicate's HTTP API](https://replicate.com/docs/reference/http). > [!IMPORTANT] > This library can't interact with Replicate's API directly from a browser. @@ -13,7 +16,10 @@ A Node.js client for [Replicate](https://replicate.com). It lets you run models - [Bun](https://bun.sh) >= 1.0 - [Deno](https://deno.com) >= 1.28 -You can also use this client library on most serverless platforms, including [Cloudflare Workers](https://developers.cloudflare.com/workers/), [Vercel functions](https://vercel.com/docs/functions), and [AWS Lambda](https://aws.amazon.com/lambda/). +You can also use this client library on most serverless platforms, including +[Cloudflare Workers](https://developers.cloudflare.com/workers/), +[Vercel functions](https://vercel.com/docs/functions), and +[AWS Lambda](https://aws.amazon.com/lambda/). ## Installation @@ -40,9 +46,7 @@ Instantiate the client: ```js const replicate = new Replicate({ // get your token from https://replicate.com/account/api-tokens - auth: "my api token", - - // defaults to process.env.REPLICATE_API_TOKEN + auth: "my api token", // defaults to process.env.REPLICATE_API_TOKEN }); ``` @@ -54,21 +58,19 @@ const input = { prompt: "a 19th century portrait of a raccoon gentleman wearing a suit", }; const [output] = await replicate.run(model, { input }); - // FileOutput('https://replicate.delivery/pbxt/GtQb3Sgve42ZZyVnt8xjquFk9EX5LP0fF68NTIWlgBMUpguQA/out-0.png') -console.log(output.url()); -// 'https://replicate.delivery/pbxt/GtQb3Sgve42ZZyVnt8xjquFk9EX5LP0fF68NTIWlgBMUpguQA/out-0.png' -console.log(output.blob()); -// Blob +console.log(output.url()); // 'https://replicate.delivery/pbxt/GtQb3Sgve42ZZyVnt8xjquFk9EX5LP0fF68NTIWlgBMUpguQA/out-0.png' +console.log(output.blob()); // Blob ``` > [!NOTE] -> A model that outputs file data returns a `FileOutput` object by default. - -This is an implementation of `ReadableStream` that returns the file contents. It has a `.blob()` method for accessing a `Blob` representation and a `.url()` method that will return the underlying data-source. - -We recommend accessing file data directly either as readable stream or via `.blob()` as the `.url()` property may not always return a HTTP URL in future. +> A model that outputs file data returns a `FileOutput` object by default. This is an implementation +> of `ReadableStream` that returns the file contents. It has a `.blob()` method for accessing a +> `Blob` representation and a `.url()` method that will return the underlying data-source. +> +> We recommend accessing file data directly either as readable stream or via `.blob()` as the +> `.url()` property may not always return a HTTP URL in future. You can also run a model in the background: @@ -95,10 +97,13 @@ console.log(prediction.output); // ['https://replicate.delivery/pbxt/RoaxeXqhL0xaYyLm6w3bpGwF5RaNBjADukfFnMbhOyeoWBdhA/out-0.png'] ``` -To run a model that takes a file input you can pass either a URL to a publicly accessible file on the Internet or a handle to a file on your local device. +To run a model that takes a file input you can pass either +a URL to a publicly accessible file on the Internet +or a handle to a file on your local device. ```js const fs = require("node:fs/promises"); + // Or when using ESM. // import fs from "node:fs/promises"; @@ -107,7 +112,6 @@ const input = { image: await fs.readFile("path/to/image.png"), }; const [output] = await replicate.run(model, { input }); - // FileOutput('https://replicate.delivery/mgxm/e7b0e122-9daa-410e-8cde-006c7308ff4d/output.png') ``` @@ -180,48 +184,50 @@ const output = await replicate.run("tencent/hunyuan3d-2mv", { input }); await writeFile("hunyuan3d-2mv.glb", output); ``` -## TypeScript usage -This library exports TypeScript definitions. +## TypeScript usage -You can import them like this: +This library exports TypeScript definitions. You can import them like this: ```ts -import Replicate, { type Prediction } from "replicate"; +import Replicate, { type Prediction } from 'replicate'; ``` Here's an example that uses the `Prediction` type with a custom `onProgress` callback: ```ts -import Replicate, { type Prediction } from "replicate"; +import Replicate, { type Prediction } from 'replicate'; const replicate = new Replicate(); const model = "black-forest-labs/flux-schnell"; const prompt = "a 19th century portrait of a raccoon gentleman wearing a suit"; - function onProgress(prediction: Prediction) { console.log({ prediction }); } -const output = await replicate.run(model, { input: { prompt } }, onProgress); -console.log({ output }); +const output = await replicate.run(model, { input: { prompt } }, onProgress) +console.log({ output }) ``` See the full list of exported types in [index.d.ts](./index.d.ts). ### Webhooks -Webhooks provide real-time updates about your prediction. Specify an endpoint when you create a prediction, and Replicate will send HTTP POST requests to that URL when the prediction is created, updated, and finished. It is possible to provide a URL to the `predictions.create()` function that will be requested by Replicate when the prediction status changes. This is an alternative to polling. To receive webhooks you’ll need a web server. +Webhooks provide real-time updates about your prediction. Specify an endpoint when you create a prediction, and Replicate will send HTTP POST requests to that URL when the prediction is created, updated, and finished. + +It is possible to provide a URL to the predictions.create() function that will be requested by Replicate when the prediction status changes. This is an alternative to polling. -The following example uses Hono, a web standards based server, but this pattern applies to most frameworks. +To receive webhooks you’ll need a web server. The following example uses Hono, a web standards based server, but this pattern applies to most frameworks. + +
+ See example ```js -import { serve } from "@hono/node-server"; -import { Hono } from "hono"; +import { serve } from '@hono/node-server'; +import { Hono } from 'hono'; const app = new Hono(); - -app.get("/webhooks/replicate", async (c) => { +app.get('/webhooks/replicate', async (c) => { // Get the prediction from the request. const prediction = await c.req.json(); console.log(prediction); @@ -229,74 +235,80 @@ app.get("/webhooks/replicate", async (c) => { // Acknowledge the webhook. c.status(200); - c.json({ ok: true }); -}); + c.json({ok: true}); +})); serve(app, (info) => { - console.log(`Listening on http://localhost:${info.port}`); + console.log(`Listening on http://localhost:${info.port}`) //=> Listening on http://localhost:3000 }); ``` -Create the prediction passing in the webhook URL to `webhook` and specify which events you want to receive in `webhook_events_filter` out of `start`, `output`, `logs`, and `completed`: +
+ +Create the prediction passing in the webhook URL to `webhook` and specify which events you want to receive in `webhook_events_filter` out of "start", "output", ”logs” and "completed": ```js const Replicate = require("replicate"); const replicate = new Replicate(); const input = { - image: "https://replicate.delivery/pbxt/KWDkejqLfER3jrroDTUsSvBWFaHtapPxfg4xxZIqYmfh3zXm/Screenshot%202024-02-28%20at%2022.14.00.png", - denoising_strength: 0.5, - instant_id_strength: 0.8, + image: "https://replicate.delivery/pbxt/KWDkejqLfER3jrroDTUsSvBWFaHtapPxfg4xxZIqYmfh3zXm/Screenshot%202024-02-28%20at%2022.14.00.png", + denoising_strength: 0.5, + instant_id_strength: 0.8 }; const callbackURL = `https://my.app/webhooks/replicate`; - await replicate.predictions.create({ version: "19deaef633fd44776c82edf39fd60e95a7250b8ececf11a725229dc75a81f9ca", input: input, webhook: callbackURL, webhook_events_filter: ["completed"], }); + +// The server will now handle the event and log: +// => {"id": "xyz", "status": "successful", ... } ``` ## Verifying webhooks To prevent unauthorized requests, Replicate signs every webhook and its metadata with a unique key for each user or organization. You can use this signature to verify the webhook indeed comes from Replicate before you process it. -This client includes a `validateWebhook` convenience function that you can use to validate webhooks. To validate webhooks: +This client includes a `validateWebhook` convenience function that you can use to validate webhooks. + +To validate webhooks: 1. Check out the [webhooks guide](https://replicate.com/docs/webhooks) to get started. -2. [Retrieve your webhook signing secret](https://replicate.com/docs/webhooks#retrieving-the-webhook-signing-key) and store it in your environment. -3. Update your webhook handler to call `validateWebhook(request, secret)`, where `request` is an instance of a [web-standard `Request` object](https://developer.mozilla.org/en-US/docs/Web/API/Request), and `secret` is the signing secret for your environment. +1. [Retrieve your webhook signing secret](https://replicate.com/docs/webhooks#retrieving-the-webhook-signing-key) and store it in your enviroment. +1. Update your webhook handler to call `validateWebhook(request, secret)`, where `request` is an instance of a [web-standard `Request` object](https://developer.mozilla.org/en-US/docs/Web/API/object), and `secret` is the signing secret for your environment. Here's an example of how to validate webhooks using Next.js: ```js -import { NextResponse } from "next/server"; -import { validateWebhook } from "replicate"; +import { NextResponse } from 'next/server'; +import { validateWebhook } from 'replicate'; export async function POST(request) { const secret = process.env.REPLICATE_WEBHOOK_SIGNING_SECRET; if (!secret) { - console.log("Skipping webhook validation. To validate webhooks, set REPLICATE_WEBHOOK_SIGNING_SECRET"); + console.log("Skipping webhook validation. To validate webhooks, set REPLICATE_WEBHOOK_SIGNING_SECRET") const body = await request.json(); console.log(body); - return NextResponse.json( - { detail: "Webhook received (but not validated)" }, - { status: 200 } - ); + return NextResponse.json({ detail: "Webhook received (but not validated)" }, { status: 200 }); } const webhookIsValid = await validateWebhook(request.clone(), secret); + if (!webhookIsValid) { return NextResponse.json({ detail: "Webhook is invalid" }, { status: 401 }); } + // process validated webhook here... console.log("Webhook is valid!"); const body = await request.json(); console.log(body); + return NextResponse.json({ detail: "Webhook is valid" }, { status: 200 }); } ``` @@ -305,31 +317,27 @@ If your environment doesn't support `Request` objects, you can pass the required ```js const requestData = { - id: "123", // the `Webhook-Id` header + id: "123", // the `Webhook-Id` header timestamp: "0123456", // the `Webhook-Timestamp` header - signature: "xyz", // the `Webhook-Signature` header - body: "{...}", // the request body as a string, ArrayBuffer or ReadableStream - secret: "shhh", // the webhook secret, obtained from the `replicate.webhooks.default.secret` endpoint + signature: "xyz", // the `Webhook-Signature` header + body: "{...}", // the request body as a string, ArrayBuffer or ReadableStream + secret: "shhh", // the webhook secret, obtained from the `replicate.webhooks.defaul.secret` endpoint }; - const webhookIsValid = await validateWebhook(requestData); ``` > [!NOTE] -> The `validateWebhook` function uses the global `crypto` API available in most JavaScript runtimes. - -Node <= 18 does not provide this global so in this case you need to either call node with the `--no-experimental-global-webcrypto` or provide the `webcrypto` module manually. - -```js -const crypto = require("node:crypto").webcrypto; -const webhookIsValid = await validateWebhook(requestData, crypto); -``` +> The `validateWebhook` function uses the global `crypto` API available in most JavaScript runtimes. Node <= 18 does not provide this global so in this case you need to either call node with the `--no-experimental-global-webcrypto` or provide the `webcrypto` module manually. +> ```js +> const crypto = require("node:crypto").webcrypto; +> const webhookIsValid = await valdiateWebhook(requestData, crypto); +> ``` ## TypeScript The `Replicate` constructor and all `replicate.*` methods are fully typed. -Currently in order to support the module format used by `replicate` you'll need to set `esModuleInterop` to `true` in your `tsconfig.json`. +Currently in order to support the module format used by `replicate` you'll need to set `esModuleInterop` to `true` in your tsconfig.json. ## API @@ -339,18 +347,30 @@ Currently in order to support the module format used by `replicate` you'll need const replicate = new Replicate(options); ``` -| name | type | description | -| --- | --- | --- | -| `options.auth` | string | **Required**. API access token | -| `options.userAgent` | string | Identifier of your app. Defaults to `replicate-javascript/${packageJSON.version}` | -| `options.baseUrl` | string | Defaults to `https://api.replicate.com/v1` | -| `options.fetch` | function | Fetch function to use. Defaults to `globalThis.fetch` | -| `options.fileEncodingStrategy` | string | Determines the file encoding strategy to use. Possible values: `"default"`, `"upload"`, or `"data-uri"`. Defaults to `"default"` | -| `options.useFileOutput` | boolean | Determines if the `replicate.run()` method should convert file output into `FileOutput` objects | - -The client makes requests to Replicate's API using [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch). By default, the `globalThis.fetch` function is used, which is available on [Node.js 18](https://nodejs.org/en/blog/announcements/v18-release-announce#fetch-experimental) and later, as well as [Cloudflare Workers](https://developers.cloudflare.com/workers/runtime-apis/fetch/), [Vercel Functions](https://vercel.com/docs/functions), and other environments. - -On earlier versions of Node.js and other environments where global fetch isn't available, you can install a fetch function from an external package like [cross-fetch](https://www.npmjs.com/package/cross-fetch) and pass it to the `fetch` option in the constructor. +| name | type | description | +| ------------------------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------- | +| `options.auth` | string | **Required**. API access token | +| `options.userAgent` | string | Identifier of your app. Defaults to `replicate-javascript/${packageJSON.version}` | +| `options.baseUrl` | string | Defaults to https://api.replicate.com/v1 | +| `options.fetch` | function | Fetch function to use. Defaults to `globalThis.fetch` | +| `options.fileEncodingStrategy` | string | Determines the file encoding strategy to use. Possible values: `"default"`, `"upload"`, or `"data-uri"`. Defaults to `"default"` | +| `options.useFileOutput` | boolean | Determines if the `replicate.run()` method should convert file output into `FileOutput` objects | + + +The client makes requests to Replicate's API using +[fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch). +By default, the `globalThis.fetch` function is used, +which is available on [Node.js 18](https://nodejs.org/en/blog/announcements/v18-release-announce#fetch-experimental) and later, +as well as +[Cloudflare Workers](https://developers.cloudflare.com/workers/runtime-apis/fetch/), +[Vercel Functions](https://vercel.com/docs/functions), +and other environments. + +On earlier versions of Node.js +and other environments where global fetch isn't available, +you can install a fetch function from an external package like +[cross-fetch](https://www.npmjs.com/package/cross-fetch) +and pass it to the `fetch` option in the constructor. ```js const Replicate = require("replicate"); @@ -363,13 +383,16 @@ const fetch = require("fetch"); const replicate = new Replicate({ fetch }); ``` -You can also use the `fetch` option to add custom behavior to client requests, such as injecting headers or adding log statements. +You can also use the `fetch` option to add custom behavior to client requests, +such as injecting headers or adding log statements. ```js const customFetch = (url, options) => { const headers = options && options.headers ? { ...options.headers } : {}; headers["X-Custom-Header"] = "some value"; + console.log("fetch", { url, ...options, headers }); + return fetch(url, { ...options, headers }); }; @@ -378,33 +401,33 @@ const replicate = new Replicate({ fetch: customFetch }); ### `replicate.run` -Run a model and await the result. - -Unlike [`replicate.predictions.create`](#replicatepredictionscreate), this method returns only the prediction output rather than the entire prediction object. +Run a model and await the result. Unlike [`replicate.prediction.create`](#replicatepredictionscreate), this method returns only the prediction output rather than the entire prediction object. ```js const output = await replicate.run(identifier, options, progress); ``` -| name | type | description | -| --- | --- | --- | -| `identifier` | string | **Required**. The model version identifier in the format `{owner}/{name}:{version}`, for example `stability-ai/sdxl:8beff3369e81422112d93b89ca01426147de542cd4684c244b673b105188fe5f` | -| `options.input` | object | **Required**. An object with the model inputs. | -| `options.wait` | object | Options for waiting for the prediction to finish | -| `options.wait.mode` | `"poll" \| "block"` | `"block"` holds the request open, `"poll"` makes repeated requests to fetch the prediction. Defaults to `"block"` | -| `options.wait.interval` | number | Polling interval in milliseconds. Defaults to 500 | -| `options.wait.timeout` | number | In `"block"` mode determines how long the request will be held open until falling back to polling. Defaults to 60 | -| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output | -| `options.webhook_events_filter` | string[] | An array of events which should trigger [webhooks](https://replicate.com/docs/webhooks). Allowable values are `start`, `output`, `logs`, and `completed` | -| `options.signal` | object | An [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel the prediction | -| `progress` | function | Callback function that receives the prediction object as it's updated. The function is called when the prediction is created, each time it's updated while polling for completion, and when it's completed. | +| name | type | description | +| ------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `identifier` | string | **Required**. The model version identifier in the format `{owner}/{name}:{version}`, for example `stability-ai/sdxl:8beff3369e81422112d93b89ca01426147de542cd4684c244b673b105188fe5f` | +| `options.input` | object | **Required**. An object with the model inputs. | +| `options.wait` | object | Options for waiting for the prediction to finish | +| `options.wait.mode` | `"poll" \| "block"` | `"block"` holds the request open, `"poll"` makes repeated requests to fetch the prediction. Defaults to `"block"` | +| `options.wait.interval` | number | Polling interval in milliseconds. Defaults to 500 | +| `options.wait.timeout` | number | In `"block"` mode determines how long the request will be held open until falling back to polling. Defaults to 60 | +| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output | +| `options.webhook_events_filter` | string[] | An array of events which should trigger [webhooks](https://replicate.com/docs/webhooks). Allowable values are `start`, `output`, `logs`, and `completed` | +| `options.signal` | object | An [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel the prediction | +| `progress` | function | Callback function that receives the prediction object as it's updated. The function is called when the prediction is created, each time it's updated while polling for completion, and when it's completed. | Throws `Error` if the prediction failed. -Returns `Promise` which resolves with the output of running the model. +Returns `Promise` which resolves with the output of running the model. > [!NOTE] -> Currently the TypeScript return type of `replicate.run()` is `Promise`; this is misleading as a model can return array types as well as primitive types like strings, numbers and booleans. +> Currently the TypeScript return type of `replicate.run()` is `Promise` this is +> misleading as a model can return array types as well as primative types like strings, +> numbers and booleans. Example: @@ -419,40 +442,39 @@ Example that logs progress as the model is running: ```js const model = "stability-ai/sdxl:8beff3369e81422112d93b89ca01426147de542cd4684c244b673b105188fe5f"; const input = { prompt: "a 19th century portrait of a raccoon gentleman wearing a suit" }; - const onProgress = (prediction) => { - const last_log_line = prediction.logs.split("\n").pop(); - console.log({ id: prediction.id, log: last_log_line }); -}; - -const output = await replicate.run(model, { input }, onProgress); + const last_log_line = prediction.logs.split("\n").pop() + console.log({id: prediction.id, log: last_log_line}) +} +const output = await replicate.run(model, { input }, onProgress) ``` #### Sync vs. Async API (`"poll"` vs. `"block"`) -The `replicate.run()` API takes advantage of the [Replicate sync API](https://replicate.com/docs/topics/predictions/create-a-prediction) which is optimized for low latency requests to file models like `black-forest-labs/flux-dev` and `black-forest-labs/flux-schnell`. When creating a prediction this will hold a connection open to the server and return a `FileObject` containing the generated file as quickly as possible. +The `replicate.run()` API takes advantage of the [Replicate sync API](https://replicate.com/docs/topics/predictions/create-a-prediction) +which is optimized for low latency requests to file models like `black-forest-labs/flux-dev` and +`black-forest-labs/flux-schnell`. When creating a prediction this will hold a connection open to the +server and return a `FileObject` containing the generated file as quickly as possible. ### `replicate.stream` Run a model and stream its output. Unlike [`replicate.prediction.create`](#replicatepredictionscreate), this method returns only the prediction output rather than the entire prediction object. ```js -for await (const event of replicate.stream(identifier, options)) { - /* ... */ -} +for await (const event of replicate.stream(identifier, options)) { /* ... */ } ``` -| name | type | description | -| --- | --- | --- | -| `identifier` | string | **Required**. The model version identifier in the format `{owner}/{name}` or `{owner}/{name}:{version}`, for example `meta/llama-2-70b-chat` | -| `options.input` | object | **Required**. An object with the model inputs. | -| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output | +| name | type | description | +| ------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `identifier` | string | **Required**. The model version identifier in the format `{owner}/{name}` or `{owner}/{name}:{version}`, for example `meta/llama-2-70b-chat` | +| `options.input` | object | **Required**. An object with the model inputs. | +| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output | | `options.webhook_events_filter` | string[] | An array of events which should trigger [webhooks](https://replicate.com/docs/webhooks). Allowable values are `start`, `output`, `logs`, and `completed` | -| `options.signal` | object | An [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel the prediction | +| `options.signal` | object | An [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel the prediction | Throws `Error` if the prediction failed. -Returns `AsyncGenerator` which yields the events of running the model. +Returns `AsyncGenerator` which yields the events of running the model. Example: @@ -464,8 +486,8 @@ const options = { }, // webhook: "https://smee.io/dMUlmOMkzeyRGjW" // optional }; - const output = []; + for await (const { event, data } of replicate.stream(model, options)) { if (event === "output") { output.push(data); @@ -479,14 +501,256 @@ console.log(output.join("").trim()); A stream generates server-sent events with the following properties: -| name | type | description | -| --- | --- | --- | -| `event` | string | The type of event. | -| `data` | string | The event data. | -| `id` | string | The event ID. | -| `retry` | number | The reconnection interval in milliseconds. | +| name | type | description | +| ------- | ------ | ---------------------------------------------------------------------------- | +| `event` | string | The type of event. Possible values are `output`, `logs`, `error`, and `done` | +| `data` | string | The event data | +| `id` | string | The event id | +| `retry` | number | The number of milliseconds to wait before reconnecting to the server | + +As the prediction runs, the generator yields `output` and `logs` events. If an error occurs, the generator yields an `error` event with a JSON object containing the error message set to the `data` property. When the prediction is done, the generator yields a `done` event with an empty JSON object set to the `data` property. + +Events with the `output` event type have their `toString()` method overridden to return the event data as a string. Other event types return an empty string. + +### `replicate.models.get` + +Get metadata for a public model or a private model that you own. + +```js +const response = await replicate.models.get(model_owner, model_name); +``` + +| name | type | description | +| ------------- | ------ | ----------------------------------------------------------------------- | +| `model_owner` | string | **Required**. The name of the user or organization that owns the model. | +| `model_name` | string | **Required**. The name of the model. | + +```jsonc +{ + "url": "https://replicate.com/replicate/hello-world", + "owner": "replicate", + "name": "hello-world", + "description": "A tiny model that says hello", + "visibility": "public", + "github_url": "https://github.com/replicate/cog-examples", + "paper_url": null, + "license_url": null, + "latest_version": { + /* ... */ + } +} +``` + +### `replicate.models.list` + +Get a paginated list of all public models. + +```js +const response = await replicate.models.list(); +``` + +```jsonc +{ + "next": null, + "previous": null, + "results": [ + { + "url": "https://replicate.com/replicate/hello-world", + "owner": "replicate", + "name": "hello-world", + "description": "A tiny model that says hello", + "visibility": "public", + "github_url": "https://github.com/replicate/cog-examples", + "paper_url": null, + "license_url": null, + "run_count": 5681081, + "cover_image_url": "...", + "default_example": { + /* ... */ + }, + "latest_version": { + /* ... */ + } + } + ] +} +``` + +### `replicate.models.search` + +Search for public models on Replicate. + +```js +const response = await replicate.models.search(query); +``` + +| name | type | description | +| ------- | ------ | -------------------------------------- | +| `query` | string | **Required**. The search query string. | + +### `replicate.models.create` + +Create a new public or private model. + +```js +const response = await replicate.models.create(model_owner, model_name, options); +``` + +| name | type | description | +| ------------------------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `model_owner` | string | **Required**. The name of the user or organization that will own the model. This must be the same as the user or organization that is making the API request. In other words, the API token used in the request must belong to this user or organization. | +| `model_name` | string | **Required**. The name of the model. This must be unique among all models owned by the user or organization. | +| `options.visibility` | string | **Required**. Whether the model should be public or private. A public model can be viewed and run by anyone, whereas a private model can be viewed and run only by the user or organization members that own the model. | +| `options.hardware` | string | **Required**. The SKU for the hardware used to run the model. Possible values can be found by calling [`replicate.hardware.list()`](#replicatehardwarelist). | +| `options.description` | string | A description of the model. | +| `options.github_url` | string | A URL for the model's source code on GitHub. | +| `options.paper_url` | string | A URL for the model's paper. | +| `options.license_url` | string | A URL for the model's license. | +| `options.cover_image_url` | string | A URL for the model's cover image. This should be an image file. | + +### `replicate.hardware.list` + +List available hardware for running models on Replicate. + +```js +const response = await replicate.hardware.list() +``` + +```jsonc +[ + {"name": "CPU", "sku": "cpu" }, + {"name": "Nvidia T4 GPU", "sku": "gpu-t4" }, + {"name": "Nvidia A40 GPU", "sku": "gpu-a40-small" }, + {"name": "Nvidia A40 (Large) GPU", "sku": "gpu-a40-large" }, +] +``` + +### `replicate.models.versions.list` + +Get a list of all published versions of a model, including input and output schemas for each version. + +```js +const response = await replicate.models.versions.list(model_owner, model_name); +``` + +| name | type | description | +| ------------- | ------ | ----------------------------------------------------------------------- | +| `model_owner` | string | **Required**. The name of the user or organization that owns the model. | +| `model_name` | string | **Required**. The name of the model. | + +```jsonc +{ + "previous": null, + "next": null, + "results": [ + { + "id": "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", + "created_at": "2022-04-26T19:29:04.418669Z", + "cog_version": "0.3.0", + "openapi_schema": { + /* ... */ + } + }, + { + "id": "e2e8c39e0f77177381177ba8c4025421ec2d7e7d3c389a9b3d364f8de560024f", + "created_at": "2022-03-21T13:01:04.418669Z", + "cog_version": "0.3.0", + "openapi_schema": { + /* ... */ + } + } + ] +} +``` + +### `replicate.models.versions.get` + +Get metadata for a specific version of a model. + +```js +const response = await replicate.models.versions.get(model_owner, model_name, version_id); +``` + +| name | type | description | +| ------------- | ------ | ----------------------------------------------------------------------- | +| `model_owner` | string | **Required**. The name of the user or organization that owns the model. | +| `model_name` | string | **Required**. The name of the model. | +| `version_id` | string | **Required**. The model version | + +```jsonc +{ + "id": "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", + "created_at": "2022-04-26T19:29:04.418669Z", + "cog_version": "0.3.0", + "openapi_schema": { + /* ... */ + } +} +``` + +### `replicate.collections.get` + +Get a list of curated model collections. See [replicate.com/collections](https://replicate.com/collections). + +```js +const response = await replicate.collections.get(collection_slug); +``` + +| name | type | description | +| ----------------- | ------ | ------------------------------------------------------------------------------ | +| `collection_slug` | string | **Required**. The slug of the collection. See http://replicate.com/collections | + +### `replicate.predictions.create` + +Run a model with inputs you provide. + +```js +const response = await replicate.predictions.create(options); +``` + +| name | type | description | +| ------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------- | +| `options.input` | object | **Required**. An object with the model's inputs | +| `options.model` | string | The name of the model, e.g. `black-forest-labs/flux-schnell`. This is required if you're running an [official model](https://replicate.com/explore#official-models). | +| `options.version` | string | The 64-character [model version id](https://replicate.com/docs/topics/models/versions), e.g. `80537f9eead1a5bfa72d5ac6ea6414379be41d4d4f6679fd776e9535d1eb58bb`. This is required if you're not specifying a `model`. | +| `options.wait` | number | Wait up to 60s for the prediction to finish before returning. Disabled by default. See [Synchronous predictions](https://replicate.com/docs/topics/predictions/create-a-prediction#sync-mode) for more information. | +| `options.stream` | boolean | Requests a URL for streaming output output | +| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output | +| `options.webhook_events_filter` | string[] | You can change which events trigger webhook requests by specifying webhook events (`start` \| `output` \| `logs` \| `completed`) | + +```jsonc +{ + "id": "ufawqhfynnddngldkgtslldrkq", + "version": "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", + "status": "succeeded", + "input": { + "text": "Alice" + }, + "output": null, + "error": null, + "logs": null, + "metrics": {}, + "created_at": "2022-04-26T22:13:06.224088Z", + "started_at": null, + "completed_at": null, + "urls": { + "get": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq", + "cancel": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq/cancel", + "stream": "https://streaming.api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq" // Present only if `options.stream` is `true` + } +} +``` + +#### Streaming -Predictions that are streamed receive a `urls.stream` field in the prediction object. This point to an SSE event stream that can be used to consume the model's output. +Specify the `stream` option when creating a prediction +to request a URL to receive streaming output using +[server-sent events (SSE)](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events). + +If the requested model version supports streaming, +then the returned prediction will have a `stream` entry in its `urls` property +with a URL that you can use to construct an +[`EventSource`](https://developer.mozilla.org/en-US/docs/Web/API/EventSource). ```js if (prediction && prediction.urls && prediction.urls.stream) { @@ -509,13 +773,14 @@ if (prediction && prediction.urls && prediction.urls.stream) { A prediction's event stream consists of the following event types: -| event | format | description | -| --- | --- | --- | +| event | format | description | +| -------- | ---------- | ---------------------------------------------- | | `output` | plain text | Emitted when the prediction returns new output | -| `error` | JSON | Emitted when the prediction returns an error | -| `done` | JSON | Emitted when the prediction finishes | +| `error` | JSON | Emitted when the prediction returns an error | +| `done` | JSON | Emitted when the prediction finishes | -A `done` event is emitted when a prediction finishes successfully, is cancelled, or produces an error. +A `done` event is emitted when a prediction finishes successfully, +is cancelled, or produces an error. ### `replicate.predictions.get` @@ -523,6 +788,32 @@ A `done` event is emitted when a prediction finishes successfully, is cancelled, const response = await replicate.predictions.get(prediction_id); ``` +| name | type | description | +| --------------- | ------ | ------------------------------- | +| `prediction_id` | number | **Required**. The prediction id | + +```jsonc +{ + "id": "ufawqhfynnddngldkgtslldrkq", + "version": "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", + "urls": { + "get": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq", + "cancel": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq/cancel" + }, + "status": "starting", + "input": { + "text": "Alice" + }, + "output": null, + "error": null, + "logs": null, + "metrics": {}, + "created_at": "2022-04-26T22:13:06.224088Z", + "started_at": null, + "completed_at": null +} +``` + ### `replicate.predictions.cancel` Stop a running prediction before it finishes. @@ -531,6 +822,32 @@ Stop a running prediction before it finishes. const response = await replicate.predictions.cancel(prediction_id); ``` +| name | type | description | +| --------------- | ------ | ------------------------------- | +| `prediction_id` | number | **Required**. The prediction id | + +```jsonc +{ + "id": "ufawqhfynnddngldkgtslldrkq", + "version": "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", + "urls": { + "get": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq", + "cancel": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq/cancel" + }, + "status": "canceled", + "input": { + "text": "Alice" + }, + "output": null, + "error": null, + "logs": null, + "metrics": {}, + "created_at": "2022-04-26T22:13:06.224088Z", + "started_at": "2022-04-26T22:13:06.224088Z", + "completed_at": "2022-04-26T22:13:06.224088Z" +} +``` + ### `replicate.predictions.list` Get a paginated list of all the predictions you've created. @@ -541,16 +858,74 @@ const response = await replicate.predictions.list(); `replicate.predictions.list()` takes no arguments. +```jsonc +{ + "previous": null, + "next": "https://api.replicate.com/v1/predictions?cursor=cD0yMDIyLTAxLTIxKzIzJTNBMTglM0EyNC41MzAzNTclMkIwMCUzQTAw", + "results": [ + { + "id": "jpzd7hm5gfcapbfyt4mqytarku", + "version": "b21cbe271e65c1718f2999b038c18b45e21e4fba961181fbfae9342fc53b9e05", + "urls": { + "get": "https://api.replicate.com/v1/predictions/jpzd7hm5gfcapbfyt4mqytarku", + "cancel": "https://api.replicate.com/v1/predictions/jpzd7hm5gfcapbfyt4mqytarku/cancel" + }, + "source": "web", + "status": "succeeded", + "created_at": "2022-04-26T20:00:40.658234Z", + "started_at": "2022-04-26T20:00:84.583803Z", + "completed_at": "2022-04-26T20:02:27.648305Z" + } + /* ... */ + ] +} +``` + ### `replicate.trainings.create` -Use the [training API](https://replicate.com/docs/fine-tuning) to fine-tune language models to make them better at a particular task. To see what **language models** currently support fine-tuning, check out Replicate's [collection of trainable language models](https://replicate.com/collections/trainable-language-models). +Use the [training API](https://replicate.com/docs/fine-tuning) to fine-tune language models +to make them better at a particular task. +To see what **language models** currently support fine-tuning, +check out Replicate's [collection of trainable language models](https://replicate.com/collections/trainable-language-models). -If you're looking to fine-tune **image models**, check out Replicate's [guide to fine-tuning image models](https://replicate.com/docs/guides/fine-tune-an-image-model). +If you're looking to fine-tune **image models**, +check out Replicate's [guide to fine-tuning image models](https://replicate.com/docs/guides/fine-tune-an-image-model). ```js const response = await replicate.trainings.create(model_owner, model_name, version_id, options); ``` +| name | type | description | +| ------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------- | +| `model_owner` | string | **Required**. The name of the user or organization that owns the model. | +| `model_name` | string | **Required**. The name of the model. | +| `version` | string | **Required**. The model version | +| `options.destination` | string | **Required**. The destination for the trained version in the form `{username}/{model_name}` | +| `options.input` | object | **Required**. An object with the model's inputs | +| `options.webhook` | string | An HTTPS URL for receiving a webhook when the training has new output | +| `options.webhook_events_filter` | string[] | You can change which events trigger webhook requests by specifying webhook events (`start` \| `output` \| `logs` \| `completed`) | + +```jsonc +{ + "id": "zz4ibbonubfz7carwiefibzgga", + "version": "3ae0799123a1fe11f8c89fd99632f843fc5f7a761630160521c4253149754523", + "status": "starting", + "input": { + "text": "..." + }, + "output": null, + "error": null, + "logs": null, + "started_at": null, + "created_at": "2023-03-28T21:47:58.566434Z", + "completed_at": null +} +``` + +> **Warning** +> If you try to fine-tune a model that doesn't support training, +> you'll get a `400 Bad Request` response from the server. + ### `replicate.trainings.get` Get metadata and status of a training. @@ -559,6 +934,31 @@ Get metadata and status of a training. const response = await replicate.trainings.get(training_id); ``` +| name | type | description | +| ------------- | ------ | ----------------------------- | +| `training_id` | number | **Required**. The training id | + +```jsonc +{ + "id": "zz4ibbonubfz7carwiefibzgga", + "version": "3ae0799123a1fe11f8c89fd99632f843fc5f7a761630160521c4253149754523", + "status": "succeeded", + "input": { + "data": "..." + "param1": "..." + }, + "output": { + "version": "..." + }, + "error": null, + "logs": null, + "webhook_completed": null, + "started_at": "2023-03-28T21:48:02.402755Z", + "created_at": "2023-03-28T21:47:58.566434Z", + "completed_at": "2023-03-28T02:49:48.492023Z" +} +``` + ### `replicate.trainings.cancel` Stop a running training job before it finishes. @@ -567,6 +967,31 @@ Stop a running training job before it finishes. const response = await replicate.trainings.cancel(training_id); ``` +| name | type | description | +| ------------- | ------ | ----------------------------- | +| `training_id` | number | **Required**. The training id | + +```jsonc +{ + "id": "zz4ibbonubfz7carwiefibzgga", + "version": "3ae0799123a1fe11f8c89fd99632f843fc5f7a761630160521c4253149754523", + "status": "canceled", + "input": { + "data": "..." + "param1": "..." + }, + "output": { + "version": "..." + }, + "error": null, + "logs": null, + "webhook_completed": null, + "started_at": "2023-03-28T21:48:02.402755Z", + "created_at": "2023-03-28T21:47:58.566434Z", + "completed_at": "2023-03-28T02:49:48.492023Z" +} +``` + ### `replicate.trainings.list` Get a paginated list of all the trainings you've run. @@ -577,18 +1002,50 @@ const response = await replicate.trainings.list(); `replicate.trainings.list()` takes no arguments. +```jsonc +{ + "previous": null, + "next": "https://api.replicate.com/v1/trainings?cursor=cD0yMDIyLTAxLTIxKzIzJTNBMTglM0EyNC41MzAzNTclMkIwMCUzQTAw", + "results": [ + { + "id": "jpzd7hm5gfcapbfyt4mqytarku", + "version": "b21cbe271e65c1718f2999b038c18b45e21e4fba961181fbfae9342fc53b9e05", + "urls": { + "get": "https://api.replicate.com/v1/trainings/jpzd7hm5gfcapbfyt4mqytarku", + "cancel": "https://api.replicate.com/v1/trainings/jpzd7hm5gfcapbfyt4mqytarku/cancel" + }, + "source": "web", + "status": "succeeded", + "created_at": "2022-04-26T20:00:40.658234Z", + "started_at": "2022-04-26T20:00:84.583803Z", + "completed_at": "2022-04-26T20:02:27.648305Z" + } + /* ... */ + ] +} +``` + ### `replicate.deployments.predictions.create` -Run a model using your own custom deployment. Deployments allow you to run a model with a private, fixed API endpoint. You can configure the version of the model, the hardware it runs on, and how it scales. See the [deployments guide](https://replicate.com/docs/deployments) to learn more and get started. +Run a model using your own custom deployment. + +Deployments allow you to run a model with a private, fixed API endpoint. You can configure the version of the model, the hardware it runs on, and how it scales. See the [deployments guide](https://replicate.com/docs/deployments) to learn more and get started. ```js -const response = await replicate.deployments.predictions.create( - deployment_owner, - deployment_name, - options -); +const response = await replicate.deployments.predictions.create(deployment_owner, deployment_name, options); ``` +| name | type | description | +| ------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------- | +| `deployment_owner` | string | **Required**. The name of the user or organization that owns the deployment | +| `deployment_name` | string | **Required**. The name of the deployment | +| `options.input` | object | **Required**. An object with the model's inputs | +| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output | +| `options.webhook_events_filter` | string[] | You can change which events trigger webhook requests by specifying webhook events (`start` \| `output` \| `logs` \| `completed`) | + +Use `replicate.wait` to wait for a prediction to finish, +or `replicate.predictions.cancel` to cancel a prediction before it finishes. + ### `replicate.deployments.list` List your deployments. @@ -597,6 +1054,21 @@ List your deployments. const response = await replicate.deployments.list(); ``` +```jsonc +{ + "next": null, + "previous": null, + "results": [ + { + "owner": "acme", + "name": "my-app-image-generator", + "current_release": { /* ... */ } + } + /* ... */ + ] +} +``` + ### `replicate.deployments.create` Create a new deployment. @@ -605,6 +1077,39 @@ Create a new deployment. const response = await replicate.deployments.create(options); ``` +| name | type | description | +| ----------------------- | ------ | -------------------------------------------------------------------------------- | +| `options.name` | string | Required. Name of the new deployment | +| `options.model` | string | Required. Name of the model in the format `{username}/{model_name}` | +| `options.version` | string | Required. ID of the model version | +| `options.hardware` | string | Required. SKU of the hardware to run the deployment on (`cpu`, `gpu-a100`, etc.) | +| `options.min_instances` | number | Minimum number of instances to run. Defaults to 0 | +| `options.max_instances` | number | Maximum number of instances to scale up to based on traffic. Defaults to 1 | + +```jsonc +{ + "owner": "acme", + "name": "my-app-image-generator", + "current_release": { + "number": 1, + "model": "stability-ai/sdxl", + "version": "da77bc59ee60423279fd632efb4795ab731d9e3ca9705ef3341091fb989b7eaf", + "created_at": "2024-03-14T11:43:32.049157Z", + "created_by": { + "type": "organization", + "username": "acme", + "name": "Acme, Inc.", + "github_url": "https://github.com/replicate" + }, + "configuration": { + "hardware": "gpu-a100", + "min_instances": 1, + "max_instances": 0 + } + } +} +``` + ### `replicate.deployments.update` Update an existing deployment. @@ -613,25 +1118,97 @@ Update an existing deployment. const response = await replicate.deployments.update(deploymentOwner, deploymentName, options); ``` +| name | type | description | +| ----------------------- | ------ | -------------------------------------------------------------------------------- | +| `deploymentOwner` | string | Required. Owner of the deployment | +| `deploymentName` | string | Required. Name of the deployment to update | +| `options.model` | string | Name of the model in the format `{username}/{model_name}` | +| `options.version` | string | ID of the model version | +| `options.hardware` | string | Required. SKU of the hardware to run the deployment on (`cpu`, `gpu-a100`, etc.) | +| `options.min_instances` | number | Minimum number of instances to run | +| `options.max_instances` | number | Maximum number of instances to scale up to | + +```jsonc +{ + "owner": "acme", + "name": "my-app-image-generator", + "current_release": { + "number": 2, + "model": "stability-ai/sdxl", + "version": "39ed52f2a78e934b3ba6e2a89f5b1c712de7dfea535525255b1aa35c5565e08b", + "created_at": "2024-03-14T11:43:32.049157Z", + "created_by": { + "type": "organization", + "username": "acme", + "name": "Acme, Inc.", + "github_url": "https://github.com/replicate" + }, + "configuration": { + "hardware": "gpu-a100", + "min_instances": 1, + "max_instances": 0 + } + } +} +``` + ### `replicate.files.create` Upload a file to Replicate. > [!TIP] -> The client library calls this endpoint automatically to upload the contents of file handles provided as prediction and training inputs. +> The client library calls this endpoint automatically to upload the contents of +> file handles provided as prediction and training inputs. > You don't need to call this method directly unless you want more control. -> For example, you might want to reuse a file across multiple predictions without re-uploading it each time, +> For example, you might want to reuse a file across multiple predictions +> without re-uploading it each time, > or you may want to set custom metadata on the file resource. > -> You can configure how a client handles file handle inputs by setting the `fileEncodingStrategy` option in the [client constructor](#constructor). +> You can configure how a client handles file handle inputs +> by setting the `fileEncodingStrategy` option in the +> [client constructor](#constructor). ```js const response = await replicate.files.create(file, metadata); ``` +| name | type | description | +| ---------- | --------------------- | ---------------------------------------------------------- | +| `file` | Blob, File, or Buffer | **Required**. The file to upload. | +| `metadata` | object | Optional. User-provided metadata associated with the file. | + +```jsonc +{ + "id": "MTQzODcyMDct0YjZkLWE1ZGYtMmRjZTViNWIwOGEyNjNhNS0", + "name": "photo.webp", + "content_type": "image/webp", + "size": 96936, + "etag": "f211779ff7502705bbf42e9874a17ab3", + "checksums": { + "sha256": "7282eb6991fa4f38d80c312dc207d938c156d714c94681623aedac846488e7d3", + "md5": "f211779ff7502705bbf42e9874a17ab3" + }, + "metadata": { + "customer_reference_id": "123" + }, + "created_at": "2024-06-28T10:16:04.062Z", + "expires_at": "2024-06-29T10:16:04.062Z", + "urls": { + "get": "https://api.replicate.com/v1/files/MTQzODcyMDct0YjZkLWE1ZGYtMmRjZTViNWIwOGEyNjNhNS0" + } +} +``` + Files uploaded to Replicate using this endpoint expire after 24 hours. -Pass the `urls.get` property of a file resource to use it as an input when running a model on Replicate. The value of `urls.get` is opaque, and shouldn't be inferred from other attributes. The contents of a file are only made accessible to a model running on Replicate, and only when passed as a prediction or training input by the user or organization who created the file. +Pass the `urls.get` property of a file resource +to use it as an input when running a model on Replicate. +The value of `urls.get` is opaque, +and shouldn't be inferred from other attributes. + +The contents of a file are only made accessible to a model running on Replicate, +and only when passed as a prediction or training input +by the user or organization who created the file. ### `replicate.files.list` @@ -649,19 +1226,33 @@ Get metadata for a specific file. const response = await replicate.files.get(file_id); ``` +| name | type | description | +| --------- | ------ | --------------------------------- | +| `file_id` | string | **Required**. The ID of the file. | + ### `replicate.files.delete` -Delete a file. Files uploaded using the `replicate.files.create` method expire after 24 hours. You can use this method to delete them sooner. +Delete a file. + +Files uploaded using the `replicate.files.create` method expire after 24 hours. +You can use this method to delete them sooner. ```js const response = await replicate.files.delete(file_id); ``` +| name | type | description | +| --------- | ------ | --------------------------------- | +| `file_id` | string | **Required**. The ID of the file. | + ### `replicate.paginate` -Pass another method as an argument to iterate over results that are spread across multiple pages. +Pass another method as an argument to iterate over results +that are spread across multiple pages. -This method is implemented as an [async generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator), which you can use in a `for await` loop or iterate over manually. +This method is implemented as an +[async generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator), +which you can use in a for loop or iterate over manually. ```js // iterate over paginated results in a for loop @@ -684,18 +1275,32 @@ Low-level method used by the Replicate client to interact with API endpoints. const response = await replicate.request(route, parameters); ``` +| name | type | description | +| -------------------- | ------------------- | ----------- | +| `options.route` | `string` | Required. REST API endpoint path. +| `options.params` | `object` | URL query parameters for the given route. | +| `options.method` | `string` | HTTP method for the given route. | +| `options.headers` | `object` | Additional HTTP headers for the given route. | +| `options.data` | `object \| FormData` | Request body. | +| `options.signal` | `AbortSignal` | Optional `AbortSignal`. | + +The `replicate.request()` method is used by the other methods +to interact with the Replicate API. +You can call this method directly to make other requests to the API. + +The method accepts an `AbortSignal` which can be used to cancel the request in flight. + ### `FileOutput` `FileOutput` is a `ReadableStream` instance that represents a model file output. It can be used to stream file data to disk or as a `Response` body to an HTTP request. ```javascript -const [output] = await replicate.run("black-forest-labs/flux-schnell", { - input: { prompt: "astronaut riding a rocket like a horse" }, +const [output] = await replicate.run("black-forest-labs/flux-schnell", { + input: { prompt: "astronaut riding a rocket like a horse" } }); // To access the file URL: -console.log(output.url()); -//=> "http://example.com" +console.log(output.url()); //=> "http://example.com" // To write the file to disk: fs.writeFile("my-image.png", output); @@ -715,10 +1320,10 @@ You can opt out of FileOutput by passing `useFileOutput: false` to the `Replicat const replicate = new Replicate({ useFileOutput: false }); ``` -| method | returns | description | -| --- | --- | --- | -| `blob()` | object | A `Blob` instance containing the binary file | -| `url()` | string | A `URL` object pointing to the underlying data source. Please note that this may not always be an HTTP URL in future. | +| method | returns | description | +| -------------------- | ------ | ------------------------------------------------------------ | +| `blob()` | object | A `Blob` instance containing the binary file | +| `url()` | string | A `URL` object pointing to the underlying data source. Please note that this may not always be an HTTP URL in future. | ## Troubleshooting