Create User API Request – Code Example

The examples below provide code for creating a User.

Prerequisites

Before making an 'create work item' request, you must have an access token to sign the request. You can retrieve an access token using one of the following flows:

Code Examples

C#

using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
namespace HotDocsAdvanceApiExamples
{
    public class UserRequest
    {
        public static async Task<HttpStatusCode> CreateUserAsync(string token)
        {
            using var client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
           
            var userId = Guid.NewGuid();
            var request = new HttpRequestMessage
            {
                RequestUri = new Uri($"https://yourtenancy.yourorganization.com/HdaApi/rest/v1.1/Users/{userId}"),
                Method = HttpMethod.Post,
                Content = JsonContent.Create(new
                {
                    FirstName = "Emma",
                    LastName = "MacDiarmid",
                    EmailAddress = "emma.macdiarmid@example.com",
                    Roles = new[] { "SiteAdministrator" },
                    SendEmail = true
                })
            };
           
            var response = await client.SendAsync(request);
            return response.StatusCode;
        }
    }
}

Powershell

The example below assumes that you have already retrieved an access token and assigned it to the variable $token.


$body = @{
    "firstName" = "Emma"
    "lastName" = "MacDiarmid"
    "emailAddress" = "emma.macdiarmid@example.com"
    "roles" = @("SiteAdministrator")
} | ConvertTo-Json
$header = @{
 "Accept"="application/json"
 "Content-Type"="application/json"
 "Authorization" = "bearer " + $token
} 
$createUserResult = Invoke-RestMethod -Uri "https://yourtenancy.yourorganization.com/HdaApi/rest/v1/Users/5b505239-557c-5570-b53f-7cc7f46445bd" -Method 'Post' -Body $body -Headers $header
Write-Host $createUserResult

JavaScript

The example below assumes that you have already retrieved an access token using the Implicit flow, returning the token as a fragment.

// You must omit the # character from the beginning of the token.
var token = window.location.hash.substr(1);
// The data to be sent with the request
var data = {
    "firstName": "Bob",
    "lastName": "Sherunkle",
    "emailAddress": "bob.sherunkle@example.com",
    "roles": [
        "SiteAdministrator"
    ], 
    "sendEmail": true
};
// Create User request URL
var url = "https://yourtenancy.yourorganization.com/HdaApi/rest/v1/Users/3ca8dc6a-8523-4d89-8c1d-b7306651f7a2";
  // Create the request var xhr = new XMLHttpRequest(); xhr.open("POST", url, true); xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); xhr.setRequestHeader('Authorization', 'Bearer ' + token);// Send the request xhr.send(JSON.stringify(data));