Sign Documents with Digital Signatures using REST API in Node.js

You can electronically sign your documents with digital signatures programmatically on the cloud. The digital signatures are used to validate the authenticity and integrity of the documents. It also enables you to attach a code with your document that acts as a signature. This article will be focusing on how to sign documents with digital signatures using a REST API in Node.js.

The following topics shall be covered in this article:

Document Signature REST API and Node.js SDK

For signing PDF and DOCX files, I will be using the Node.js SDK of GroupDocs.Signature Cloud API. It enables you to create, verify and search various types of signatures such as image, ‎barcode, QR-Code, text-based, digital, and stamp signatures. These signatures can easily be applied in portable or simple ‎documents, spreadsheets, presentations, and images of supported file formats. You can integrate the API into your existing Node.js applications. It also provides .NET, Java, PHP, Android, Ruby, and Python SDKs as its document signature family members for the Cloud API.

You can install GroupDocs.Signature Cloud to your Node.js project using the following command in the console:

npm install groupdocs-signature-cloud --save

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

global.clientId = "112f0f38-9dae-42d5-b4fc-cc84ae644972";
global.clientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618";
global.myStorage = "";
const config = new groupdocs_signature_cloud.Configuration(clientId, clientSecret);
config.apiBaseUrl = "https://api.groupdocs.cloud";

Sign Word Documents using a REST API in Node.js

You can sign Word documents with digital signatures on the Cloud by following the simple steps mentioned below:

Upload the Document

Firstly, upload the Word document to the Cloud using the code example given below:

var resourcesFolder = 'C:\\Files\\sample.docx';
fs.readFile(resourcesFolder, (err, fileStream) => {
// construct FileApi
var fileApi = groupdocs_signature_cloud.FileApi.fromConfig(config);
// create upload file request
var request = new groupdocs_signature_cloud.UploadFileRequest("sample.docx", fileStream, myStorage);
fileApi.uploadFile(request)
});

As a result, the uploaded Word file will be available in the files section of your dashboard on the cloud. Please use the above code sample to upload the certificate and signature image file to the Cloud.

Sign Word Documents with Digital Signatures using Node.js

You can sign your DOCX files with digital signatures programmatically by following the steps given below:

  • Create an instance of the SignApi
  • Create an instance of the FileInfo
  • Set the DOCX file path
  • Create an instance of the SignDigitalOptions
  • Set SignatureType to Digital
  • Set the ImageFilePath and CertificateFilePath
  • Provide the password
  • Set the signature position
  • Create an instance of the SignSettings
  • Assign the SignDigitalOptions and SaveOptions to the SignSettings
  • Create the CreateSignaturesRequest
  • Get results by calling the SignApi.createSignatures() method

The following code example shows how to sign a Word document with digital signatures using a REST API in Node.js.

// initialize api
let signApi = groupdocs_signature_cloud.SignApi.fromKeys(clientId, clientSecret);
// create file info
let fileInfo = new groupdocs_signature_cloud.FileInfo();
fileInfo.filePath = "sample.docx";
// define sign digital options
let opts = new groupdocs_signature_cloud.SignDigitalOptions();
opts.signatureType = groupdocs_signature_cloud.OptionsBase.SignatureTypeEnum.Digital;
opts.imageFilePath = "signature.jpg";
opts.certificateFilePath = "certificate.pfx";
opts.password = "1234567890";
// set signature position on a page
opts.left = 350;
opts.top = 450;
opts.width = 200;
opts.height = 100;
opts.location_measure_type = "Pixels";
opts.size_measure_type = "Pixels";
opts.rotation_angle = 0;
opts.horizontal_alignment = "None";
opts.vertical_alignment = "None";
opts.margin = new groupdocs_signature_cloud.Padding();
opts.margin.all = 5;
opts.margin_measure_type = "Pixels";
// set page to show signatures
opts.page = 1;
// define sign settings
let settings = new groupdocs_signature_cloud.SignSettings();
settings.fileInfo = fileInfo;
settings.options = [opts];
// define save options
settings.saveOptions = new groupdocs_signature_cloud.SaveOptions();
settings.saveOptions.outputFilePath = "signedDigital_sample.docx";
// create signature request
let request = new groupdocs_signature_cloud.CreateSignaturesRequest(settings);
let response = await signApi.createSignatures(request);
console.log("Output file link: " + response.downloadUrl);
Sign Word Documents with Digital Signatures using Node.js

Sign Word Documents with Digital Signatures using Node.js

