0
Create image
One script reply has been approved by the moderators Verified

Creates an image given a prompt.

Created by hugo697 136 days ago Viewed 3742 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 136 days ago
1
type Openai = {
2
  api_key: string;
3
  organization_id: string;
4
};
5
/**
6
 * Create image
7
 * Creates an image given a prompt.
8
 */
9
export async function main(
10
  auth: Openai,
11
  body: {
12
    prompt: string;
13
    model?: string | ("dall-e-2" | "dall-e-3");
14
    n?: number;
15
    quality?: "standard" | "hd";
16
    response_format?: "url" | "b64_json";
17
    size?: "256x256" | "512x512" | "1024x1024" | "1792x1024" | "1024x1792";
18
    style?: "vivid" | "natural";
19
    user?: string;
20
    [k: string]: unknown;
21
  }
22
) {
23
  const url = new URL(`https://api.openai.com/v1/images/generations`);
24

25
  const response = await fetch(url, {
26
    method: "POST",
27
    headers: {
28
      "OpenAI-Organization": auth.organization_id,
29
      "Content-Type": "application/json",
30
      Authorization: "Bearer " + auth.api_key,
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