Chilkat Online Tools

Node.js / Sunshine Conversations API / Create User

Back to Collection Items

var os = require('os');
if (os.platform() == 'win32') {  
    if (os.arch() == 'ia32') {
        var chilkat = require('@chilkat/ck-node21-win-ia32');
    } else {
        var chilkat = require('@chilkat/ck-node21-win64'); 
    }
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('@chilkat/ck-node21-arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('@chilkat/ck-node21-linux32');
    } else {
        var chilkat = require('@chilkat/ck-node21-linux64');
    }
} else if (os.platform() == 'darwin') {
    if (os.arch() == 'arm64') {
        var chilkat = require('@chilkat/ck-node21-mac-m1');
    } else {
        var chilkat = require('@chilkat/ck-node21-macosx');
    }
}


function chilkatExample() {

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

    var http = new chilkat.Http();
    var success;

    http.BasicAuth = true;
    http.Login = "username";
    http.Password = "password";

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

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

    // {
    //   "externalId": "your-own-id",
    //   "signedUpAt": "2020-05-21T15:53:30.197Z",
    //   "profile": {
    //     "givenName": "Jane",
    //     "surname": "Doe",
    //     "email": "jane.doe@gmail.com",
    //     "avatarUrl": "https://s3.amazonaws.com/avatar.jpg"
    //   },
    //   "metadata": {
    //     "lang": "en-ca"
    //   }
    // }

    var json = new chilkat.JsonObject();
    json.UpdateString("externalId","your-own-id");
    json.UpdateString("signedUpAt","2020-05-21T15:53:30.197Z");
    json.UpdateString("profile.givenName","Jane");
    json.UpdateString("profile.surname","Doe");
    json.UpdateString("profile.email","jane.doe@gmail.com");
    json.UpdateString("profile.avatarUrl","https://s3.amazonaws.com/avatar.jpg");
    json.UpdateString("metadata.lang","en-ca");

    http.SetRequestHeader("Content-Type","application/json");

    // resp: HttpResponse
    var resp = http.PostJson3("https://domain.com/","application/json",json);
    if (http.LastMethodSuccess == false) {
        console.log(http.LastErrorText);
        return;
    }

    var sbResponseBody = new chilkat.StringBuilder();
    resp.GetBodySb(sbResponseBody);

    var jResp = new chilkat.JsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.EmitCompact = false;

    console.log("Response Body:");
    console.log(jResp.Emit());

    var respStatusCode = resp.StatusCode;
    console.log("Response Status Code = " + respStatusCode);
    if (respStatusCode >= 400) {
        console.log("Response Header:");
        console.log(resp.Header);
        console.log("Failed.");

        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "user": {
    //     "id": "7494535bff5cef41a15be74d",
    //     "externalId": "your-own-id",
    //     "signedUpAt": "2020-05-21T15:53:30.197Z",
    //     "profile": {
    //       "givenName": "Jane",
    //       "surname": "Doe",
    //       "email": "jane.doe@gmail.com",
    //       "avatarUrl": "https://s3.amazonaws.com/avatar.jpg"
    //     },
    //     "metadata": {
    //       "lang": "en-ca"
    //     }
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    var Id = jResp.StringOf("user.id");
    var ExternalId = jResp.StringOf("user.externalId");
    var SignedUpAt = jResp.StringOf("user.signedUpAt");
    var GivenName = jResp.StringOf("user.profile.givenName");
    var Surname = jResp.StringOf("user.profile.surname");
    var v_Email = jResp.StringOf("user.profile.email");
    var AvatarUrl = jResp.StringOf("user.profile.avatarUrl");
    var Lang = jResp.StringOf("user.metadata.lang");

}

chilkatExample();

Curl Command

curl -X POST
	-u 'username:password'
	-H "Content-Type: application/json"
	-d '{
    "externalId": "your-own-id",
    "signedUpAt": "2020-05-21T15:53:30.197Z",
    "profile": {
        "givenName": "Jane",
        "surname": "Doe",
        "email": "jane.doe@gmail.com",
        "avatarUrl": "https://s3.amazonaws.com/avatar.jpg"
    },
    "metadata": {
        "lang": "en-ca"
    }
}'
https://domain.com/

Postman Collection Item JSON

{
  "name": "Create User",
  "_postman_id": "44749d06-472c-4409-82b1-abc8a22ad666",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"externalId\": \"your-own-id\",\n    \"signedUpAt\": \"2020-05-21T15:53:30.197Z\",\n    \"profile\": {\n        \"givenName\": \"Jane\",\n        \"surname\": \"Doe\",\n        \"email\": \"jane.doe@gmail.com\",\n        \"avatarUrl\": \"https://s3.amazonaws.com/avatar.jpg\"\n    },\n    \"metadata\": {\n        \"lang\": \"en-ca\"\n    }\n}"
    },
    "url": "{{url}}/v2/apps/{{appId}}/users",
    "description": "Creates a new user."
  },
  "response": [
    {
      "id": "2f2a4e6e-0d1a-4483-b30d-160379704ff5",
      "name": "User already exists",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": {
              "content": "Added as a part of security scheme: bearer",
              "type": "text/plain"
            },
            "key": "Authorization",
            "value": "Bearer <token>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"externalId\": \"your-own-id\",\n    \"signedUpAt\": \"2020-05-21T15:53:30.197Z\",\n    \"profile\": {\n        \"givenName\": \"Jane\",\n        \"surname\": \"Doe\",\n        \"email\": \"jane.doe@gmail.com\",\n        \"avatarUrl\": \"https://s3.amazonaws.com/avatar.jpg\"\n    },\n    \"metadata\": {\n        \"lang\": \"en-ca\"\n    }\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/users",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "users"
          ],
          "variable": [
            {
              "key": "appId"
            }
          ]
        }
      },
      "status": "Conflict",
      "code": 409,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/plain"
        }
      ],
      "cookie": [
      ],
      "body": ""
    },
    {
      "id": "6a8ada4a-ff1f-4176-9d03-904fbc145bb6",
      "name": "Created",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": {
              "content": "Added as a part of security scheme: bearer",
              "type": "text/plain"
            },
            "key": "Authorization",
            "value": "Bearer <token>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"externalId\": \"your-own-id\",\n    \"signedUpAt\": \"2020-05-21T15:53:30.197Z\",\n    \"profile\": {\n        \"givenName\": \"Jane\",\n        \"surname\": \"Doe\",\n        \"email\": \"jane.doe@gmail.com\",\n        \"avatarUrl\": \"https://s3.amazonaws.com/avatar.jpg\"\n    },\n    \"metadata\": {\n        \"lang\": \"en-ca\"\n    }\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/users",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "users"
          ],
          "variable": [
            {
              "key": "appId"
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"user\": {\n  \"id\": \"7494535bff5cef41a15be74d\",\n  \"externalId\": \"your-own-id\",\n  \"signedUpAt\": \"2020-05-21T15:53:30.197Z\",\n  \"profile\": {\n   \"givenName\": \"Jane\",\n   \"surname\": \"Doe\",\n   \"email\": \"jane.doe@gmail.com\",\n   \"avatarUrl\": \"https://s3.amazonaws.com/avatar.jpg\"\n  },\n  \"metadata\": {\n   \"lang\": \"en-ca\"\n  }\n }\n}"
    }
  ]
}