Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulvramesh/09f3afc7db57299d72fee474e43e076b to your computer and use it in GitHub Desktop.
Save rahulvramesh/09f3afc7db57299d72fee474e43e076b to your computer and use it in GitHub Desktop.
Generates JSON from Figma file
import request from 'request'
const api_endpoint = 'https://api.figma.com/v1'
const personal_access_token = 'FIGMA_ACCESS_TOKEN_HERE' // https://www.figma.com/developers/docs#auth-dev-token
function downloadSvgFromAWS(url) {
return new Promise((resolve, reject) => {
request.get(
url,
{
headers: {
'Content-Type': 'images/svg+xml',
},
},
function(error, response, body) {
if (error) {
reject(error)
} else {
resolve(body)
}
}
)
})
}
function getComponentsFromChildren(children) {
const components = []
const check = c => {
if (c.type == 'COMPONENT') {
components.push(c)
} else if (c.children) {
c.children.forEach(check)
}
}
children.forEach(check)
return components
}
function getImageUrls(file_key, componentIds) {
return new Promise((resolve, reject) => {
request.get(
`${api_endpoint}/images/${file_key}`,
{
headers: {
'Content-Type': 'application/json',
'x-figma-token': personal_access_token,
},
qs: {
ids: componentIds,
format: 'svg',
},
json: true,
},
function(error, response, body) {
if (error) {
reject(error)
} else {
resolve(body.images)
}
}
)
})
}
function getJSONFromFigmaFile(file_key) {
return new Promise((resolve, reject) => {
request.get(
`${api_endpoint}/files/${file_key}`,
{
headers: {
'Content-Type': 'application/json',
'x-figma-token': personal_access_token,
},
},
function(error, response, body) {
if (error) {
reject(error)
} else {
const components = getComponentsFromChildren(
JSON.parse(body).document.children
)
console.log(components)
getImageUrls(file_key, components.map(c => c.id).join(',')).then(
images => {
const imagesArray = Object.keys(images).map(key => images[key])
downloadSvgFromAWS(imagesArray[0]).then(image =>
console.log(image)
)
resolve(imagesArray)
}
)
}
}
)
})
}
export default getJSONFromFigmaFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment