Chilkat Online Tools

SQL Server / DocuSign REST API / 1. Create an Envelope

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    -- Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    -- The following JSON is sent in the request body.

    -- {
    --   "documents": [
    --     {
    --       "documentBase64": "{{documentBase64}}",
    --       "documentId": 123,
    --       "fileExtension": "pdf",
    --       "name": "document"
    --     }
    --   ],
    --   "emailSubject": "Simple Signing Example",
    --   "recipients": {
    --     "signers": [
    --       {
    --         "email": "{{email}}",
    --         "name": "{{name}}",
    --         "recipientId": "{{recipientId}}"
    --       }
    --     ]
    --   },
    --   "status": "sent"
    -- }

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'documents[0].documentBase64', '{{documentBase64}}'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'documents[0].documentId', 123
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'documents[0].fileExtension', 'pdf'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'documents[0].name', 'document'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'emailSubject', 'Simple Signing Example'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'recipients.signers[0].email', '{{email}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'recipients.signers[0].name', '{{name}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'recipients.signers[0].recipientId', '{{recipientId}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'status', 'sent'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
    -- Adds the "Authorization: Bearer {{accessToken}}" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '{{accessToken}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/{{apiVersion}}/accounts/{{accountId}}/envelopes', 'application/json', @json
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json


END
GO

Curl Command

curl -X POST
	-H "Content-Type: application/json"
	-H "Authorization: Bearer {{accessToken}}"
	-d '{
  "documents": [
    {
      "documentBase64": {{document}},
      "documentId": {{documentId}},
      "fileExtension": "pdf",
      "name": "document"
    }
  ],
  "emailSubject": "Simple Signing Example",
  "recipients": {
    "signers": [
      {
        "email": {{user1_email}},
        "name": {{user1_name}},
        "recipientId": "{{recipientId}}"
      }
    ]
  },
  "status": "sent"
}'
https://domain.com/{{apiVersion}}/accounts/{{accountId}}/envelopes

Postman Collection Item JSON

{
  "name": "1. Create an Envelope",
  "event": [
    {
      "listen": "test",
      "script": {
        "type": "text/javascript",
        "exec": [
          "var jsonData = JSON.parse(responseBody);",
          "postman.setEnvironmentVariable(\"envelopeId\", jsonData.envelopeId);"
        ]
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Authorization",
        "value": "Bearer {{accessToken}}"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"documents\": [\n    {\n      \"documentBase64\": {{document}},\n      \"documentId\": {{documentId}},\n      \"fileExtension\": \"pdf\",\n      \"name\": \"document\"\n    }\n  ],\n  \"emailSubject\": \"Simple Signing Example\",\n  \"recipients\": {\n    \"signers\": [\n      {\n        \"email\": {{user1_email}},\n        \"name\": {{user1_name}},\n        \"recipientId\": \"{{recipientId}}\"\n      }\n    ]\n  },\n  \"status\": \"sent\"\n}"
    },
    "url": {
      "raw": "{{baseUrl}}/{{apiVersion}}/accounts/{{accountId}}/envelopes",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "{{apiVersion}}",
        "accounts",
        "{{accountId}}",
        "envelopes"
      ]
    },
    "description": "This example is a very basic example of creating an evenlope with a document and a single signer. This evenlope does not specify a tab."
  },
  "response": [
  ]
}