Download Document From DocuSign

In the previous article, we see how to get document data from DocuSign. In this article, we see how to download the document using API calls.

Get Started:

Step 1:

Create a DocumentRequest and StreamObject class.

public class DocumentRequest
{
    public string AccessToken { get; set; }

    public string EnvelopId { get; set; }
}
public class StreamObject
{
    public Stream FileStream { get; set; }

    public string MimeType { get; set; }

    public string DocumentName { get; set; }
}

Step 2:

Define a method in Interface.

Task<StreamObject> DownloadDocument(DocumentRequest documentRequest);

Step 3:

Add method GetApiClient and Download in DocuSignService class.

private async Task<ApiClient> GetApiClient(string token)
{
     var docuSignConfig = new Configuration(_configuration["DocuSign:BaseURI"] + "/restapi");
     docuSignConfig.AddDefaultHeader("Authorization", "Bearer " + token);
     docuSignConfig.AccessToken = token;
     var apiClient = new ApiClient(docuSignConfig);
     apiClient.SetBasePath(docuSignConfig.BasePath);
     return await Task.FromResult(apiClient);
}

public async Task<StreamObject> Download(ApiClient client, string envelopId, EnvelopeDocument docItem)
{
    string documentId = "1";
    EnvelopesApi envelopesApi = new EnvelopesApi(client);

    Stream results = envelopesApi.GetDocument(_configuration["DocuSign:AccountId"], envelopId, documentId);

    string docName = docItem.Name;
    bool hasPDFsuffix = docName.ToUpper().EndsWith(".PDF");

    string docType = docItem.Type;
    if (("content".Equals(docType) || "summary".Equals(docType)) && !hasPDFsuffix)
    {
        docName += ".pdf";
    }

    var response = new StreamObject()
    {
        FileStream = results,
        MimeType = "application/pdf",
        DocumentName = docName
    };
    return await Task.FromResult(response);

}

Step 4:

Add GetDocumentData method in DocuSignService class.

public async Task<Envelope> GetDocumentData(GetDocumentDataRequest request)
{
    Envelope envelopeData = new Envelope();

    if (request.EnvelopId != string.Empty && request.AccessToken != string.Empty)
    {
        var client = await GetApiClient(request.AccessToken);
        var envelopesApi = new EnvelopesApi(client);

        Envelope envelope = envelopesApi.GetEnvelope(_configuration["DocuSign:AccountId"], request.EnvelopId);
        EnvelopeFormData results = envelopesApi.GetFormData(_configuration["DocuSign:AccountId"], request.EnvelopId);

        if (envelope != null)
        {
            envelopeData = envelope;
        }
    }

    return await Task.FromResult(envelopeData);
}

Step 5:

Finally, add the method to the controller.

[HttpPost("DownloadDocument")]
public async Task<IActionResult> DownloadDocument([FromBody] DocumentRequest documentRequest)
{
    var apiResponse = await _docuSignService.DownloadDocument(documentRequest);
    if (apiResponse != null)
    {
        return File(apiResponse.FileStream, apiResponse.MimeType, apiResponse.DocumentName);
    }
    else
    {
        return new JsonResult(apiResponse);
    }
}

Conclusion:

You will receive a file stream after adding this code, which you can then use for direct API requests to download files. Keep in mind that in order to complete this activity, you also need a DocuSign access token and an envelope id.

Related Articles:

Getting Started With The DocuSign

Get Authentication Token From The DocuSign (Part-1)

Get Authentication Token From The DocuSign (Part-2)

How To Create JSON Document File For Signature In DocuSign

Send Document To Other Users For Signature In DocuSign

Get Document Data From DocuSign

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories