GET a signature

I've spent two days getting this figured out, thought I'd share the pain

The code sample below is complete instructions on how to send this JSON document to Docusign. Docusign will then email the customer a pdf and request a signature. When the signing process is complete, Docusign will email you the signed pdf. 

Mostly, this web site handles code related to Dynamics GP and 365. I'm a full time Dynamics developer, and this code resulted from a Dynamics customer project. 

But I know that we'll get a lot of 'non Dynamics' traffic on this subject, and the 'membership' thing will irritate them. 

Here's the deal: I spent 16 hours developing this code, and it works. That's worth $9.99, IMHO. It was pretty challenging. Not the JSON API, I've done that lots. Sending the correct auth info, that took some figuring out. 

HTH...

{
  "documents": [
    {
      "documentBase64": "[PDF]",
      "name": "signme.pdf",
      "fileExtension": "pdf",
      "documentId": "1"
    }
  ],
  "emailSubject": "Please sign this document",
  "recipients": {
    "signers": [
      {
        "email": "joe@sample.com",
        "name": "Joe Sample",
        "recipientId": "1",
        "routingOrder": "1",
        "tabs": {
          "signHereTabs": [
            {
              "documentId": "1",
              "pageNumber": "1",
              "recipientId": "1",
              "tabLabel": "SignHereTab",
              "xPosition": "195",
              "yPosition": "147"
            }
          ]
        }
      }
    ]
  },
  "status": "sent"
}

The complete, working code is below, but I need to add a preface about where to get the info. <deep breath> Here goes

Don't confuse any Docusign account you may currently have with the Docusign Sandbox login. 

Go to https://developers.docusign.com/get-started and sign up/login. This is a free dev account, not the paid Docusign account

Go to https://admindemo.docusign.com/api-integrator-key and see if you have a key there under apps and integration keys. If not, sign up for one by clicking 'add'

So, now you have a dev user name, pass, and app key. In the top right corner click the user icon (your initials) and get your acccount ID. Again, note that this is not the paid account that you may or not have, this is the sandbox account id. 

Armed with that information, the code below will work. 

The 'LoginInfo' method is something of a 'hello world', it will bring back some basic account info

The 'SendEnvelope' method will send an email to the email address hard coded into the JSON above. That person can sign, and the signed document will be emailed back to you.

 

 

 

Imports System.IO
Imports System.Net
 
Public Class Test6
 
     Public Sub LogonInfo(strUserName As String, strPassword As String, strIngegratorKey As String)
        Try
 
            'needed to upgrade my project to .NET 4.5.1 to have this available
            ServicePointManager.Expect100Continue = True
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
 
            'form the url
            Dim strUrl As String = "https://demo.docusign.net/restapi/v2.1/login_information"
 
            Dim oRequest As System.Net.HttpWebRequest
            Dim oResponse As System.Net.WebResponse = Nothing
            Dim oPostStream As System.IO.Stream = Nothing
 
            ' Create the web request
            oRequest = DirectCast(System.Net.WebRequest.Create(strUrl), System.Net.HttpWebRequest)
            oRequest.Method = "GET"
            oRequest.Accept = "application/json"
            oRequest.ContentType = "application/json"
 
            ' Add authentication to request
            oRequest.Headers.Add("X-DocuSign-Authentication",
                String.Format("<DocuSignCredentials><Username>{0}</Username><Password>{1}</Password><IntegratorKey>{2}</IntegratorKey></DocuSignCredentials>", strUserName, strPassword, strIngegratorKey))
            'oRequest.ContentLength = 0
 
 
            ' Get the response
            oResponse = DirectCast(oRequest.GetResponse(), System.Net.WebResponse)
 
            'convert the response to a Stream
            Dim oStream As System.IO.Stream = oResponse.GetResponseStream()
            'convert the Stream to a StreamReader
            Dim oStreamReader As New System.IO.StreamReader(oStream)
            'read the response into a String
            Dim strResponse As String = oStreamReader.ReadToEnd
            Stop
 
            'This is the result of this call
            'NOTE: the 'accountID' is neede for the envelope call
            '{
            '    "loginAccounts": [{
            '        "name": "my account name",
            '        "accountId": "12345678",
            '        "baseUrl": "https://demo.docusign.net/restapi/v2.1/accounts/12345678",
            '        "isDefault": "true",
            '        "userName": "Steve Gray",
            '        "userId": "00000000-0000-0000-0000-000000000000",
            '        "email": "me@myemail.com",
            '        "siteDescription": ""
            '    }]
            '}
 
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
 
    End Sub
    Public Sub SendEnvelope(strAccountID As String, strUserName As String, strPassword As String, strIngegratorKey As String)
        Try
 
            ServicePointManager.Expect100Continue = True
            'needed to upgrade my project to .NET 4.5.1 to have this available
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
 
            Dim strJSON As String
            Dim strFile As String = "c:\temp\sample.pdf"
            'Convert the PDF to a stream, then save the stream into a string
            Dim doc3PdfBytes As String = Convert.ToBase64String(System.IO.File.ReadAllBytes(strFile))
 
            'This is the JSON document that holds all the information for the signing
            Dim oEnvelope As New StreamReader("Envelope.json")
            'open it, read it into a string
            strJSON = oEnvelope.ReadToEnd
            'do a string replace, call the pdf into the json. This will be big
            strJSON = Replace(strJSON, "[PDF]", doc3PdfBytes)
 
            'form the url
            Dim strUrl As String = String.Format("https://demo.docusign.net/restapi/v2.1/accounts/{0}/envelopes", strAccountID)
 
            Dim oRequest As System.Net.HttpWebRequest
            Dim oResponse As System.Net.WebResponse = Nothing
            Dim oPostStream As System.IO.Stream = Nothing
 
            'call the JSON string into a byte array
            Dim byteData() As Byte
            byteData = System.Text.UTF8Encoding.UTF8.GetBytes(strJSON)
 
            ' Create the web request
            oRequest = DirectCast(System.Net.WebRequest.Create(strUrl), System.Net.HttpWebRequest)
            oRequest.Method = "POST"
            oRequest.Accept = "application/json"
            oRequest.ContentType = "application/json"
            oRequest.ContentLength = strJSON.Length
 
            ' Add authentication to request
            'THIS is the hard part to figure out
            oRequest.Headers.Add("X-DocuSign-Authentication",
                String.Format("<DocuSignCredentials><Username>{0}</Username><Password>{1}</Password><IntegratorKey>{2}</IntegratorKey></DocuSignCredentials>", strUserName, strPassword, strIngegratorKey))
 
            'send our document
            oPostStream = oRequest.GetRequestStream()
            oPostStream.Write(byteData, 0, byteData.Length)
 
            ' Get the response
            oResponse = DirectCast(oRequest.GetResponse(), System.Net.WebResponse)
 
            'convert the response to a Stream
            Dim oStream As System.IO.Stream = oResponse.GetResponseStream()
            'convert the Stream to a StreamReader
            Dim oStreamReader As New System.IO.StreamReader(oStream)
            'read the response into a String
            Dim strResponse As String = oStreamReader.ReadToEnd
 
            Stop
            'This is the result of this call.
            '{
            '    "envelopeId": "00000000-0000-0000-0000-000000000000",
            '    "uri": "/envelopes/00000000-0000-0000-0000-000000000000",
            '    "statusDateTime": "2020-10-12T14:53:40.2030000Z",
            '    "status": "sent"
            '}
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
 
    End Sub
End Class

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences