Version: 5.2.x

Create User

Create User#

To create a user, you will need to make a TLS request to the OpenHIM API for the below method and endpoint and supply the JSON object for the user.

Method: POST
Endpoint: {openhim_url}:8080/users
Payload: JSON object of the user

Example#

Before we can send our request to the OpenHIM API we need to ensure that we construct our valid HTTP headers to successfully authenticate with the OpenHIM API.

Copy the below code at the bottom of your nodejs script that handles the authentication of the OpenHIM headers as described in the authentication section.

Replace the openhimOptions values with the correct implementation details and supply the SampleData payload to submit. You will also need to update the userPassword variable to your desired password. The password will be used to create the passwordHash and passwordSalt, which are added to the SampleData payload.

// append below code to the "openhim-api.js" script containing the authentication methods.
// This is described within the authentication section
const genUserPassword = (password) => {
return new Promise((resolve) => {
const passwordSalt = crypto.randomBytes(16)
// create passhash
let shasum = crypto.createHash('sha512')
shasum.update(password)
shasum.update(passwordSalt.toString('hex'))
const passwordHash = shasum.digest('hex')
resolve({
"passwordSalt": passwordSalt.toString('hex'),
"passwordHash": passwordHash
})
})
}
(async () => {
const openhimOptions = {
apiURL: 'https://localhost:8080',
apiEndpoint: '/users',
username: '[email protected]',
password: 'openhim-password',
rejectUnauthorized: false
}
const userPassword = "user-password"
const userPasswordDetails = await genUserPassword(userPassword)
const SampleData = 'SampleData'
SampleData.passwordHash = userPasswordDetails.passwordHash
SampleData.passwordSalt = userPasswordDetails.passwordSalt
const headers = await genAuthHeaders(openhimOptions)
const options = {
method: 'POST',
url: `${openhimOptions.apiURL}${openhimOptions.apiEndpoint}`,
rejectUnauthorized: openhimOptions.rejectUnauthorized,
headers: headers,
body: SampleData,
json: true
}
request(options, (error, response, body) => {
if (error) throw new Error(error)
console.log({
statusCode: response.statusCode,
body
})
})
})()

Execute the below command in your terminal to run the nodejs script

node openhim-api.js

The response status will be 201 if successful.

Once created, a user's activation email is sent to the users email. For this email to be sent, the OpenHIM needs to be configured with the correct mail settings. This can be done in the configuration