Chilkat Online Tools

Swift3 / DocuSign eSignature REST API / 19. Create a Template with Role Only

Back to Collection Items

func chilkatTest() {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()!
    var success: Bool

    // 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",
    //         "tabs": {
    //           "signHereTabs": [
    //             {
    //               "documentId": 123,
    //               "pageNumber": 1,
    //               "xPosition": "4",
    //               "yPosition": "4"
    //             }
    //           ]
    //         }
    //       }
    //     ]
    //   }
    // }

    let json = CkoJsonObject()!
    json.update("documents[0].name", value: "agreement")
    json.update("documents[0].documentBase64", value: "{{documentBase64}}")
    json.updateInt("documents[0].documentId", value: 123)
    json.update("documents[0].fileExtension", value: "pdf")
    json.update("emailBlurb", value: "Email message")
    json.update("emailSubject", value: "email subject")
    json.update("recipients.signers[0].recipientId", value: "1")
    json.update("recipients.signers[0].roleName", value: "seller")
    json.updateInt("recipients.signers[0].tabs.signHereTabs[0].documentId", value: 123)
    json.updateInt("recipients.signers[0].tabs.signHereTabs[0].pageNumber", value: 1)
    json.update("recipients.signers[0].tabs.signHereTabs[0].xPosition", value: "4")
    json.update("recipients.signers[0].tabs.signHereTabs[0].yPosition", value: "4")

    // Adds the "Authorization: Bearer <access_token>" header.
    http.authToken = "<access_token>"

    var resp: CkoHttpResponse? = http.postJson3("https://domain.com/{{apiVersion}}/accounts/{{accountId}}/templates", contentType: "application/json", json: json)
    if http.lastMethodSuccess == false {
        print("\(http.lastErrorText!)")
        return
    }

    print("\(resp!.statusCode.intValue)")
    print("\(resp!.bodyStr!)")
    resp = nil

}

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-d '{
  "documents": [
    {
      "name": "agreement",
      "documentBase64": {{document}},
      "documentId": {{documentId}},
      "fileExtension": "pdf"
    }
  ],
  "emailBlurb": "Email message",
  "emailSubject": "email subject",
  "recipients": {
    "signers": [
      {
        "recipientId": "1",
        "roleName": "seller",
        "tabs": {
          "signHereTabs": [
            {
              "documentId": {{documentId}},
              "pageNumber": 1,
              "xPosition": "4",
              "yPosition": "4"
            }
          ]
        }
      }
    ]
  }
}'
https://domain.com/{{apiVersion}}/accounts/{{accountId}}/templates

Postman Collection Item JSON

{
  "name": "19. Create a Template with Role Only",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "var jsonData = JSON.parse(responseBody);",
          "var templateId = jsonData.templateId;",
          "postman.setEnvironmentVariable(\"templateId\", templateId); "
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "bearer",
      "bearer": [
        {
          "key": "token",
          "value": "{{accessToken}}",
          "type": "string"
        }
      ]
    },
    "method": "POST",
    "header": [
    ],
    "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        \"tabs\": {\n          \"signHereTabs\": [\n            {\n              \"documentId\": {{documentId}},\n              \"pageNumber\": 1,\n              \"xPosition\": \"4\",\n              \"yPosition\": \"4\"\n            }\n          ]\n        }\n      }\n    ]\n  }\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "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": [
  ]
}