PowerBuilder
PowerBuilder
DocuSign: Requesting a Signature via Email (Remote Signing)
See more DocuSign Examples
This code example demonstrates the simplest and quickest workflow for requesting a signature for a document via email. The email will contain a signing link the recipient can use to electronically sign a document from their mobile or desktop computer.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_PdfData
oleobject loo_Json
oleobject loo_JsonToken
oleobject loo_SbAuth
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
string ls_EnvelopeId
string ls_Uri
string ls_StatusDateTime
string ls_Status
li_Success = 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
// Implements the following CURL command:
// curl --request POST https://demo.docusign.net/restapi/v2.1/accounts/${accountId}/envelopes \
// --header "Authorization: Bearer ${accessToken}" \
// --header "Content-Type: application/json" \
// --data '{
// "emailSubject": "Please sign this document",
// "documents": [
// {
// "documentBase64": "JVBERi0xLjMKMyAwIG9iag ... dGFydHhyZWYKNjk5CiUlRU9GCg==",
// "name": "Lorem Ipsum",
// "fileExtension": "pdf",
// "documentId": "1"
// }
// ],
// "recipients": {
// "signers": [
// {
// "email": "joe_sample@example.com",
// "name": "Joe Sample",
// "recipientId": "1",
// "routingOrder": "1",
// "tabs": {
// "signHereTabs": [
// {
// "documentId": "1", "pageNumber": "1",
// "recipientId": "1", "tabLabel": "SignHereTab",
// "xPosition": "195", "yPosition": "147"
// }
// ]
// }
// }
// ]
// },
// "status": "sent"
// }'
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "emailSubject": "Please sign this document",
// "documents": [
// {
// "documentBase64": "JVBERi0xLjMKMyAwIG9iag ... dGFydHhyZWYKNjk5CiUlRU9GCg==",
// "name": "Lorem Ipsum",
// "fileExtension": "pdf",
// "documentId": "1"
// }
// ],
// "recipients": {
// "signers": [
// {
// "email": "joe_sample@example.com",
// "name": "Joe Sample",
// "recipientId": "1",
// "routingOrder": "1",
// "tabs": {
// "signHereTabs": [
// {
// "documentId": "1",
// "pageNumber": "1",
// "recipientId": "1",
// "tabLabel": "SignHereTab",
// "xPosition": "195",
// "yPosition": "147"
// }
// ]
// }
// }
// ]
// },
// "status": "sent"
// }
// Load a PDF to be signed.
loo_PdfData = create oleobject
li_rc = loo_PdfData.ConnectToNewObject("Chilkat.BinData")
li_Success = loo_PdfData.LoadFile("qa_data/pdf/helloWorld.pdf")
if li_Success = 0 then
Write-Debug "Failed to load local PDF file."
destroy loo_Http
destroy loo_PdfData
return
end if
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.UpdateString("emailSubject","Please sign this document")
loo_Json.UpdateString("documents[0].documentBase64",loo_PdfData.GetEncoded("base64"))
loo_Json.UpdateString("documents[0].name","Lorem Ipsum")
loo_Json.UpdateString("documents[0].fileExtension","pdf")
loo_Json.UpdateString("documents[0].documentId","1")
loo_Json.UpdateString("recipients.signers[0].email","joe_sample@example.com")
loo_Json.UpdateString("recipients.signers[0].name","Joe Sample")
loo_Json.UpdateString("recipients.signers[0].recipientId","1")
loo_Json.UpdateString("recipients.signers[0].routingOrder","1")
loo_Json.UpdateString("recipients.signers[0].tabs.signHereTabs[0].documentId","1")
loo_Json.UpdateString("recipients.signers[0].tabs.signHereTabs[0].pageNumber","1")
loo_Json.UpdateString("recipients.signers[0].tabs.signHereTabs[0].recipientId","1")
loo_Json.UpdateString("recipients.signers[0].tabs.signHereTabs[0].tabLabel","SignHereTab")
loo_Json.UpdateString("recipients.signers[0].tabs.signHereTabs[0].xPosition","195")
loo_Json.UpdateString("recipients.signers[0].tabs.signHereTabs[0].yPosition","147")
loo_Json.UpdateString("status","sent")
// Get our previously obtained OAuth2 access token, which should contain JSON like this:
// {
// "access_token": "eyJ0eXA....YQyig",
// "token_type": "Bearer",
// "refresh_token": "eyJ0eXA....auE3eHKg",
// "expires_in": 28800
// }
loo_JsonToken = create oleobject
li_rc = loo_JsonToken.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_JsonToken.LoadFile("qa_data/tokens/docusign.json")
loo_SbAuth = create oleobject
li_rc = loo_SbAuth.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbAuth.Append("Bearer ")
loo_SbAuth.Append(loo_JsonToken.StringOf("access_token"))
loo_Http.SetRequestHeader("Authorization",loo_SbAuth.GetAsString())
loo_Http.SetRequestHeader("Content-Type","application/json")
// Don't forget to modify this line to use your account ID
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
li_Success = loo_Http.HttpJson("POST","https://demo.docusign.net/restapi/v2.1/accounts/${accountId}/envelopes",loo_Json,"application/json",loo_Resp)
if li_Success = 0 then
Write-Debug loo_Http.LastErrorText
destroy loo_Http
destroy loo_PdfData
destroy loo_Json
destroy loo_JsonToken
destroy loo_SbAuth
destroy loo_Resp
return
end if
loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")
loo_Resp.GetBodySb(loo_SbResponseBody)
loo_JResp = create oleobject
li_rc = loo_JResp.ConnectToNewObject("Chilkat.JsonObject")
loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0
Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()
li_RespStatusCode = loo_Resp.StatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
Write-Debug "Response Header:"
Write-Debug loo_Resp.Header
Write-Debug "Failed."
destroy loo_Http
destroy loo_PdfData
destroy loo_Json
destroy loo_JsonToken
destroy loo_SbAuth
destroy loo_Resp
destroy loo_SbResponseBody
destroy loo_JResp
return
end if
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "envelopeId": "d51cfdab-22ed-4832-bf68-446c44077ffc",
// "uri": "/envelopes/d51cfdab-22ed-4832-bf68-446c44077ffc",
// "statusDateTime": "2018-04-17T16:31:51.8830000Z",
// "status": "sent"
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
ls_EnvelopeId = loo_JResp.StringOf("envelopeId")
ls_Uri = loo_JResp.StringOf("uri")
ls_StatusDateTime = loo_JResp.StringOf("statusDateTime")
ls_Status = loo_JResp.StringOf("status")
destroy loo_Http
destroy loo_PdfData
destroy loo_Json
destroy loo_JsonToken
destroy loo_SbAuth
destroy loo_Resp
destroy loo_SbResponseBody
destroy loo_JResp