0

Create a prediction using an official model

by
Published Oct 17, 2025

Create a prediction using an [official model](https://replicate.

Script replicate Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Replicate = {
3
  token: string;
4
};
5
/**
6
 * Create a prediction using an official model
7
 * Create a prediction using an [official model](https://replicate.
8
 */
9
export async function main(
10
  auth: Replicate,
11
  model_owner: string,
12
  model_name: string,
13
  body: {
14
    input: {};
15
    stream?: false | true;
16
    webhook?: string;
17
    webhook_events_filter?: "start" | "output" | "logs" | "completed"[];
18
  },
19
  Prefer?: string,
20
) {
21
  const url = new URL(
22
    `https://api.replicate.com/v1/models/${model_owner}/${model_name}/predictions`,
23
  );
24

25
  const response = await fetch(url, {
26
    method: "POST",
27
    headers: {
28
      ...(Prefer ? { Prefer: Prefer } : {}),
29
      "Content-Type": "application/json",
30
      Authorization: "Bearer " + auth.token,
31
    },
32
    body: JSON.stringify(body),
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.json();
39
}
40