Hey there, fellow Ruby enthusiast! Ready to add some digital signature magic to your app? Let's dive into the world of DocuSign API integration. With the power of the docusign_esign
gem, you'll be sending and managing digital documents like a pro in no time.
Before we jump in, make sure you've got:
Got all that? Great! Let's get our hands dirty.
First things first, let's add the docusign_esign
gem to your project:
gem 'docusign_esign'
Run bundle install
, and you're good to go.
Now, let's set up those API credentials. Create a config file or use environment variables – whatever floats your boat. Here's a quick example:
DocuSign.configure do |config| config.integration_key = 'your_integration_key' config.host = 'https://demo.docusign.net/restapi' config.api_version = 'v2.1' end
We'll be using JWT Grant authentication because, let's face it, it's awesome. Here's how to get your access token:
private_key = File.read('path/to/your/private_key.pem') token = DocuSign::JWT.get_token( private_key: private_key, user_id: 'your_user_id', expires_in: 3600 )
Boom! You've got your token. Hold onto it; we'll need it soon.
Now for the fun part – let's create and send an envelope!
client = DocuSign::Client.new(access_token: token) envelope_api = DocuSign::EnvelopesApi.new(client) # Create envelope definition envelope_definition = DocuSign::EnvelopeDefinition.new( email_subject: 'Please sign this document', documents: [DocuSign::Document.new( document_base64: Base64.encode64(File.read('path/to/document.pdf')), name: 'Document', file_extension: 'pdf', document_id: '1' )], recipients: DocuSign::Recipients.new( signers: [DocuSign::Signer.new( email: 'john@example.com', name: 'John Doe', recipient_id: '1', tabs: DocuSign::Tabs.new( sign_here_tabs: [DocuSign::SignHere.new( x_position: '100', y_position: '100', document_id: '1', page_number: '1' )] ) )] ), status: 'sent' ) # Send the envelope result = envelope_api.create_envelope(account_id: 'your_account_id', envelope_definition: envelope_definition) puts "Envelope sent! Envelope ID: #{result.envelope_id}"
And just like that, you've sent your first document for signing. Pretty cool, right?
Want to know when your document's been signed? Set up a webhook endpoint in your app:
post '/docusign_webhook' do # Parse the incoming XML doc = Nokogiri::XML(request.body.read) # Extract relevant information envelope_id = doc.at_xpath('//EnvelopeStatus/EnvelopeID').text status = doc.at_xpath('//EnvelopeStatus/Status').text # Do something with the information puts "Envelope #{envelope_id} status changed to #{status}" # Always respond with 200 OK status 200 end
Once you've got the basics down, you can do all sorts of cool stuff:
Here's a quick example of checking envelope status:
status = envelope_api.get_envelope(account_id: 'your_account_id', envelope_id: 'envelope_id') puts "Envelope status: #{status.status}"
Remember to wrap your API calls in begin/rescue blocks to handle any hiccups:
begin result = envelope_api.create_envelope(account_id: 'your_account_id', envelope_definition: envelope_definition) rescue DocuSign::ApiError => e puts "Oops! Something went wrong: #{e.message}" end
And don't forget about rate limits – DocuSign has them, so be nice and don't hammer their API too hard!
Use DocuSign's sandbox environment for testing. It's just like the real thing, but without the commitment. And when things go sideways (they will, trust me), turn on logging:
DocuSign.configure do |config| # ... other config ... config.debugging = true end
And there you have it! You're now armed and dangerous with DocuSign API knowledge. Remember, this is just scratching the surface – there's so much more you can do. So go forth, experiment, and may your documents always be signed on time!
Happy coding, Rubyist!