Automate eSignature Requests in Apps Script using BoldSign API
Published:
This guide provides a step-by-step approach to automating document workflows using Google Forms, Google Sheets, Google Docs templates, and BoldSign for electronic signatures. Upon submission of a Google Form, the system dynamically generates a document from a predefined Google Docs template, populates it with data from the associated Google Sheet, converts it into a PDF, and seamlessly sends it to BoldSign for eSignature.
Follow the Step-by-Step Instructions:
Step 1: Create a Google Docs Template
- Go to https://docs.google.com and create a new document.
- Add placeholders as per your requirement, for example like {{Name}} and {{Email}} etc., where you want the data to appear.
- Save the document and copy the document ID from the URL.
Step 2: Create a Google Form
- Go to https://forms.google.com and create a new form.
- Add fields based on your requirements, for example Name, Email
- Link the form to a Google Sheet to store responses.
Step 3: Prepare the Apps Script
- Open the linked Google Sheet.
- Go to Extensions > Apps Script.
Code Snippet:
function onFormSubmit(e) {
const apiKey = 'YOUR_BOLDSIGN_API_KEY ';
const templateId = 'YOUR_GOOGLE_DOC_TEMPLATE_ID ';
const name = e.values[1]; // Name field
const email = e.values[2]; // Email field
// Step 1: Copy the template
const copiedDoc = DriveApp.getFileById(templateId).makeCopy(`Agreement for ${name}`);
const docId = copiedDoc.getId();
const doc = DocumentApp.openById(docId);
const body = doc.getBody();
// Step 2: Replace placeholders
body.replaceText('{{Name}}', name);
body.replaceText('{{Email}}', email);
doc.saveAndClose();
// Step 3: Export as PDF
const pdf = DriveApp.getFileById(docId).getAs('application/pdf');
const base64File = Utilities.base64Encode(pdf.getBytes());
const base64WithType = `data:${pdf.getContentType()};base64,${base64File}`;
// Step 4: Send to BoldSign
const payload = {
title: 'Employment Agreement',
message: 'Enter a message.',
files: [
{
fileName: pdf.getName(),
base64: base64WithType
}
],
signers: [
{
name: name,
emailAddress: email,
formFields: [
{
id: 'Sig1',
fieldType: "Signature",
isRequired: true,
pageNumber: 1,
bounds: {
x: 100,
y: 300,
width: 200,
height: 30
}
},
]
}
],
};
const options = {
method: 'post',
contentType: 'application/json',
headers: {
'X-API-KEY': apiKey
},
payload: JSON.stringify(payload),
muteHttpExceptions: true
};
try {
const response = UrlFetchApp.fetch('https://api.boldsign.com/v1/document/send', options);
Logger.log('Response Code: ' + response.getResponseCode());
Logger.log('Response Body: ' + response.getContentText());
} catch (error) {
Logger.log('Error: ' + error.message);
}
}
Step 4: Set Up the Trigger
- In Apps Script, go to the Triggers tab.
- Click “+ Add Trigger”.
- Choose the function: “onFormSubmit”
- Event source: “From spreadsheet”
- Event type: “On form submit”
- Save the changes.
Step 5: Test the Workflow
- Submit a test entry via Google Form.
- A personalized PDF document will be generated and automatically sent to BoldSign for signature.