How Do I Create a New Version of a Proof Using API

This article provides an example of creating a new version of a proof using Ashore’s API.

Below is an example in Python that retrieves a proof ID, creates a new version of the proof, and then uploads a local file and/or file from a remote url.

Code Example: https://pastebin.com/DiNH8R8L

import os
import requests
import json
from dotenv import load_dotenv
 
load_dotenv()
AccessToken = os.getenv('ACCESSTOKEN')
 
 
# Function to get the latest proof version ID
def get_proof(proof_id):
    url = f"https://api.ashoreapp.com/proof/{proof_id}/0?loadAllVersions=false"
    headers = {
        'accept': 'application/json',
        'accesstoken': AccessToken,
        'content-type': 'application/json'
    }
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        data = response.json()
        return data
    else:
        raise Exception("Failed to get proof version ID: Status Code {}".format(response.status_code))
 
 
# Function to upload a new version of the document
def create_new_proof_version(proof_id, current_proof_stage_id=0):
    url = "https://api.ashoreapp.com/proof/new-version"
    headers = {
        'accept': 'application/json',
        'accesstoken': AccessToken,
        'content-type': 'application/json'
    }
    payload = {
        "senderUserId": 2,
        "proofId": proof_id,
        "proofVersionFileIdsToPromoteToTheNextVersion": [],
        "proofName": "test",
        "subjectLine": "You Have A Proof To Review",
        "message": "<p>Please Review This Proof At Your Convenience</p>",
        "templateId": 0,
        "sendToWorkflowStageId": current_proof_stage_id,
        "sendOptions": 0  # SendToAll
    }
    # ATTN: sendToWorkflowStageId defaults to current stage (stages are attached to proof versions leaving this blank
    # will put the file on the new version) so you don't need to get the proof object first in order to upload files
    # to to the current stage
    # ATTN: SendOptions enumeration explains the behavior of sendOptions field:
    # 0 - SendToAll: Send to all approvers and reviewers.
    # 1 - SendToUnApprovedWithReviewers: Send only to unapproved approvers with reviewers.
    # 2 - SendToNone: Do not send to any approvers or reviewers.
    # 3 - Manual: Manually specify approvers and reviewers to send to.
    # 1 - SendToUnApprovedWithoutReviewers: Send only to unapproved approvers without reviewers.
    response = requests.post(url, headers=headers, data=json.dumps(payload))
    if response.status_code == 200:
        print("New proof version created successfully.")
        return response.json()
    else:
        raise Exception("Failed to create a new proof version: Status Code {}".format(response.status_code))
 
 
# Function to upload a file to the specified proof version
def upload_file(proof_id, proof_version_id, file_path):
    url = f"https://api.ashoreapp.com/proof/{proof_id}/proof-version/{proof_version_id}/file"
    headers = {
        'accept': 'application/json',
        'accesstoken': AccessToken
    }
    with open(file_path, 'rb') as file:
        files = {'file': (os.path.basename(file_path), file, 'image/png')}
        response = requests.put(url, headers=headers, files=files)
        if response.status_code == 200:
            print("File uploaded successfully.")
        else:
            raise Exception("Failed to upload file: Status Code {}".format(response.status_code))
 
 
# Function to upload a file from a URL to the specified proof version
def upload_file_from_url(proof_id, proof_version_id, file_name, file_url):
    url = f"https://api.ashoreapp.com/proof/{proof_id}/proof-version/{proof_version_id}/file-from-url"
    headers = {
        'accept': 'application/json',
        'accesstoken': AccessToken,
        'content-type': 'application/json'
    }
    payload = {
        'fileName': file_name,
        'fileUrl': file_url
    }
    response = requests.post(url, headers=headers, data=json.dumps(payload))
    if response.status_code == 200:
        print("File from URL uploaded successfully.")
    else:
        raise Exception("Failed to upload file from URL: Status Code {}".format(response.status_code))
 
 
# Main execution
if __name__ == "__main__":
    try:
        proofId = 197656
        proof = get_proof(proofId)
        proofVersion = create_new_proof_version(proofId, proof['currentWorkflowStageId'])
        upload_file(197656, proofVersion['id'], './ashore-logo.png')
        upload_file_from_url(197656, proofVersion['id'], 'ashore-logo.png',
                             'https://ashoreapp.com/wp-content/uploads/2023/07/ashore-logo.png')
 
    except Exception as e:
        print(str(e))