0

Create Style

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
 * Create Style
7
 *
8
 */
9
export async function main(
10
  auth: Recraft,
11
  body: {
12
    images: string[];
13
    private?: false | true;
14
    style:
15
      | "digital_illustration"
16
      | "icon"
17
      | "realistic_image"
18
      | "vector_illustration";
19
  },
20
) {
21
  const url = new URL(`https://external.api.recraft.ai/v1/styles`);
22

23
  const formData = new FormData();
24
  for (const [k, v] of Object.entries(body)) {
25
    if (v !== undefined) {
26
      formData.append(k, String(v));
27
    }
28
  }
29
  const response = await fetch(url, {
30
    method: "POST",
31
    headers: {
32
      Authorization: "Bearer " + auth.apiKey,
33
    },
34
    body: formData,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42