0

Calculating API Cost

by
Published Apr 8, 2025

This endpoint returns the cost used for generating images using a particular service type.

Script leonardoai Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Leonardoai = {
3
  apiKey: string;
4
};
5
/**
6
 * Calculating API Cost
7
 * This endpoint returns the cost used for generating images using a particular service type.
8
 */
9
export async function main(
10
  auth: Leonardoai,
11
  body: {
12
    service?:
13
      | "IMAGE_GENERATION"
14
      | "FANTASY_AVATAR_GENERATION"
15
      | "MOTION_GENERATION"
16
      | "LCM_GENERATION"
17
      | "MODEL_TRAINING"
18
      | "TEXTURE_GENERATION"
19
      | "UNIVERSAL_UPSCALER"
20
      | "UNIVERSAL_UPSCALER_ULTRA";
21
    serviceParams?: {
22
      IMAGE_GENERATION?: {
23
        imageHeight?: number;
24
        imageWidth?: number;
25
        numImages?: number;
26
        inferenceSteps?: number;
27
        promptMagic?: false | true;
28
        promptMagicStrength?: number;
29
        promptMagicVersion?: string;
30
        alchemyMode?: false | true;
31
        photoRealMode?: false | true;
32
        photoRealStrength?: number;
33
        photoRealVersion?: string;
34
        highResolution?: false | true;
35
        loraCount?: number;
36
        isModelCustom?: false | true;
37
        controlnetsCost?: number;
38
        isPhoenix?: false | true;
39
        isSDXL?: false | true;
40
        isSDXLLightning?: false | true;
41
        ultra?: false | true;
42
      };
43
      FANTASY_AVATAR_GENERATION?: {
44
        imageHeight?: number;
45
        imageWidth?: number;
46
        numImages?: number;
47
      };
48
      MOTION_GENERATION?: { durationSeconds?: number };
49
      LCM_GENERATION?: {
50
        height?: number;
51
        width?: number;
52
        instantRefine?: false | true;
53
        refine?: false | true;
54
      };
55
      MODEL_TRAINING?: { resolution?: number };
56
      TEXTURE_GENERATION?: { preview?: false | true };
57
      UNIVERSAL_UPSCALER?: { megapixel?: number };
58
      UNIVERSAL_UPSCALER_ULTRA?: {
59
        inputWidth?: number;
60
        inputHeight?: number;
61
        upscaleMultiplier?: number;
62
      };
63
    };
64
  },
65
) {
66
  const url = new URL(
67
    `https://cloud.leonardo.ai/api/rest/v1/pricing-calculator`,
68
  );
69

70
  const response = await fetch(url, {
71
    method: "POST",
72
    headers: {
73
      "Content-Type": "application/json",
74
      Authorization: "Bearer " + auth.apiKey,
75
    },
76
    body: JSON.stringify(body),
77
  });
78
  if (!response.ok) {
79
    const text = await response.text();
80
    throw new Error(`${response.status} ${text}`);
81
  }
82
  return await response.json();
83
}
84