Every time you call saveProduct or finishProductDesign, Customer's Canvas creates a state file representing all objects loaded in the editor. By default, these state files are saved in the ..\userdata\<someUserId>\states\
folder for a single user and they are kept forever until you delete these files manually. Customer's Canvas does not automatically clean them up. At the same time, you can use the Web API to manipulate state files in your application: list available state files, delete state files, reload images in state files from their original sources, and more.
This API works based on HTTPS requests and is handled by the StateFiles controller with the following methods:
Function | Request type | URL |
---|---|---|
Get a list of user's state files | GET |
~/api/users/{userId}/states?page=number
|
Check if a state file is available | HEAD |
~/api/users/{userId}/states/{stateId}
|
Upload a state file | POST |
~/api/users/{userId}/states/{stateId}
|
Download a user's state file | GET |
~/api/users/{userId}/states/{stateId}
|
Delete a user's state file | DELETE |
~/api/users/{userId}/states/{stateId}
|
Copy and clone a state file | POST |
~/api/users/{userId}/states/{stateId}
|
Modify a state file | PATCH |
~/api/users/{userId}/states/{stateId}
|
Get product tags from a state file | GET |
~/api/users/{userId}/states/{stateId}/tags
|
Get variable items from a state file | GET |
~/api/users/{userId}/states/{stateId}/variables
|
Get the entire object model from a state file | GET |
~/api/users/{userId}/states/{stateId}/model
|
The StateFiles controller requires the following parameters:
You can also pass the following optional parameters when getting a list of state files:
null
.null
.false
, outputs only state IDs and timestamps. The default value is true
.When you pass both page and items, then the response headers will contain the number of TotalPages and TotalItems.
This controller can return the following errors:
Status Code: 403 Forbidden Content: HTTPS Required
Status Code: 403 Forbidden Content: Invalid Security Key
Status Code: 404 Not Found Content: The specified state file cannot be found.
Status Code: 400 Bad Request Content: This state already exists.
Status Code: 500 Internal Server Error Content: The remote server returned an error: (404) Not Found.
It is recommended to download a code sample written in ECMAScript 2015 that demonstrates how you can work with the API and refer to this sample when reading this topic.
The security model of Customer's Canvas requires you to pass X-CustomersCanvasAPIKey in request headers. The snippets below define the API security key in JavaScript code. It could be highly insecure if it runs on a public site. However, you can use it this way in your admin panel, or just for demonstration purposes.
To get a list of state files, you can make the following request. If a user has many state files, it will display only the first 10 of them.
const userId = "default"; const url = `https://localhost:44300/api/users/${userId}/states?page=1&items=10`; fetch(url).then(response => { return response.json(); }).then(json => { for (let i = 0; i < json.length; i++) { const s = json[i]; console.log(s.stateId); } });
For example, you will get the following response to this request when the default user has a state file.
Status Code: 200 OK TotalPages: 1 TotalItems: 1 Content: [{ "stateId":"0777bb82-6f89-4e72-93b1-aad1b875602b", "dateModified":"5/26/2018 8:29", "surfaceCount":2, "tag":null }]
To check if a user's folder contains a state file, you can make the following request.
var userId = "JohnWood"; var stateId = "1f190673-226d-4c31-87b4-0140805cc445"; var url = `https://localhost:44300/api/users/${userId}/states/${stateId}`; fetch(url, { method: "HEAD", headers: { "X-CustomersCanvasAPIKey": "UniqueSecurityKey" } }).then(async function (response) { if (response.ok) { console.log("OK"); } else { console.log("Not Found"); } });
When the state file is in the user's folder, this controller returns:
Status Code: 200 OK
This method accepts a state file in the request payload and uploads this file to the ..\userdata\{userId}\states\
folder. When you pass the stateId parameter in your request, this identifier is assigned to the uploaded state file. When you omit stateId, this controller generates a new unique identifier and returns it in the response.
const userId = "JohnWood"; var formData = new FormData(document.querySelector("#state_to_upload")) fetch("https://localhost:44300/api/users/${userId}/states/", { method: "POST", headers: { "X-CustomersCanvasAPIKey": "UniqueSecurityKey" }, body: formData }) .then( response => { console.log(response); } );
When the state file has been successfully uploaded, this controller returns an ID of this state:
Status Code: 200 OK Content: "d1ba8901-5884-4a2c-8e28-1463b6862fa9"
const userId = "JohnWood"; const stateId = "1f190673-226d-4c31-87b4-0140805cc445"; const url = `https://localhost:44300/api/users/${userId}/states/${stateId}`; logElement.textContent = "init request"; fetch(url, { method: "GET", headers: { "X-CustomersCanvasAPIKey": "UniqueSecurityKey", 'Accept': 'application/json', "Content-Type": "application/json" } }) .then( response => { if (response.ok) { console.log("The state file was downloaded successfully."); console.log(response.url); } else console.error("Failed to download the state file."); }, e => console.error("Failed to download the state file.") );
If the result is successful, this API returns a file stream:
Status Code: 200 OK Content-type: application/octet-stream
To delete a saved product from the userdata
folder, you can make the following request.
const userId = "JohnWood"; const stateId = "1f190673-226d-4c31-87b4-0140805cc445"; const url = `https://localhost:44300/api/users/${userId}/states/${stateId}`; fetch(url, { method: "DELETE", headers: { "X-CustomersCanvasAPIKey": "UniqueSecurityKey", "Content-Type": "application/json" } }) .then( response => { return response.json(); }, e => console.log("Failed to delete the state file.") ) .then(json => { if (json === true) console.log("The state file was deleted successfully."); else if (json === false) console.log("Requested state file is not found."); });
If the result is successful, this API can return the following:
Status Code: 200 OK Content: false
Status Code: 200 OK Content: true
To copy state files, you need to pass the following parameters in the request body:
const parameters = { "CopyFrom": { "UserId": "sourceUserId", "StateId": "sourceStateId" }, "Overwrite": true };
Note that userId and stateId in the request URL are the destination identifiers.
const userId = "JohnWood"; const stateId = "Invitation"; const sourceUserId = "masteruser"; const sourceStateId = "1f190673-226d-4c31-87b4-0140805cc445"; const url = `https://localhost:44300/api/users/${userId}/states/${stateId}`; const parameters = { "CopyFrom": { "UserId": sourceUserId, "StateId": sourceStateId }, "Overwrite": true }; fetch(url, { method: "POST", headers: { "X-CustomersCanvasAPIKey": "UniqueSecurityKey", "Accept": "application/json", "Content-Type": "application/json" }, body: JSON.stringify(parameters) }) .then( response => { if (response.ok) console.log("The state file was copied."); else console.log("Failed to copy the state file."); } );
When a state file has been successfully copied, this controller returns:
Status Code: 204 No Content
The StateFiles controller allows you to modify state files. Depending on the type parameter, you can either update images obtained from external sources through direct URLs, replace Depositphotos previews with purchased images, specify product tags, or save personalization data.
This PATCH request expects the type parameter in the request body. You can define one of the following values:
After your end users have added images through the Asset Manager, the original files may be changed. If the application requires those changes to be applied to saved state files, you can use this Web API method. This method will trigger Customer's Canvas to download new versions of these images and will replace them in the state file. Reloading images invalidates preview and hi-res links in the cache, thus they are recreated based on the new versions of these images when the application requests them next time.
If the original image has been edited so that its dimensions have changed, then the new image is arbitrarily resized to fit into the old bounds when reloading this image. If any of the original images are deleted, the controller returns the 500 Server error and does not reload images.
const userId = "JohnWood"; const stateId = "1f190673-226d-4c31-87b4-0140805cc445"; const url = `https://localhost:44300/api/users/${userId}/states/${stateId}`; fetch(url, { method: "PATCH", headers: { "X-CustomersCanvasAPIKey": "UniqueSecurityKey", "Content-Type": "application/json" }, body: JSON.stringify({ type: "MemorySourceReloadAll" }) }) .then( response => { if (response.ok) console.log("Images were reloaded successfully."); else console.log("Failed to reload images."); } );
You can set product tags as follows:
const userId = "JohnWood"; const stateId = "1f190673-226d-4c31-87b4-0140805cc445"; const url = `https://localhost:44300/api/users/${userId}/states/${stateId}`; const data = {"product": "postcard", "design": "flowers", "colorTheme": "red"}; fetch(url, { method: "PATCH", headers: { "X-CustomersCanvasAPIKey": "UniqueSecurityKey", "Content-Type": "application/json" }, body: JSON.stringify({ type: "PatchProductTags", data: data }) });
You may want to save variable data in product tags to implement personalized rendering. In this case, you can organize them in an array so that each element represents a data set to render an output image. The data set, in turn, is an array of objects defining how a single field should appear in the hi-res output. For example, you can define the data to render two product copies with predefined values of the Name, Photo, and Barcode elements as follows:
const data = [ [ { name: "Name", value: "Neo", type: "InString" }, { name: "Photo", value: "https://example.com/Neo.jpg", type: "ImagePlaceholder" }, { name: "Barcode", value: "1234567", type: "BarcodePlaceholder", barcodeFormat: "EAN-8" } ], [ { name: "Name", value: "Trinity", type: "InString" }, { name: "Photo", value: "https://example.com/Trinity.jpg", type: "ImagePlaceholder" }, { name: "Barcode", value: "7654321", type: "BarcodePlaceholder", barcodeFormat: "EAN-8" } ] ];
Such an object may define the following properties:
You can also refer to the following example to find out how to save the personalization data through the VdpData request.
The following example illustrates how you can save itemsData to a state file.
const userId = "JohnWood"; const stateId = "1f190673-226d-4c31-87b4-0140805cc445"; const url = `https://localhost:44300/api/users/${userId}/states/${stateId}`; const parameters = { "type": "VdpData", "itemsData": { "Name": { text: "Christopher", font: { fauxItalic: true } } } }; fetch(url, { method: "PATCH", headers: { "X-CustomersCanvasAPIKey": "UniqueSecurityKey", "Content-Type": "application/json" }, body: JSON.stringify(parameters) });
In the request body, you must pass commands - an array with the parameters of the required patches.
The following example illustrates how you can both personalize a state file and specify product tags in a composite request.
const userId = "JohnWood"; const stateId = "c9846515-98ce-4f5a-b0b7-f585be3faf3f"; const url = `https://localhost:44300/api/users/${userId}/states/${stateId}`; const parameters = { "type": "Composite", "commands": [ { "type": "VdpData", "dataSet": { "surfacesData": [ { "surfaceBinding": { "surfaceIndexes": [ 0 ] }, "data": [ { "Name": { "text": "John Wood" } }, { "Name": { "text": "Cristopher Bennet" } } ] } ] }, "itemsData": null }, { "type": "PatchProductTags", "data": { "product": "invitation", "design": "BBQ" } } ] }; fetch(url, { method: "PATCH", headers: { "X-CustomersCanvasAPIKey": "UniqueSecurityKey", "Content-Type": "application/json" }, body: JSON.stringify(parameters) });
When a state file has been successfully patched, this controller returns:
Status Code: 204 No Content
To retrieve product tags, you can make the following request.
const userId = "JohnWood"; const stateId = "1f190673-226d-4c31-87b4-0140805cc445"; const url = `https://localhost:44300/api/users/${userId}/states/${stateId}/tags`; console.log(await (await fetch(url, { method: "GET", headers: { "X-CustomersCanvasAPIKey": "UniqueSecurityKey", "Content-Type": "application/json" } }).json));
For example, you can get the following response.
Status Code: 200 OK Content: { "product": "postcard", "design": "flowers", "colorTheme": "red" }
To retrieve variable items, you can make the following request.
const userId = "JohnWood"; const stateId = "1f190673-226d-4c31-87b4-0140805cc445"; const url = `https://localhost:44300/api/users/${userId}/states/${stateId}/variables`; (async() => { const response = await fetch(url, { method: "GET", headers: { "X-CustomersCanvasAPIKey": "UniqueSecurityKey", "Content-Type": "application/json" } }); console.log(await response.json()); })()
For example, you can get the following response if a product contains three variable items: a text, an in-string placeholder, and an image.
Status Code: 200 OK Content: [ { "name": "Sale", "value": "BBQ burger", "type": "Text" }, { "name": "Promo", "value": "2 For $5.99 Each", "type": "InString" }, { "name": "Photo", "value": null, "type": "Image" } ]
To retrieve a list of design items, you can make the following request.
const userId = "JohnWood"; const stateId = "1f190673-226d-4c31-87b4-0140805cc445"; const url = `https://localhost:44300/api/users/${userId}/states/${stateId}/model`; (async () => { const response = await fetch(url, { method: "GET", headers: { "X-CustomersCanvasAPIKey": "UniqueSecurityKey", "Content-Type": "application/json" } }); const model = await response.json(); if (response.status === 200) { // Output the object model. console.log(model); // Output the name and type of design items on the first page. model.surfaces[0].containers.find(c => c.name === "Main").items.forEach(item => { console.log("Name: " + item.name, " Type: " + item.$type); }); } else if (response.status === 500) // Exception details. console.log(result.details); })()
If the state file contains only two items, this example may return the following list.
Name: design.pdf Type: ImageItem Name: caption Type: PlainTextItem
Status Code: 200 OK Content: { defaultCropMarks: null, defaultDesignLocation: {isEmpty: true, x: 0, y: 0}, defaultSafetyLines: null, id: "c1295e78-c3a1-47d0-b80c-d870d1f0e69f", name: null, surfaces: [ {width: 161.517532, height: 161.517532, rotateAngle: 0, tags: {...}, size: "161.517532, 161.517532", ...} ], tags: {printColorSpace: "Rgb", userId: "default"}, version: "5.32.100", watermarkConfig: {text: {...}, image: null, visibility: {...}} }