I’m currently working on a project where I need to prefill DocuSign template document fields with dynamic data before sending the envelope. I’ve tried multiple approaches but haven’t been successful so far.
Current approach:
I’m using the DocuSign eSign Ruby SDK to interact with the DocuSign API. Here’s a simplified version of my code:
<code> def send_data_using_template(access_token)
api_client = DocuSign_eSign::ApiClient.new
api_client.default_headers['Authorization'] = 'Bearer ' + access_token
templates_api = DocuSign_eSign::TemplatesApi.new(api_client)
envelopes_api = DocuSign_eSign::EnvelopesApi.new(api_client)
options = DocuSign_eSign::ListTemplatesOptions.new
options.search_text = "Example document generation template"
results = templates_api.list_templates(ENV['DOCUSIGN_ACCOUNT_ID'], options)
template = results.envelope_templates.find { |template| template.name == "Example document generation template" }
template_id = template.template_id if template
current_user_name = "Alia"
current_user_email = "[email protected]"
envelope_definition = DocuSign_eSign::EnvelopeDefinition.new({
emailSubject:" EMAIL_SUBJECT"
text_tab = DocuSign_eSign::Text.new
text_tab.tab_label = 'Text 9104f26a-e0fc-4b18-9843-07944aaedcda'
text_tab.value = 'Jabberwocky!'
tabs = DocuSign_eSign::Tabs.new
tabs.text_tabs = [text_tab]
signer = DocuSign_eSign::TemplateRole.new({
email: current_user_email,
envelope_definition.template_roles = [signer]
envelopes_api = DocuSign_eSign::EnvelopesApi.new api_client
response = envelopes_api.create_envelope ENV['DOCUSIGN_ACCOUNT_ID'], envelope_definition
if response.envelope_id.present?
puts "Envelope created successfully"
envelope_status = envelopes_api.get_envelope ENV['DOCUSIGN_ACCOUNT_ID'], response.envelope_id
puts "Envelope status: #{envelope_status.status}.................................."
break if ['completed', 'signed'].include?(envelope_status.status)
"https://demo.docusign.net/restapi/v2.1/accounts/#{ENV['DOCUSIGN_ACCOUNT_ID']}/envelopes/#{response.envelope_id}/documents/combined",
headers: { "Authorization" => "Bearer #{access_token}" }
timestamp = Time.now.strftime("%Y%m%d%H%M%S")
signed_document_path = "config/signed_document_#{timestamp}.pdf"
File.open(signed_document_path, "wb") { |file| file.write(response.body) }
puts "Signed document saved successfully at #{signed_document_path}"
puts "Error retrieving signed document: #{response}"
puts "Error creating envelope: #{response}"
<code> def send_data_using_template(access_token)
api_client = DocuSign_eSign::ApiClient.new
api_client.default_headers['Authorization'] = 'Bearer ' + access_token
templates_api = DocuSign_eSign::TemplatesApi.new(api_client)
envelopes_api = DocuSign_eSign::EnvelopesApi.new(api_client)
options = DocuSign_eSign::ListTemplatesOptions.new
options.search_text = "Example document generation template"
results = templates_api.list_templates(ENV['DOCUSIGN_ACCOUNT_ID'], options)
template = results.envelope_templates.find { |template| template.name == "Example document generation template" }
byebug
template_id = template.template_id if template
byebug
current_user_name = "Alia"
current_user_email = "[email protected]"
envelope_definition = DocuSign_eSign::EnvelopeDefinition.new({
status: 'sent',
templateId: template_id,
emailSubject:" EMAIL_SUBJECT"
})
text_tab = DocuSign_eSign::Text.new
text_tab.tab_label = 'Text 9104f26a-e0fc-4b18-9843-07944aaedcda'
text_tab.value = 'Jabberwocky!'
tabs = DocuSign_eSign::Tabs.new
tabs.text_tabs = [text_tab]
signer = DocuSign_eSign::TemplateRole.new({
email: current_user_email,
name: current_user_name,
roleName: 'signer',
tabs: tabs
})
envelope_definition.template_roles = [signer]
envelopes_api = DocuSign_eSign::EnvelopesApi.new api_client
response = envelopes_api.create_envelope ENV['DOCUSIGN_ACCOUNT_ID'], envelope_definition
if response.envelope_id.present?
puts "Envelope created successfully"
loop do
envelope_status = envelopes_api.get_envelope ENV['DOCUSIGN_ACCOUNT_ID'], response.envelope_id
puts "Envelope status: #{envelope_status.status}.................................."
break if ['completed', 'signed'].include?(envelope_status.status)
sleep 5
end
response = HTTParty.get(
"https://demo.docusign.net/restapi/v2.1/accounts/#{ENV['DOCUSIGN_ACCOUNT_ID']}/envelopes/#{response.envelope_id}/documents/combined",
headers: { "Authorization" => "Bearer #{access_token}" }
)
if response.success?
timestamp = Time.now.strftime("%Y%m%d%H%M%S")
signed_document_path = "config/signed_document_#{timestamp}.pdf"
File.open(signed_document_path, "wb") { |file| file.write(response.body) }
puts "Signed document saved successfully at #{signed_document_path}"
else
puts "Error retrieving signed document: #{response}"
end
else
puts "Error creating envelope: #{response}"
end
end
</code>
def send_data_using_template(access_token)
api_client = DocuSign_eSign::ApiClient.new
api_client.default_headers['Authorization'] = 'Bearer ' + access_token
templates_api = DocuSign_eSign::TemplatesApi.new(api_client)
envelopes_api = DocuSign_eSign::EnvelopesApi.new(api_client)
options = DocuSign_eSign::ListTemplatesOptions.new
options.search_text = "Example document generation template"
results = templates_api.list_templates(ENV['DOCUSIGN_ACCOUNT_ID'], options)
template = results.envelope_templates.find { |template| template.name == "Example document generation template" }
byebug
template_id = template.template_id if template
byebug
current_user_name = "Alia"
current_user_email = "[email protected]"
envelope_definition = DocuSign_eSign::EnvelopeDefinition.new({
status: 'sent',
templateId: template_id,
emailSubject:" EMAIL_SUBJECT"
})
text_tab = DocuSign_eSign::Text.new
text_tab.tab_label = 'Text 9104f26a-e0fc-4b18-9843-07944aaedcda'
text_tab.value = 'Jabberwocky!'
tabs = DocuSign_eSign::Tabs.new
tabs.text_tabs = [text_tab]
signer = DocuSign_eSign::TemplateRole.new({
email: current_user_email,
name: current_user_name,
roleName: 'signer',
tabs: tabs
})
envelope_definition.template_roles = [signer]
envelopes_api = DocuSign_eSign::EnvelopesApi.new api_client
response = envelopes_api.create_envelope ENV['DOCUSIGN_ACCOUNT_ID'], envelope_definition
if response.envelope_id.present?
puts "Envelope created successfully"
loop do
envelope_status = envelopes_api.get_envelope ENV['DOCUSIGN_ACCOUNT_ID'], response.envelope_id
puts "Envelope status: #{envelope_status.status}.................................."
break if ['completed', 'signed'].include?(envelope_status.status)
sleep 5
end
response = HTTParty.get(
"https://demo.docusign.net/restapi/v2.1/accounts/#{ENV['DOCUSIGN_ACCOUNT_ID']}/envelopes/#{response.envelope_id}/documents/combined",
headers: { "Authorization" => "Bearer #{access_token}" }
)
if response.success?
timestamp = Time.now.strftime("%Y%m%d%H%M%S")
signed_document_path = "config/signed_document_#{timestamp}.pdf"
File.open(signed_document_path, "wb") { |file| file.write(response.body) }
puts "Signed document saved successfully at #{signed_document_path}"
else
puts "Error retrieving signed document: #{response}"
end
else
puts "Error creating envelope: #{response}"
end
end
I’ve explored the following resources and approaches:
-
DocuSign documentation on prefilling tabs: Link
-
Stack Overflow threads: Thread 1, Thread 2
However, I haven’t been able to successfully prefill the template fields with dynamic data using these methods.