使用DocuSign API与Ruby实现合同预填及发送的技术求助
Hey there! Let’s walk through solving your DocuSign + Ruby workflow hurdles—since you’ve already got dependencies installed, we can focus right on the communication flow setup and info gathering steps you’re stuck on.
1. First, Clarify the Core Workflow You’re Building
Let’s break down the end-to-end process to make it easier to map:
- Pull existing customer data (name, contact info, etc.)
- Map that data to pre-fill fields in your DocuSign template
- Create an envelope that includes the template, pre-filled data, and both signers
- Send the envelope to the signers and track its status
2. Gather All Required Information First
Before writing code, make sure you have these key pieces handy—they’re non-negotiable for the API to work:
- DocuSign API Credentials:
- Your Account ID (find this in DocuSettings > Identify > API Information)
- Integration Key (from your DocuSign developer app)
- Access Token (you can get this via OAuth2 authorization or JWT flow; for testing, use the sandbox’s quick start token)
- Template Details:
- Template ID (copy this from the template’s page in DocuSign)
- Role names for your two signers (set these up in the template editor—they need to match exactly in your code)
- Pre-fill field names (these are the tags you added to the template for customer data, e.g.,
CustomerFullName,CustomerAddress)
- Customer & Signer Data:
- Existing customer data points that map to your pre-fill fields
- Full names and email addresses for both signers
3. Build the Communication Flow with Ruby
Using the docusign_esign gem (the official SDK), here’s a practical code example that ties everything together:
require 'docusign_esign' # Initialize the DocuSign API client (use demo environment first!) api_client = DocuSign_eSign::ApiClient.new('https://demo.docusign.net/restapi') # Set up authorization (replace with your actual access token) api_client.default_headers['Authorization'] = "Bearer YOUR_ACCESS_TOKEN" # Define your envelope with pre-filled data and signers envelope_def = DocuSign_eSign::EnvelopeDefinition.new( status: 'sent', # This tells DocuSign to send the envelope immediately template_id: 'YOUR_TEMPLATE_ID', # Link to your pre-built template # Map signers to template roles template_roles: [ DocuSign_eSign::TemplateRole.new( email: 'first_signer@example.com', name: 'Jane Smith', role_name: 'PrimarySigner' # Must match the role name in your template ), DocuSign_eSign::TemplateRole.new( email: 'second_signer@example.com', name: 'Bob Johnson', role_name: 'SecondarySigner' ) ], # Add pre-filled customer data using custom fields custom_fields: DocuSign_eSign::CustomFields.new( text_custom_fields: [ DocuSign_eSign::TextCustomField.new( name: 'CustomerFullName', # Match the tag name in your template value: 'John Doe' # Your existing customer data ), DocuSign_eSign::TextCustomField.new( name: 'CustomerEmail', value: 'john.doe@business.com' ) ] ) ) # Send the envelope via the Envelopes API envelopes_api = DocuSign_eSign::EnvelopesApi.new(api_client) response = envelopes_api.create_envelope('YOUR_ACCOUNT_ID', envelope_definition: envelope_def) puts "Envelope successfully sent! Envelope ID: #{response.envelope_id}"
4. Fix Common Roadblocks You Might Hit
- Mismatched Role/Field Names: DocuSign is case-sensitive—if your template’s role name is
PrimarySigner, don’t writeprimary_signerin code. Double-check these matches! - Authorization Issues: If you get a 401 error, verify your access token is valid (sandbox tokens expire quickly) and your integration key is linked to your account.
- Pre-fill Data Not Showing Up: Make sure your template uses custom fields or text tabs with exact names matching your code. Test with a simple field first to confirm mapping works.
5. Pro Tips for a Smooth Integration
- Test everything in the DocuSign sandbox (
demo.docusign.net) before touching production—you don’t want to send test envelopes to real signers! - Use the DocuSign Template Editor to preview pre-filled data before coding—this helps you confirm field names are correct.
- Store the
envelope_idfrom the response—you can use it later to check if signers have completed the document via the API.
内容的提问来源于stack exchange,提问作者Alizée Albrecht
相关产品推荐
相关产品推荐

