0

Get file JSON

by
Published Apr 8, 2025

Returns the document identified by `file_key` as a JSON object. The file key can be parsed from any Figma file url: `https://www.figma.com/file/{file_key}/{title}`. The `document` property contains a node of type `DOCUMENT`. The `components` property contains a mapping from node IDs to component metadata. This is to help you determine which components each instance comes from.

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
7
 * Returns the document identified by `file_key` as a JSON object. The file key can be parsed from any Figma file url: `https://www.figma.com/file/{file_key}/{title}`.
8

9
The `document` property contains a node of type `DOCUMENT`.
10

11
The `components` property contains a mapping from node IDs to component metadata. This is to help you determine which components each instance comes from.
12
 */
13
export async function main(
14
  auth: Figma,
15
  file_key: string,
16
  version: string | undefined,
17
  ids: string | undefined,
18
  depth: string | undefined,
19
  geometry: string | undefined,
20
  plugin_data: string | undefined,
21
  branch_data: string | undefined,
22
) {
23
  const url = new URL(`https://api.figma.com/v1/files/${file_key}`);
24
  for (const [k, v] of [
25
    ["version", version],
26
    ["ids", ids],
27
    ["depth", depth],
28
    ["geometry", geometry],
29
    ["plugin_data", plugin_data],
30
    ["branch_data", branch_data],
31
  ]) {
32
    if (v !== undefined && v !== "" && k !== undefined) {
33
      url.searchParams.append(k, v);
34
    }
35
  }
36
  const response = await fetch(url, {
37
    method: "GET",
38
    headers: {
39
      Authorization: "Bearer " + auth.token,
40
    },
41
    body: undefined,
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49