0

Create a prediction

by
Published Oct 17, 2025

Create a prediction for the model version and inputs you provide.

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
7
 * Create a prediction for the model version and inputs you provide.
8
 */
9
export async function main(
10
  auth: Replicate,
11
  body: {
12
    input: {};
13
    stream?: false | true;
14
    version: string;
15
    webhook?: string;
16
    webhook_events_filter?: "start" | "output" | "logs" | "completed"[];
17
  },
18
  Prefer?: string,
19
) {
20
  const url = new URL(`https://api.replicate.com/v1/predictions`);
21

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