Skip to content

Instantly share code, notes, and snippets.

@renatodeleao
Last active September 14, 2023 17:04
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatodeleao/a1f9e6880e9b0f7630fe38d689a04953 to your computer and use it in GitHub Desktop.
Save renatodeleao/a1f9e6880e9b0f7630fe38d689a04953 to your computer and use it in GitHub Desktop.
Figma API — Get an svg node (via /images endpoint)
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;
})
});
@zhibirc
Copy link

zhibirc commented Jan 5, 2021

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment