Create a new design
By default, nothing happens when a user selects a product or template via the product catalog. This is because an integration needs to explicitly define the behavior.
This part of the tutorial explains how to:
    Detect when a user selects a product or template.
    Create and open a new design in the Canva editor.

Step 1: Detect when a user selects a product or template

After the initialize method, create a function named onProductSelect:
1
const onProductSelect = (opts) => {
2
console.log("Product selected.");
3
};
Copied!
Then pass this function into the showCatalog method:
1
api.showCatalog({
2
onProductSelect,
3
});
Copied!
This function runs when a user selects a product. It receives an opts object that contains information about the selected product, such as its dimensions and category.

Step 2: Create a new design in the Canva editor

In the onProductSelect function, call the createDesign method.
1
const onProductSelect = (opts) => {
2
api.createDesign({
3
...opts,
4
});
5
};
Copied!
The ...opts is an object spread operator that merges the opts object with any other properties. This enables the opts object from the onProductSelect function to be passed into the createDesign method. This ensures that the createDesign method can still access the relevant product and template data.
The createDesign method also requires an onBackClick callback function. This function runs when a user clicks the back button in the Canva editor's header. You should use this function to return the user to the previous screen.
For this tutorial, use the onBackClick function to log a message to the JavaScript Console:
1
const onBackClick = () => {
2
console.log("You clicked the 'Back' button in the Canva editor's header.");
3
};
4
5
const onProductSelect = (opts) => {
6
api.createDesign({
7
...opts,
8
onBackClick,
9
});
10
};
Copied!

Example

src/views/index.ejs

1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="UTF-8" />
5
<meta
6
name="viewport"
7
content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover"
8
/>
9
<title>Print Partnership Example</title>
10
<script src="https://sdk.canva.com/partnership.js"></script>
11
<style type="text/css">
12
body,
13
html {
14
margin: 0;
15
}
16
17
#container {
18
height: 100vh;
19
}
20
</style>
21
</head>
22
<body>
23
<div id="container"></div>
24
<script type="text/javascript">
25
(async () => {
26
const api = await Canva.Partnership.initialize({
27
apiKey: "<%= partnerApiKey %>",
28
container: document.getElementById("container"),
29
autoAuthToken: "<%= autoAuthToken %>",
30
});
31
32
const onBackClick = () => {
33
console.log(
34
"You clicked the 'Back' button in the Canva editor's header."
35
);
36
};
37
38
const onProductSelect = (opts) => {
39
api.createDesign({
40
...opts,
41
onBackClick,
42
});
43
};
44
45
api.showCatalog({
46
onProductSelect,
47
});
48
})();
49
</script>
50
</body>
51
</html>
Copied!
Last modified 3mo ago