Articles in this section
Category / Section

How to fetch template details and create a new template using the BoldSign API with Python?

Published:
2 mins read

In BoldSign, you can retrieve properties from an existing template and reuse them to create a new template with identical configurations.
To achieve this in your codebase, you need to:

  • Send a GET request to the /template/properties endpoint to fetch the details of an existing template.
  • Then, use a POST request to the /template/create endpoint to generate a new template using those fetched properties.

Below is a sample Python code that demonstrates this process:

Python

import requests
import base64

api_url = "https://api.boldsign.com/v1/template/create"
api_key = "Your API Key"

properties_url = "https://api.boldsign.com/v1/template/properties?templateId=Your template Id"

HEADERS = {
    "accept": "application/json",
    "X-API-KEY": api_key,
    "Content-Type": "application/json" 
}

properties_response = requests.get(properties_url, headers=HEADERS)
template_properties = properties_response.json()

roles = []
for original_role in template_properties.get("roles", []):
    form_fields = [
        {
            "id": x["id"],
            "fieldType": "TextBox" if x["fieldType"] in ["name", "email"] else x["fieldType"],
            "isReadOnly": x["fieldType"] in ["name", "email"] or x["isReadOnly"],
            "bounds": x["bounds"],
            "value": x["value"],
            "pageNumber": x["pageNumber"]
        }
        for x in original_role.get("formFields", [])
    ]

    temp_role = {
        "index": original_role["index"],
        "signerOrder": original_role["signerOrder"],
        "signerType": original_role["signerType"],
        "defaultSignerEmail": original_role.get("defaultSignerEmail"),
        "defaultSignerName": original_role.get("defaultSignerName"),
        "name": original_role["name"],
        "formFields": form_fields
    }
    roles.append(temp_role)

def file_to_base64(file_path):
    with open(file_path, "rb") as file:
        base64_encoded = base64.b64encode(file.read()).decode('utf-8')
    return f"data:application/pdf;base64,{base64_encoded}"

file_path = "Your File Path.pdf"  
base64_string = file_to_base64(file_path)


payload = {
    "Title": "Sample Template", 
    "DocumentMessage": template_properties.get("documentMessage", ""),
    "Description": template_properties.get("description", ""),
    "DocumentTitle": template_properties.get("documentTitle", ""),
    "Roles": roles, 
    "Files" : [base64_string]
}

response = requests.post(api_url, headers=HEADERS, json=payload)

print(response.text)

In the example code above, replace Your API Key, Your template Id, and Your File Path with your actual details. Once executed, a new template will be created in your BoldSign account using the properties fetched from the existing template.

Was this article useful?
Like
Dislike
Help us improve this page
Please provide feedback or comments
Access denied
Access denied