0

Create/modify/delete variables

by
Published Apr 8, 2025

**This API is available to full members of Enterprise orgs with Editor seats.

Script figma Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Figma = {
3
  token: string;
4
};
5
/**
6
 * Create/modify/delete variables
7
 * **This API is available to full members of Enterprise orgs with Editor seats.
8
 */
9
export async function main(
10
  auth: Figma,
11
  file_key: string,
12
  body: {
13
    variableCollections?:
14
      | {
15
          action: "CREATE";
16
          id?: string;
17
          name: string;
18
          initialModeId?: string;
19
          hiddenFromPublishing?: false | true;
20
        }
21
      | {
22
          action: "UPDATE";
23
          id: string;
24
          name?: string;
25
          hiddenFromPublishing?: false | true;
26
        }
27
      | { action: "DELETE"; id: string }[];
28
    variableModes?:
29
      | {
30
          action: "CREATE";
31
          id?: string;
32
          name: string;
33
          variableCollectionId: string;
34
        }
35
      | {
36
          action: "UPDATE";
37
          id: string;
38
          name?: string;
39
          variableCollectionId: string;
40
        }
41
      | { action: "DELETE"; id: string }[];
42
    variables?:
43
      | {
44
          action: "CREATE";
45
          id?: string;
46
          name: string;
47
          variableCollectionId: string;
48
          resolvedType: "BOOLEAN" | "FLOAT" | "STRING" | "COLOR";
49
          description?: string;
50
          hiddenFromPublishing?: false | true;
51
          scopes?:
52
            | "ALL_SCOPES"
53
            | "TEXT_CONTENT"
54
            | "CORNER_RADIUS"
55
            | "WIDTH_HEIGHT"
56
            | "GAP"
57
            | "ALL_FILLS"
58
            | "FRAME_FILL"
59
            | "SHAPE_FILL"
60
            | "TEXT_FILL"
61
            | "STROKE_COLOR"
62
            | "STROKE_FLOAT"
63
            | "EFFECT_FLOAT"
64
            | "EFFECT_COLOR"
65
            | "OPACITY"
66
            | "FONT_FAMILY"
67
            | "FONT_STYLE"
68
            | "FONT_WEIGHT"
69
            | "FONT_SIZE"
70
            | "LINE_HEIGHT"
71
            | "LETTER_SPACING"
72
            | "PARAGRAPH_SPACING"
73
            | "PARAGRAPH_INDENT"[];
74
          codeSyntax?: { WEB?: string; ANDROID?: string; iOS?: string };
75
        }
76
      | {
77
          action: "UPDATE";
78
          id: string;
79
          name?: string;
80
          description?: string;
81
          hiddenFromPublishing?: false | true;
82
          scopes?:
83
            | "ALL_SCOPES"
84
            | "TEXT_CONTENT"
85
            | "CORNER_RADIUS"
86
            | "WIDTH_HEIGHT"
87
            | "GAP"
88
            | "ALL_FILLS"
89
            | "FRAME_FILL"
90
            | "SHAPE_FILL"
91
            | "TEXT_FILL"
92
            | "STROKE_COLOR"
93
            | "STROKE_FLOAT"
94
            | "EFFECT_FLOAT"
95
            | "EFFECT_COLOR"
96
            | "OPACITY"
97
            | "FONT_FAMILY"
98
            | "FONT_STYLE"
99
            | "FONT_WEIGHT"
100
            | "FONT_SIZE"
101
            | "LINE_HEIGHT"
102
            | "LETTER_SPACING"
103
            | "PARAGRAPH_SPACING"
104
            | "PARAGRAPH_INDENT"[];
105
          codeSyntax?: { WEB?: string; ANDROID?: string; iOS?: string };
106
        }
107
      | { action: "DELETE"; id: string }[];
108
    variableModeValues?: {
109
      variableId: string;
110
      modeId: string;
111
      value:
112
        | string
113
        | number
114
        | false
115
        | true
116
        | { r: number; g: number; b: number }
117
        | { r: number; g: number; b: number; a: number }
118
        | { type: "VARIABLE_ALIAS"; id: string };
119
    }[];
120
  },
121
) {
122
  const url = new URL(`https://api.figma.com/v1/files/${file_key}/variables`);
123

124
  const response = await fetch(url, {
125
    method: "POST",
126
    headers: {
127
      "Content-Type": "application/json",
128
      Authorization: "Bearer " + auth.token,
129
    },
130
    body: JSON.stringify(body),
131
  });
132
  if (!response.ok) {
133
    const text = await response.text();
134
    throw new Error(`${response.status} ${text}`);
135
  }
136
  return await response.json();
137
}
138