1
votes

Does anyone have any success adding documents to a draft envelope following the examples in the REST API?

  1. Add Documents to a Draft Envelope - I can't get this to work at all.
  2. Add a Document to a Draft Envelope - I have partial success. On the web console, I can see the document is added to the envelope, but when trying to open the document, the document shows "Unable to load PDF". The link to the document looks like https://demo.docusign.net/Member/noname.pdf?d=00000000-0000-0000-0000-000000000000&...

The PUT request that I used for #2:

X-DocuSign-Authentication: {"SendOnBehalfOf": "xxx", "Username":"xxx","Password":"xxx","IntegratorKey":"xxx"} Accept: application/json Content-Type: application/pdf Content-Disposition: file; filename="api_upload.pdf"; documentId=3; fileExtension="pdf" Content-Transfer-Encoding: base64

[base64 encoded bytes removed]

Any suggestions will be appreciated.

1
So what error message(s) are you receiving?Ergin

1 Answers

0
votes

I just tested and was able to add two documents to a draft envelope with the following PHP code. If you enter your credentials this code will Login, Create a Draft Envelope, then add 2 documents to that draft envelope, all in one go.

To run this code just:

  1. Save file locally.
  2. Enter credentials at top.
  3. Copy two pdfs to same directory, name them document1.pdf and document2.pdf.
  4. Run the code.

Note: The REST API documentation for this call has some inaccuracies, the below request body works so follow this instead...

<?php

// Input your info here:
$integratorKey = '...';
$email = '...';
$password = '...';
$name = 'John Doe';

// construct the authentication header:
$header = "<DocuSignCredentials><Username>" . $email . "</Username><Password>" . $password . "</Password><IntegratorKey>" . $integratorKey . "</IntegratorKey></DocuSignCredentials>";

/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 1 - Login (to retrieve baseUrl and accountId)
/////////////////////////////////////////////////////////////////////////////////////////////////
$url = "https://demo.docusign.net/restapi/v2/login_information";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-DocuSign-Authentication: $header"));

$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status != 200 ) {
    echo "error calling webservice, status is:" . $status;
    exit(-1);
}

$response = json_decode($json_response, true);
$accountId = $response["loginAccounts"][0]["accountId"];
$baseUrl = $response["loginAccounts"][0]["baseUrl"];
curl_close($curl);

//--- display results
echo "\naccountId = " . $accountId . "\nbaseUrl = " . $baseUrl . "\n";

/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 2 - Create a draft envelope
/////////////////////////////////////////////////////////////////////////////////////////////////
$data = array (
    "emailBlurb" => "This comes from PHP",
    "emailSubject" => "DocuSign API Testing",
    "status" => "created");   
$data_string = json_encode($data);  

// *** append "/envelopes" to baseUrl and as signature request endpoint
$curl = curl_init($baseUrl . "/envelopes" );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',
    "X-DocuSign-Authentication: $header" )                                                                       
);

$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
    echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
    print_r($json_response); echo "\n";
    return;
}

$response = json_decode($json_response, true);
$envelopeId = $response["envelopeId"];

//--- display results
echo "Document is created! Envelope ID = " . $envelopeId . "\n\n"; 

/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 3 - Add documents to draft envelope
/////////////////////////////////////////////////////////////////////////////////////////////////
$data = array (
    "documents" => array( 
        array( "documentId" => "1", "name" => "document1.pdf", "order" => "1" ),
        array( "documentId" => "2", "name" => "document2.pdf", "order" => "2" )
    ));  
$data_string = json_encode($data);  

$file_contents1 = file_get_contents("document1.pdf");
$file_contents2 = file_get_contents("document2.pdf");

$requestBody = "\r\n"
."\r\n"
."--myboundary\r\n"
."Content-Type: application/json\r\n"
."Content-Disposition: form-data\r\n"
."\r\n"
."$data_string\r\n"
."--myboundary\r\n"
."Content-Type:application/pdf\r\n"
."Content-Disposition: file; filename=\"document1.pdf\"; documentid=1 \r\n"
."\r\n"
."$file_contents1\r\n"
."--myboundary\r\n"
."Content-Type:application/pdf\r\n"
."Content-Disposition: file; filename=\"document2.pdf\"; documentid=2 \r\n"
."\r\n"
."$file_contents2\r\n"
."--myboundary--\r\n"
."\r\n";

// *** append "/envelopes/{envelopeId}/documents" to baseUrl and as endpoint
$url = $baseUrl . "/envelopes/$envelopeId/documents";
$curl = curl_init( $url );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);                                                                  
curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: multipart/form-data;boundary=myboundary',
    'Content-Length: ' . strlen($requestBody),
    "X-DocuSign-Authentication: $header" )                                                                       
);

$json_response = curl_exec($curl);
$response = json_decode($json_response, true);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
    echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
    print_r($response); echo "\n";
    exit(-1);
}

$response = json_decode($json_response, true);
$envelopeId = $response["envelopeId"];

//--- display results
echo "Document is sent! Envelope ID = " . $envelopeId . "\n\n"; 
?>

One last thing I'll note, is make sure you are doing a PUT request for this call and not a POST, as that's a common mistake I see here.