Chilkat Online Tools

SQL Server / DocuSign REST API / 19. Create a Template with Role Only

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": [
    --     {
    --       "name": "agreement",
    --       "documentBase64": "{{documentBase64}}",
    --       "documentId": 123,
    --       "fileExtension": "pdf"
    --     }
    --   ],
    --   "emailBlurb": "Email message",
    --   "emailSubject": "email subject",
    --   "recipients": {
    --     "signers": [
    --       {
    --         "recipientId": "1",
    --         "roleName": "seller"
    --       }
    --     ]
    --   }
    -- }

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'documents[0].name', 'agreement'
    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, 'emailBlurb', 'Email message'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'emailSubject', 'email subject'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'recipients.signers[0].recipientId', '1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'recipients.signers[0].roleName', 'seller'

    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}}/templates', '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": [
    {
      "name": "agreement",
      "documentBase64": {{document}},
      "documentId": {{documentId}},
      "fileExtension": "pdf"
    }
  ],
  "emailBlurb": "Email message",
  "emailSubject": "email subject",
  "recipients": {
    "signers": [
      {
        "recipientId": "1",
        "roleName": "seller",
      }
    ]
  }
}'
https://domain.com/{{apiVersion}}/accounts/{{accountId}}/templates

Postman Collection Item JSON

{
  "name": "19. Create a Template with Role Only",
  "event": [
    {
      "listen": "test",
      "script": {
        "type": "text/javascript",
        "exec": [
          "var jsonData = JSON.parse(responseBody);",
          "postman.setEnvironmentVariable(\"templateId\", jsonData.templateId);"
        ]
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Authorization",
        "value": "Bearer {{accessToken}}"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"documents\": [\n    {\n      \"name\": \"agreement\",\n      \"documentBase64\": {{document}},\n      \"documentId\": {{documentId}},\n      \"fileExtension\": \"pdf\"\n    }\n  ],\n  \"emailBlurb\": \"Email message\",\n  \"emailSubject\": \"email subject\",\n  \"recipients\": {\n    \"signers\": [\n      {\n        \"recipientId\": \"1\",\n        \"roleName\": \"seller\",\n      }\n    ]\n  }\n}"
    },
    "url": {
      "raw": "{{baseUrl}}/{{apiVersion}}/accounts/{{accountId}}/templates",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "{{apiVersion}}",
        "accounts",
        "{{accountId}}",
        "templates"
      ]
    },
    "description": "In order to make templates more board and be able to use them in `CompositeTemplates` we can create them by specifying the role only. This way, the recipients can be specified at envelope creation. This example shows how useful templates can be."
  },
  "response": [
  ]
}