Creating a template of e-sign document

Before you start

In order to create a template of e-sign document with embedded signature, the file must comply with the following requirements:

  1. The file must be either in PDF or in one of the following file types that will be converted to PDF by the notary service:

    • .doc/.docx

    • .xls/.xlsx

    • .ppt/.pptx

    • .txt

  2. Maximum file size limit is 32 MB.

  3. The file must not be protected by password.

  4. If the uploaded file is in PDF, “Changing the Document” and “Commenting” permissions must be set to “Allowed”.

Step-by-step procedure

  1. Start the Python shell and configure its session.

    The following variables should be available now:

    >>> base_url  # the base URL of the API
    'https://eu2-cloud.acronis.com/api/notary/v2'
    >>> auth  # the 'Authorization' header value with the access token
    {'Authorization': 'Bearer 8770b34b74f9e4d9424eff50c38182bb4ae7f5596582ae61900b1b6a23e3ec58'}
    
  2. Define a variable named template_data, and then assign an object with a name that will be used for created e-sign document template to this variable:

    >>> template_data = {
    ...     'name': 'Example Contract Template'
    ... }
    
  3. [Optional] Add PDF annotations to the template:

    1. Follow the Generating PDF annotations procedure to fetch the list of PDF annotations.

    2. Add the annotations key with the list of JSON objects containing PDF annotations:

      >>> template_data['annotations'] = [
      ...     '{\"bbox\":[129.81423950195312,216.01904296875,200,50], ...}',
      ...     '{\"bbox\":[186.60797119140625,409.7261962890625,200,50], ...}'
      ... ]
      

    Note

    Annotations can be added later by following the Adding PDF annotations to existing template procedure.

  4. Define a variable named file_path, and then assign the path to your file to this variable:

    >>> file_path = '<path to file>'
    

    Important

    If the path contains backslashes, remember to escape them with an additional backslash.

  5. Open the file for reading in binary mode:

    >>> file = open(file_path, 'rb')
    
  6. Define a variable named files, and then assign an object containing the file in the file key to this variable:

    >>> files = {
    ...     'file': file
    ... }
    
  7. Upload the file to the notary storage to create a template of e-sign document for the file by sending a POST request to the /templates endpoint:

    >>> response = requests.post(f'{base_url}/templates', headers=auth, files=files, data=template_data)
    
  8. Close the file:

    >>> file.close()
    
  9. Check the status code of the response:

    >>> response.status_code
    201
    

    Status code 201 means that the notary service has received the file, saved it to the storage, increased the usage of Document templates and Notary storage quotas and created an e-sign document template for the file.

    A different status code means that an error has occurred. For the details, refer to “Status and error codes”.

    Also, the response body contains an object containing the information about the created e-sign document template formatted as a JSON text. When converted to an object, it will look as follows:

    >>> pprint.pprint(response.json())
    {'template': {'created_at': '2021-04-15T07:09:29.148799071Z',
                'document_count': 0,
                'file_id': '99fb3a13-2545-4dd0-bb67-5c5b19c77b9e',
                'file_name': 'file_name.pdf',
                'id': '3922edc2-d70d-4d5d-9789-5d739a6e7bdc',
                'name': 'Example Contract Template',
                'updated_at': '2021-04-15T07:09:29.148799071Z'}}
    

Full code example

 1#!/usr/bin/env python3
 2
 3import requests  # Will be used for sending requests to the API.
 4import hashlib   # Will be used for calculating hash values.
 5import os.path   # Will be used for path-related operations.
 6import pprint    # Will be used for formatting the output of JSON objects received in API responses.
 7import json      # Will be used for converting dictionaries into JSON text
 8
 9# Define variables named "LOGIN" and "PASSWORD" and then assign them with your account credentials
10LOGIN = '<your login>'        # Change login here
11PASSWORD = '<your password>'  # Change password here
12
13# Define a variable named "cloud_url" and then assign it with the URL of the cloud platform
14cloud_url = 'https://cloud.acronis.com'
15
16# Fetch the URL of the data center where your account is located by sending a GET request to the "/api/1/accounts" endpoint
17response = requests.get(
18    f'{cloud_url}/api/1/accounts',
19    params={'login': LOGIN}
20)
21response.raise_for_status()
22
23# Convert the JSON text that the response body contains to a dictionary and store the data center URL
24# in a variable that will be used in further requests
25server_url = response.json()['server_url']
26
27# Define a variable named "account_creds", and then assign the username and password to this variable
28account_creds = {
29    'username': LOGIN,
30    'password': PASSWORD
31}
32
33# Generate a token by sending a POST request to the "/api/2/idp/token" with your account credentials to the cloud platform
34response = requests.post(
35    f'{server_url}/api/2/idp/token',
36    headers={'Content-Type': 'application/x-www-form-urlencoded'},
37    data={'grant_type': 'password', **account_creds}
38)
39response.raise_for_status()
40
41# Convert the JSON text that the response body contains to a dictionary and then assign it to a variable named "token_info"
42token_info = response.json()
43
44# Define a variable named "auth" and then assign it with a dictionary with "Authorization" key containing
45# token string formatted as "Bearer <access_token>"
46auth = {
47    'Authorization': 'Bearer ' + token_info['access_token']
48}
49
50# Define a variable named "base_url", and then assign the API base URL using the data center URL
51# to this variable
52base_url = f'{server_url}/api/notary/v2'
53
54# Define a variable named "template_data", and then assign an object with the name of the template to this variable
55template_data = {
56    'name': 'Example Contract Template'
57}
58
59# [Optional] Add the ``annotations`` key with the list of JSON objects containing PDF annotations.
60template_data['annotations'] = [
61    '{\"bbox\":[129.81423950195312,216.01904296875,200,50], ...}',
62    '{\"bbox\":[186.60797119140625,409.7261962890625,200,50], ...}'
63]
64
65# Define a variable named "file_path" and then assign it with the path to your file
66file_path = '<path to file>'  # Change path to file here
67
68# Open the file for reading in binary mode
69file = open(file_path, 'rb')
70
71# Define a variable named "files" and then assign it with the dictionary with "file" key containing a file
72files = {
73    'file': file
74}
75
76# Send the file to the notary service by sending a POST request to the "/templates" endpoint
77response = requests.post(
78    f'{base_url}/templates',
79    headers=auth,
80    files=files,
81    data=template_data
82)
83response.raise_for_status()
84
85# Close the file
86file.close()