Execute AI model

This endpoint provides users with the capability to run specific AI models on-demand. By submitting the required input data, users can receive real-time predictions or results generated by the chosen AI model. The endpoint supports various AI model types, ensuring flexibility and adaptability for diverse use cases.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Execute AI model
8
 * This endpoint provides users with the capability to run specific AI models on-demand. 
9
        
10
By submitting the required input data, users can receive real-time predictions or results generated by the chosen AI 
11
model. The endpoint supports various AI model types, ensuring flexibility and adaptability for diverse use cases.
12
 */
13
export async function main(
14
  auth: Cloudflare,
15
  account_identifier: string,
16
  model_name: string,
17
  body: { [k: string]: unknown }
18
) {
19
  const url = new URL(
20
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/ai/run/${model_name}`
21
  );
22

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