use_figma
Run JavaScript against the Figma plugin API to create or edit designs in the file open in Figma Desktop, returning node details and console output.
Instructions
Create or edit designs in the file open in Figma Desktop by running JavaScript against the Figma plugin API. code is the body of an async function with figma in scope: use await, and return a value (nodes come back as {id, name, type, x, y, width, height}); console.log output is returned too. The whole run is one undo step. If the script throws, the changes it made before the error stay.
Rules of the plugin API here:
The file uses dynamic page loading: use figma.getNodeByIdAsync, figma.setCurrentPageAsync, await page.loadAsync() before reading another page, getMainComponentAsync, and the *Async style/variable getters.
Load every font before setting text or text properties: await figma.loadFontAsync({ family: "Inter", style: "Regular" }). Check figma.listAvailableFontsAsync() when unsure a font exists.
Colours are 0-1 RGB: fills = [{ type: "SOLID", color: { r: 1, g: 0.4, b: 0 } }]. Paint arrays are immutable: assign a new array.
Prefer auto layout: layoutMode, itemSpacing, padding*, primaryAxisAlignItems, counterAxisAlignItems; set layoutSizingHorizontal/Vertical ("FILL", "HUG", "FIXED") only after the node is appended to an auto-layout parent.
New nodes land on the current page: place top-level frames clear of existing ones, and append children with parent.appendChild(node).
Reuse what the file has: find its components, styles and variables with search_design_system first, and set bound variables with figma.variables.setBoundVariableForPaint.
Images from the web: const img = await figma.createImageAsync(url), then fills = [{ type: "IMAGE", imageHash: img.hash, scaleMode: "FILL" }]. fetch(url) works too (e.g. an SVG for figma.createNodeFromSvg). Local files go in with place_image.
figma.currentPage.selection = [node] and figma.viewport.scrollAndZoomIntoView([node]) show the result.
Build a screen in a few calls (structure, then sections), return the ids, and check each step with get_screenshot.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Body of an async function run with `figma` in scope. | |
| fileKey | No | The key of the Figma file. Accepted for compatibility; the file open in Figma Desktop is always used. | |
| timeoutMs | No | How long to wait for the script, in ms (default 60000). |