0

Get file JSON for specific nodes

by
Published Apr 8, 2025

Returns the nodes referenced to by `ids` as a JSON object.

Script figma Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Figma = {
3
  token: string;
4
};
5
/**
6
 * Get file JSON for specific nodes
7
 * Returns the nodes referenced to by `ids` as a JSON object.
8
 */
9
export async function main(
10
  auth: Figma,
11
  file_key: string,
12
  ids: string | undefined,
13
  version: string | undefined,
14
  depth: string | undefined,
15
  geometry: string | undefined,
16
  plugin_data: string | undefined,
17
) {
18
  const url = new URL(`https://api.figma.com/v1/files/${file_key}/nodes`);
19
  for (const [k, v] of [
20
    ["ids", ids],
21
    ["version", version],
22
    ["depth", depth],
23
    ["geometry", geometry],
24
    ["plugin_data", plugin_data],
25
  ]) {
26
    if (v !== undefined && v !== "" && k !== undefined) {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "GET",
32
    headers: {
33
      Authorization: "Bearer " + auth.token,
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43