Download the Signed File

The above code sample will save the signed Word file on the cloud. You can download it using the code sample given below:

// construct FileApi
var fileApi = groupdocs_signature_cloud.FileApi.fromConfig(config);
// create download file request
let request = new groupdocs_signature_cloud.DownloadFileRequest("signedDigital_sample.docx", myStorage);
let response = await fileApi.downloadFile(request);
// save file to the working directory
fs.writeFile("C:\\Files\\signedDigital_sample.docx", response, "binary", function (err) { });

Sign PDF Documents with Digital Signatures using Node.js

You can sign the PDF documents with digital signatures programmatically by following the steps given below:

  • Create an instance of the SignApi
  • Create an instance of the FileInfo
  • Set the PDF file path
  • Create an instance of the SignDigitalOptions
  • Set SignatureType to Digital
  • Set the ImageFilePath and CertificateFilePath
  • Provide the password
  • Create an instance of the SignSettings
  • Assign the SignDigitalOptions and SaveOptions to the SignSettings
  • Create the CreateSignaturesRequest
  • Get results by calling the SignApi.createSignatures() method

The following code example shows how to sign a PDF document with digital signatures using a REST API in Node.js.

// initialize api
let signApi = groupdocs_signature_cloud.SignApi.fromKeys(clientId, clientSecret);
// create file info
let fileInfo = new groupdocs_signature_cloud.FileInfo();
fileInfo.filePath = "sample.pdf";
// define sign digital options
let opts = new groupdocs_signature_cloud.SignDigitalOptions();
opts.signatureType = groupdocs_signature_cloud.OptionsBase.SignatureTypeEnum.Digital;
opts.imageFilePath = "signature.jpg";
opts.certificateFilePath = "certificate.pfx";
opts.password = "1234567890";
opts.allPage = true;
// define sign options
let settings = new groupdocs_signature_cloud.SignSettings();
settings.fileInfo = fileInfo;
settings.options = [opts];
// define save options
settings.saveOptions = new groupdocs_signature_cloud.SaveOptions();
settings.saveOptions.outputFilePath = "signedDigital_sample.pdf";
// create signature request
let request = new groupdocs_signature_cloud.CreateSignaturesRequest(settings);
let response = await signApi.createSignatures(request);
console.log("Output file link: " + response.downloadUrl);
Sign PDF Documents with Digital Signatures using Node.js

Sign PDF Documents with Digital Signatures using Node.js

Verify Digital Signatures using a REST API in Node.js

You can easily verify the digital signatures programmatically by following the steps given below:

  • Create an instance of the SignApi
  • Create an instance of the FileInfo
  • Set the DOCX file path
  • Create an instance of the_VerifyDigitalOptions_
  • Set SignatureType to Digital
  • Create an instance of the VerifySettings
  • Assign the VerifyDigitalOptions and FileInfo to the VerifySettings
  • Create the VerifySignaturesRequest
  • Get results by calling the SignApi.verifySignatures() method
  • Show the results

The following code example shows how to verify the digital signatures using a REST API in Node.js.

// initialize api
let signApi = groupdocs_signature_cloud.SignApi.fromKeys(clientId, clientSecret);
// create file info
let fileInfo = new groupdocs_signature_cloud.FileInfo();
fileInfo.filePath = "signedDigital_sample.docx";
// define verify options
let opts = new groupdocs_signature_cloud.VerifyDigitalOptions();
opts.signatureType = groupdocs_signature_cloud.OptionsBase.SignatureTypeEnum.Digital;
// define verify settings
let settings = new groupdocs_signature_cloud.VerifySettings();
settings.fileInfo = fileInfo;
settings.options = [opts];
// create verify signature request
let request = new groupdocs_signature_cloud.VerifySignaturesRequest(settings);
let response = await signApi.verifySignatures(request);
// show results
if (response.isSuccess) {
console.log("Successfully verified!");
}
else{
console.log("Not verified!");
}
Verify Digital Signatures using a REST API in Node.js

Verify Digital Signatures using a REST API in Node.js

Try Online

Please try the following free online documents signature tool, which is developed using the above API. https://products.groupdocs.app/signature/

Conclusion

In this article, you have learned how to sign Word documents with digital signatures on the cloud. You have also learned how to sign PDF documents with digital signatures using a REST API in Node.js. Moreover, you have learned how to programmatically upload a Word file on the cloud and then download the signed file from the cloud. You can learn more about GroupDocs.Signature Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also