Arjun
Arjun

Reputation: 21

Node js , Docusign download signed document of the envelope

axios({
method: 'get',
url: `https://demo.docusign.net/restapi/v2/accounts/{AccountID}/envelopes/{ID}/documents/1`,
headers: {
    'X-DocuSign-Authentication': JSON.stringify({
        "Username": docusign.username,
        "Password": docusign.password,
        "IntegratorKey": docusign.integratorKey
    }),
    'Content-Type': 'application/json'
},
}).then(response =>  {
    fs.writeFileSync('./i.pdf', response.data, {encoding: 'binary'})
}).catch(console.log)

With the above code I am unable to save the proper file, Can you any help on this

Upvotes: 2

Views: 729

Answers (2)

Premji
Premji

Reputation: 87

Arjun I found a solution please check the below code.

let docuResponce = axios({
               method: 'get',
   url:`https://demo.docusign.net/restapi/v2/accounts/{AccountID}/envelopes/{ID}/documents/1`,
               headers: {
                        'X-DocuSign-Authentication': JSON.stringify({
                                 "Username": docusign.username,
                                  "Password": docusign.password,
                                  "IntegratorKey": docusign.integratorKey
                                }),
                        'Content-Type': 'application/pdf'
                      },
                  responseType: 'stream'
               });

    var fs = require('fs');
    var path = require('path');

    // file name
    var filename = envelopID + '_' + docusign.accountID + '.pdf';
    var tempFile = path.resolve(__dirname, filename); // file path

    // generate a file stream
    docu.data.pipe(fs.createWriteStream(tempFile));

    docu.data.on('end', () => {
        // file has been download
        res.status(200).json({ success: 'true', data: '' });
    });

    docu.data.on('error', (err) => {
        // error in download
        res.status(200).json({ success: 'false', data: '' });
    });

Upvotes: 1

Inbar Gazit
Inbar Gazit

Reputation: 14050

  1. You are using legacy authentication instead of using the more secure and modern oauth. I would suggest you change your code to use JWT grant.
  2. You are using v2.0 API. I would recommend you change to use API v2.1

Upvotes: 0

Related Questions