Editing designs
Edit an existing design with the JavaScript API.
After a user creates a design, they may want to edit that design at a later point in time. To enable this, Canva provides your website with the ID of the user's design. You can use this ID to re-open a design in the Canva editor.
This part of the tutorial series explains how to open an existing design in the Canva editor.

Step 1: Get the ID of the user's design

To get the ID of the user's design, register callbacks with the onDesignOpen and onDesignPublish methods:
1
api.createDesign({
2
design: {
3
type: "Poster",
4
},
5
onDesignOpen: (opts) => {
6
console.log(opts.designId);
7
},
8
onDesignPublish: (opts) => {
9
console.log(opts.designId);
10
},
11
});
Copied!
The onDesignOpen callback runs when the user opens a design via the Canva Button. The onDesignPublish callback runs when the user publishes a design via the Canva editor. Both of these callbacks receive an opts.designId argument that contains the ID of the user's design.
When your website receives the ID of a user's design, it can save the ID for later use, but it doesn't particularly matter how the website saves the ID. Some common patterns are persisting the ID to a database or storing it locally via localStorage.

Step 2: Open an existing design in the Canva editor

To open an existing design in the Canva editor, call the editDesign method:
1
api.editDesign({
2
design: {
3
id: "DESIGN ID GOES HERE",
4
},
5
});
Copied!
At a minimum, the editDesign method expects an opts.design.id argument. This argument must contain the ID of a design that Canva can open in the editor.
The editDesign method accepts many of the same arguments as the createDesign method, but the arguments are not identical. The main difference is that the editDesign method doesn't accept the opts.design.type argument. This is because the design type of an existing design can't be changed.
You can pass the onDesignOpen and onDesignPublish methods into the createDesign and editDesign methods.

Example

1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="UTF-8" />
5
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
<title>Canva Button</title>
7
<script src="https://sdk.canva.com/designbutton/v2/api.js"></script>
8
</head>
9
<body>
10
<button id="create-design">Create design</button>
11
<button id="edit-design" disabled="true">Edit design</button>
12
<button id="download-design" disabled="true">Download design</button>
13
14
<script type="text/javascript">
15
(async () => {
16
if (!window.Canva || !window.Canva.DesignButton) {
17
return;
18
}
19
20
const api = await window.Canva.DesignButton.initialize({
21
apiKey: "API KEY GOES HERE",
22
});
23
24
let previousDesignId;
25
26
const createDesignButton = document.querySelector("#create-design");
27
const editDesignButton = document.querySelector("#edit-design");
28
const downloadDesignButton = document.querySelector("#download-design");
29
30
const handleDesignOpen = (opts) => {
31
previousDesignId = opts.designId;
32
editDesignButton.disabled = false;
33
};
34
35
const handleDesignPublish = (opts) => {
36
previousDesignId = opts.designId;
37
downloadDesignButton.disabled = false;
38
downloadDesignButton.onclick = () => {
39
window.location.href = opts.exportUrl;
40
};
41
};
42
43
createDesignButton.onclick = () => {
44
api.createDesign({
45
design: {
46
type: "Poster",
47
},
48
onDesignOpen: handleDesignOpen,
49
onDesignPublish: handleDesignPublish,
50
});
51
};
52
53
editDesignButton.onclick = () => {
54
api.editDesign({
55
design: {
56
id: previousDesignId,
57
},
58
onDesignOpen: handleDesignOpen,
59
onDesignPublish: handleDesignPublish,
60
});
61
};
62
})();
63
</script>
64
</body>
65
</html>
Copied!
Last modified 4mo ago