0

Render images of file nodes

by
Published Apr 8, 2025

Renders images from a file.

Script figma Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Figma = {
3
  token: string;
4
};
5
/**
6
 * Render images of file nodes
7
 * Renders images from a file.
8
 */
9
export async function main(
10
  auth: Figma,
11
  file_key: string,
12
  ids: string | undefined,
13
  version: string | undefined,
14
  scale: string | undefined,
15
  format: "jpg" | "png" | "svg" | "pdf" | undefined,
16
  svg_outline_text: string | undefined,
17
  svg_include_id: string | undefined,
18
  svg_include_node_id: string | undefined,
19
  svg_simplify_stroke: string | undefined,
20
  contents_only: string | undefined,
21
  use_absolute_bounds: string | undefined,
22
) {
23
  const url = new URL(`https://api.figma.com/v1/images/${file_key}`);
24
  for (const [k, v] of [
25
    ["ids", ids],
26
    ["version", version],
27
    ["scale", scale],
28
    ["format", format],
29
    ["svg_outline_text", svg_outline_text],
30
    ["svg_include_id", svg_include_id],
31
    ["svg_include_node_id", svg_include_node_id],
32
    ["svg_simplify_stroke", svg_simplify_stroke],
33
    ["contents_only", contents_only],
34
    ["use_absolute_bounds", use_absolute_bounds],
35
  ]) {
36
    if (v !== undefined && v !== "" && k !== undefined) {
37
      url.searchParams.append(k, v);
38
    }
39
  }
40
  const response = await fetch(url, {
41
    method: "GET",
42
    headers: {
43
      Authorization: "Bearer " + auth.token,
44
    },
45
    body: undefined,
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53