Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can you provide example code for common use cases for the Figma REST API


Can you provide example code for common use cases for the Figma REST API


Here are some common use cases and example code for the Figma REST API:

Fetch a Figma file's metadata

javascript
const axios = require('axios');

const config = {
  headers: {
    'X-FIGMA-TOKEN': 'YOUR_ACCESS_TOKEN'
  }
};

axios.get('https://api.figma.com/v1/files/FILE_KEY', config)
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

This code uses Axios to fetch the metadata of a Figma file specified by `FILE_KEY`, using the provided `ACCESS_TOKEN`[1].

Fetch a list of images from a Figma file

javascript
const axios = require('axios');

const config = {
  headers: {
    'X-FIGMA-TOKEN': 'YOUR_ACCESS_TOKEN'
  }
};

axios.get(`https://api.figma.com/v1/files/FILE_KEY/images`, config)
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

This code fetches a list of images from a Figma file specified by `FILE_KEY`, using the provided `ACCESS_TOKEN`[1].

Export an image from a Figma file

javascript
const axios = require('axios');

const config = {
  headers: {
    'X-FIGMA-TOKEN': 'YOUR_ACCESS_TOKEN'
  }
};

const data = {
  "format": "PNG",
  "scale": 2,
  "ids": "NODE_ID"
};

axios.post(`https://api.figma.com/v1/images/FILE_KEY`, data, config)
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

This code exports an image from a Figma file specified by `FILE_KEY` for the node with `NODE_ID`, using the provided `ACCESS_TOKEN`. The image is exported as a PNG at 2x scale[1].

Fetch a list of comments from a Figma file

javascript
const axios = require('axios');

const config = {
  headers: {
    'X-FIGMA-TOKEN': 'YOUR_ACCESS_TOKEN'
  }
};

axios.get(`https://api.figma.com/v1/files/FILE_KEY/comments`, config)
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

This code fetches a list of comments from a Figma file specified by `FILE_KEY`, using the provided `ACCESS_TOKEN`[1].

These examples demonstrate how to use the Figma REST API to fetch file metadata, images, export images, and fetch comments. The `ACCESS_TOKEN` and `FILE_KEY` need to be replaced with your actual values.

Citations:
[1] https://www.figma.com/developers/api
[2] https://www.figma.com/developers/compare-apis
[3] https://forum.figma.com/t/figma-api-questions/2016
[4] https://www.figma.com/plugin-docs/async-tasks/
[5] https://stackoverflow.com/questions/76826875/nodes-from-figma-rest-api-dont-have-visible-property