Last active
September 14, 2023 17:04
-
-
Save renatodeleao/a1f9e6880e9b0f7630fe38d689a04953 to your computer and use it in GitHub Desktop.
Figma API — Get an svg node (via /images endpoint)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ACCESS_TOKEN = "token"; | |
const FILE_ID = "fileId"; | |
const NODE_ID = "Check the url for something like => 3%3A2"; | |
const container = document.getElementById("figma-container") | |
function apiRequest(url) { | |
return fetch(url, { | |
method: 'GET', | |
headers: { "x-figma-token": ACCESS_TOKEN } | |
}).then(function(response) { | |
return response.json(); | |
}).catch(function (error) { | |
return { err: error }; | |
}); | |
} | |
apiRequest(`https://api.figma.com/v1/images/${FILE_ID}?ids=${NODE_ID}&format=svg`) | |
.then(({ imagesMap }) => { | |
// returns an object with node id as key | |
// and value of an s3 link to the svg file | |
const svgUrl = imagesMap[decodeURIComponent(NODE_ID)][0]; | |
fetch(svgUrl).then(res => { | |
return res.text(); | |
}).then(svg => { | |
container.innerHTML = svg; | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice, thank you. But is it possible to rely on some stable name such as "logo.svg" to use it without checking the NODE_ID every time manually?