Docusign Python API – Send document to multiple recipients for signature

I had a lot of trouble sending documents via Docusign’s Python library, specially PDF’s. Python’s encoding doesn’t really help read the pdf data and send it across to the docusign’s API via http request. Then I chose the option to convert pdf data to base64 and use that under the document section to invoke the API. Here’s a sample code to make that happen:

# DocuSign - Embedded Sending to multiple recipients for signing
import sys, json, urllib, urllib2
import base64
 
# enter your info:
username = "yourlogin@org.com"
password = "yourP@ssword"
integratorKey = "abcd-1234567-xxxx-xxxx-xxxx-xxxxxxxxxx"
document_name = "document.pdf"

# enter recipient info:
email_1 = "signer_1@org.com"
name_1 = "Signer One"
anchor_string_1 = "signer_one"
 
email_2 = "signer_2@org.com"
name_2 = "Signer Two"
anchor_string_2 = "signer_two"

# build authentication string XML
authenticateStr = "<DocuSignCredentials>" \
                    "<Username>" + username + "</Usernamegt;" \
                    "<Password>" + password + "</Password>" \
                    "<IntegratorKey>" + integratorKey + "</IntegratorKey>" \
                    "</DocuSignCredentials>"

# Login
url = 'https://demo.docusign.net/restapi/v2/login_information';   
headers = {'X-DocuSign-Authentication': authenticateStr, 'Accept': 'application/json'}
req = urllib2.Request(url, headers=headers)
response = urllib2.urlopen(req)
json_response = response.read()

status = response.code
if (status != 200): 
    print("Error calling webservice, status is: %s" % status); sys.exit()
 
# get the baseUrl and accountId from the response body
data = json.loads(json_response)
loginInfo = data['loginAccounts']
D = loginInfo[0]
baseUrl = D['baseUrl']
accountId = D['accountId']

# open pdf document in binary read mode 
f = open(document_name, 'rb')
file_contents = f.read()
f.close()

b64_content = base64.b64encode(file_contents)

dict_body = {
    "emailBlurb" : "This comes from Python API",
    "emailSubject" : "DocuSign API - Please Sign This Document...",
    "documents" : [
        {
            "documentId" : "1",
            "documentBase64" : b64_content,
            "name" : document_name
        }
    ],
    "recipients" : {
        "signers" : [
            {
                "email" : email_1,
                "name" : name_1,
                "recipientId" : "1",
                "tabs" : {
                    "signHereTabs" : [
                        {
                            "anchorString" : anchor_string_1,
                            "anchorXOffset" : "60",
                            "anchorYOffset" : "10",
                            "anchorUnits" : "pixels",
                            "documentId" : "1",
                            "pageNumber" : "1"
                        }
                    ]
                }
            },
            {
                "email" : email_2,
                "name" : name_2,
                "recipientId" : "2",
                "tabs" : {
                    "signHereTabs" : [
                        {
                            "anchorString" : anchor_string_2,
                            "anchorXOffset" : "60",
                            "anchorYOffset" : "10",
                            "anchorUnits" : "pixels",
                            "documentId" : "1",
                            "pageNumber" : "1"
                        }
                    ]
                }
            }
        ]
    },
    "status" : "sent"
}

requestBody =  "\r\n" + \
                "\r\n" + \
                "--myboundary\r\n" + \
                "Content-Type: application/json\r\n" + \
                "Content-Disposition: form-data\r\n" + \
                "\r\n" + \
                json.dumps(dict_body)+"\r\n" + \
                "--myboundary--\r\n" + \
                "\r\n"

headers = {'Content-Type' : 'multipart/form-data;boundary=myboundary',
           'Accept' : 'application/json',
           'X-DocuSign-Authentication' : authenticateStr
           }
req = urllib2.Request(url=baseUrl+"/envelopes", data=requestBody, headers=headers)
response = urllib2.urlopen(req)
json_response = response.read()
print json_response
    
sys.exit()
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s