Basic Python Samples
- 3 minutes to read
You can find the Basic Python Samples on GitHub:
https://github.com/aurigma/CCHSample-Basic-Python
In this repo, you will find several minimalistic examples that will allow you to try out the API in action. At the moment, the following tasks are demonstrated:
- Authentication and obtaining an OAuth2 token.
- Obtaining information necessary for starting the Design Editor via the API.
- Displaying the editor on the frontend and initializing it for two tasks:
- Starting an empty design of the specified dimensions blank-editor-save-file-sample.py.
- Loading the specified template open-template-in-editor-save-file-sample.py.
- Creating a project in Customer's Canvas and starting rendering (see Processing personalization results).
- Polling the project to see if rendering is complete to download the results (see Downloading artifacts for an order).
Technical details
The examples are coded in Python 3, and do not require the installation of any third-party packages. Packages available with Python 3 out of the box are used.
SimpleHTTPRequestHandler
is used to organize the web server.urllib3
is used as an HTTP client for making API calls.
To make it clear, no HTML template engines are used here, and the front-end code is generated directly in the script.
Each example is contained in its own file, and they are independent of each other. Files downloaded from Customer's Canvas are saved directly in the folder where you start the server.
Using this approach is not recommended in real applications. However, such simplicity is essential when experimenting with the API and creating a Proof of Concept (POC). You can simply edit one file, run it for execution (python3 <file name>
), and immediately see the results.
Getting started
Set up your Customer's Canvas tenant
Before you start, prepare the data you need to use the API. Specify this data in the configuration, as described below.
- Tenant ID. Log in to your Customer's Canvas tenant, navigate to Settings > Tenant, and copy the number from the ID field (see the Tenant topic).
- Client ID/Secret. Register your application using the Client Credentials authentication scheme in Settings > External Apps (see the External apps topic).
- Storefront ID. Set up Integration in your tenant, which will identify the site where the application will be launched, in Settings > Integrations (see the Integrations topic).
- API Gateway. Depending on your hosting environment, it's https://api.customerscanvashub.com/ or https://api.eu.customerscanvashub.com/ (see the API Gateway topic).
In addition, you will need to prepare test data, such as products, design templates, and mockups.
Clone repo
Clone the repository with the following command:
git clone https://github.com/aurigma/CCHSample-Basic-Python.git
Configure app
At the beginning of each sample, there is a configuration block like this:
######################
# CONFIGURATION #
######################
# Register your system in Customer's Canvas account as explained here:
# https://customerscanvas.com/dev/backoffice/storefront/creating-custom-integration.html
storefront_id = 4242 # Insert your Storefront ID here
# In this example, we are using OAuth2 auth scheme called Client Credentials. Create an External App
# in your Customer's Canvas account and add the Client ID and Secret here.
# See more details: https://customerscanvas.com/dev/backoffice/auth.html
client_id = 'Enter Client ID'
secret = 'Enter Secret Key'
Specify your details according to the comments.
Run
To run a sample:
python3 blank-editor-save-file-sample.py
or
python3 open-template-in-editor-save-file-sample.py
This command will start the HTTP server on port 8000. To see the results, open the following address in your browser: http://localhost:8000
.
If this port is already occupied, find this line at the end of the file and change the port number to the desired one:
httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler)
Each sample is described in detail by comments. Before making your changes, we recommend reading the code with comments from beginning to end. If you have any problems or questions, please create a ticket in the helpdesk.