Created
July 18, 2022 08:59
-
-
Save tankxu/5c5d59ddc00f8784946b86c313d92f76 to your computer and use it in GitHub Desktop.
Copy Figma team's projects and files to a new team
This file contains 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
// Please input the srouceTeam id, targetTeam id, token, cookie | |
// Create a node project and install node-fetch | |
// Add `"type": "module",` to package.json | |
import fetch from "node-fetch"; | |
// Id can be copied from team url | |
let sourceTeam = { | |
name: "source", | |
id: "", | |
}; | |
let targetTeam = { | |
name: "target", | |
id: "", | |
}; | |
// Token can be obtained from https://www.figma.com/developers/api#authentication | |
let token = ""; | |
// Cookie can be copied from the figma web app or the native app | |
// Open the browser's inspect tool and switch to the Network tab | |
// Select a Figma API request and copy the cookie from the Headers tab | |
let cookie =""; | |
// List projects | |
function getProjects(teamId) { | |
return fetch(`https://api.figma.com/v1/teams/${teamId}/projects`, { | |
headers: { | |
"X-Figma-Token": token, | |
}, | |
}) | |
.then((res) => res.json()) | |
.then((data) => data.projects) | |
.catch((err) => console.log(err)); | |
} | |
// List files | |
function getFiles(projectId) { | |
return fetch(`https://api.figma.com/v1/projects/${projectId}/files`, { | |
headers: { | |
"X-Figma-Token": token, | |
}, | |
}) | |
.then((res) => res.json()) | |
.then((data) => data.files) | |
.catch((err) => console.log(err)); | |
} | |
// Create project in target team and return project id | |
function createProject(teamId, projectName) { | |
return fetch("https://www.figma.com/api/folders", { | |
headers: { | |
"content-type": "application/json", | |
cookie: cookie, | |
Referer: `https://www.figma.com/files/team/${teamId}`, | |
}, | |
body: `{"team_id":${teamId},"path":"${projectName}","is_invite_only":false,"is_view_only":false}`, | |
method: "POST", | |
}) | |
.then((res) => res.json()) | |
.then((data) => data.meta[0].id) | |
.catch((err) => console.log(err)); | |
} | |
// Copy file to target project and return new file id | |
function copyFile(fileKey, projectId) { | |
return fetch( | |
`https://www.figma.com/api/multiplayer/${fileKey}/copy?folder_id=${projectId}`, | |
{ | |
headers: { | |
"content-type": "application/json", | |
cookie: cookie, | |
}, | |
method: "POST", | |
} | |
) | |
.then((res) => res.json()) | |
.then((data) => data.meta.key) | |
.catch((err) => console.log(err)); | |
} | |
// Rename file | |
function renameFile(projectId, fileKey, newName) { | |
return fetch(`https://www.figma.com/api/files/${fileKey}`, { | |
headers: { | |
"content-type": "application/json", | |
cookie: cookie, | |
Referer: `https://www.figma.com/files/project/${projectId}`, | |
}, | |
body: `{"key":"${fileKey}","name":"${newName}"}`, | |
method: "PUT", | |
}) | |
.then((res) => res.json()) | |
.then((data) => { | |
// console.log(data); | |
console.log(`${newName} copied to new team`); | |
}) | |
.catch((err) => console.log(err)); | |
} | |
// Main function | |
// Get projects -> create project in target team & get files -> copy file -> rename file | |
getProjects(sourceTeam.id).then((projects) => { | |
projects.map((project) => { | |
let newProjectId; | |
// Create project in target team | |
createProject(targetTeam.id, project.name) | |
.then((projectId) => { | |
newProjectId = projectId; | |
}) | |
.then(() => { | |
// Get files from source project | |
getFiles(project.id).then((files) => { | |
files.map((file) => { | |
// Copy file to target project | |
copyFile(file.key, newProjectId).then((fileKey) => { | |
// Rename file | |
renameFile(newProjectId, fileKey, file.name); | |
}); | |
}); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment