0

Generate image from prompt

by
Published Apr 8, 2025
Script recraft Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Recraft = {
3
  apiKey: string;
4
};
5
/**
6
 * Generate image from prompt
7
 *
8
 */
9
export async function main(
10
  auth: Recraft,
11
  body: {
12
    block_nsfw?: false | true;
13
    calculate_features?: false | true;
14
    controls?: {
15
      artistic_level?: number;
16
      background_color?: { rgb?: number[]; std?: number[]; weight?: number };
17
      colors?: { rgb?: number[]; std?: number[]; weight?: number }[];
18
      no_text?: false | true;
19
    };
20
    model?:
21
      | "refm1"
22
      | "recraft20b"
23
      | "recraftv2"
24
      | "recraftv3"
25
      | "flux1_1pro"
26
      | "flux1dev";
27
    n?: number;
28
    prompt: string;
29
    random_seed?: number;
30
    response_format?: "url" | "b64_json";
31
    size?:
32
      | "1024x1024"
33
      | "1365x1024"
34
      | "1024x1365"
35
      | "1536x1024"
36
      | "1024x1536"
37
      | "1820x1024"
38
      | "1024x1820"
39
      | "1024x2048"
40
      | "2048x1024"
41
      | "1434x1024"
42
      | "1024x1434"
43
      | "1024x1280"
44
      | "1280x1024"
45
      | "1024x1707"
46
      | "1707x1024";
47
    style?:
48
      | "digital_illustration"
49
      | "icon"
50
      | "realistic_image"
51
      | "vector_illustration";
52
    style_id?: string;
53
    substyle?:
54
      | "2d_art_poster"
55
      | "3d"
56
      | "80s"
57
      | "glow"
58
      | "grain"
59
      | "hand_drawn"
60
      | "infantile_sketch"
61
      | "kawaii"
62
      | "pixel_art"
63
      | "psychedelic"
64
      | "seamless"
65
      | "voxel"
66
      | "watercolor"
67
      | "broken_line"
68
      | "colored_outline"
69
      | "colored_shapes"
70
      | "colored_shapes_gradient"
71
      | "doodle_fill"
72
      | "doodle_offset_fill"
73
      | "offset_fill"
74
      | "outline"
75
      | "outline_gradient"
76
      | "uneven_fill"
77
      | "70s"
78
      | "cartoon"
79
      | "doodle_line_art"
80
      | "engraving"
81
      | "flat_2"
82
      | "line_art"
83
      | "linocut"
84
      | "b_and_w"
85
      | "enterprise"
86
      | "hard_flash"
87
      | "hdr"
88
      | "motion_blur"
89
      | "natural_light"
90
      | "studio_portrait"
91
      | "line_circuit"
92
      | "2d_art_poster_2"
93
      | "engraving_color"
94
      | "flat_air_art"
95
      | "hand_drawn_outline"
96
      | "handmade_3d"
97
      | "stickers_drawings";
98
    text_layout?: { bbox: number[][]; text: string }[];
99
  },
100
) {
101
  const url = new URL(`https://external.api.recraft.ai/v1/images/generations`);
102

103
  const response = await fetch(url, {
104
    method: "POST",
105
    headers: {
106
      "Content-Type": "application/json",
107
      Authorization: "Bearer " + auth.apiKey,
108
    },
109
    body: JSON.stringify(body),
110
  });
111
  if (!response.ok) {
112
    const text = await response.text();
113
    throw new Error(`${response.status} ${text}`);
114
  }
115
  return await response.json();
116
}
117