Figma REST API Client

2.1.2-beta · active · verified Tue Apr 21

The `figma-api` package provides a thin, typed JavaScript/TypeScript wrapper around the official Figma REST API. Currently in its 2.1.2-beta version, this library offers a client-side implementation designed for both browser and Node.js environments. It leverages Promises via Axios for HTTP requests and is fully typed with TypeScript, aligning its API methods directly with the official Figma REST API specifications. A significant rewrite occurred in version 2.x, standardizing endpoint method names and argument structures to precisely match Figma's documentation, ensuring future consistency. The package differentiates itself by closely mirroring the official API spec, providing a robust, type-safe interface for interacting with Figma files and resources programmatically, making it easier for developers to build integrations without constantly referring to external documentation for method signatures.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize the Figma API client with a personal access token, fetch a specific Figma file by its key, and then log some basic information about the retrieved file structure, handling potential errors.

import * as Figma from 'figma-api';

const personalAccessToken = process.env.FIGMA_PERSONAL_ACCESS_TOKEN ?? '';
const fileKey = process.env.FIGMA_FILE_KEY ?? '';

if (!personalAccessToken || !fileKey) {
  console.error('Please set FIGMA_PERSONAL_ACCESS_TOKEN and FIGMA_FILE_KEY environment variables.');
  process.exit(1);
}

export async function getFigmaFile() {
    try {
        console.log('Initializing Figma API client...');
        const api = new Figma.Api({
            personalAccessToken: personalAccessToken,
        });

        console.log(`Fetching file with key: ${fileKey}...`);
        // The getFile method in v2.x expects an object with parameters
        const file = await api.getFile({ file_key: fileKey });

        console.log('Successfully fetched Figma file structure.');
        // Accessing basic file data
        console.log(`File Name: ${file.name}`);
        console.log(`Last modified: ${file.lastModified}`);
        console.log(`Number of top-level children: ${file.document?.children?.length ?? 0}`);
        
        return file;
    } catch (error) {
        console.error('Error fetching Figma file:', error.message);
        if (error.response) {
          console.error('Response data:', error.response.data);
          console.error('Response status:', error.response.status);
        }
        throw error;
    }
}

// Execute the function when the script runs
getFigmaFile().catch(err => {
  console.error('Figma API operation failed:', err);
});

view raw JSON →