Chilkat Online Tools

Objective-C / DocuSign REST API / 15. Create an Account Template

Back to Collection Items

#import <CkoHttp.h>
#import <CkoJsonObject.h>
#import <CkoHttpResponse.h>

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

CkoHttp *http = [[CkoHttp alloc] init];
BOOL success;

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

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"documents[0].name" value: @"agreement"];
[json UpdateString: @"documents[0].documentBase64" value: @"{{documentBase64}}"];
[json UpdateInt: @"documents[0].documentId" value: [NSNumber numberWithInt: 123]];
[json UpdateString: @"documents[0].fileExtension" value: @"pdf"];
[json UpdateString: @"emailBlurb" value: @"Email message"];
[json UpdateString: @"emailSubject" value: @"email subject"];
[json UpdateString: @"recipients.signers[0].recipientId" value: @"1"];
[json UpdateString: @"recipients.signers[0].roleName" value: @"seller"];
[json UpdateString: @"recipients.signers[0].email" value: @"{{email}}"];
[json UpdateString: @"recipients.signers[0].name" value: @"{{name}}"];
[json UpdateString: @"recipients.signers[0].tabs.signHereTabs[0].documentId" value: @"123"];
[json UpdateInt: @"recipients.signers[0].tabs.signHereTabs[0].pageNumber" value: [NSNumber numberWithInt: 1]];
[json UpdateString: @"recipients.signers[0].tabs.signHereTabs[0].xPosition" value: @"4"];
[json UpdateString: @"recipients.signers[0].tabs.signHereTabs[0].yPosition" value: @"4"];

[http SetRequestHeader: @"Content-Type" value: @"application/json"];
// Adds the "Authorization: Bearer {{accessToken}}" header.
http.AuthToken = @"{{accessToken}}";

CkoHttpResponse *resp = [http PostJson3: @"https://domain.com/{{apiVersion}}/accounts/{{accountId}}/templates" contentType: @"application/json" json: json];
if (http.LastMethodSuccess == NO) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

NSLog(@"%d",[resp.StatusCode intValue]);
NSLog(@"%@",resp.BodyStr);

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",
        "email":{{user1_email}},
        "name": {{user1_name}},
        "tabs": {
          "signHereTabs": [
            {
              "documentId": "123",
              "pageNumber": 1,
              "xPosition": "4",
              "yPosition": "4"
            }
          ]
        }
      }
    ]
  }
}'
https://domain.com/{{apiVersion}}/accounts/{{accountId}}/templates

Postman Collection Item JSON

{
  "name": "15. Create an Account Template",
  "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        \"email\":{{user1_email}},\n        \"name\": {{user1_name}},\n        \"tabs\": {\n          \"signHereTabs\": [\n            {\n              \"documentId\": \"123\",\n              \"pageNumber\": 1,\n              \"xPosition\": \"4\",\n              \"yPosition\": \"4\"\n            }\n          ]\n        }\n      }\n    ]\n  }\n}"
    },
    "url": {
      "raw": "{{baseUrl}}/{{apiVersion}}/accounts/{{accountId}}/templates",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "{{apiVersion}}",
        "accounts",
        "{{accountId}}",
        "templates"
      ]
    },
    "description": "In this example, we will create a template with document, tabs and its recipients specified. This is a great future if we know we will be sending the same envelope multiple times."
  },
  "response": [
  ]
}