Skip to main content

User API examples

The Python examples provided use the request library to facilitate the use of the API.

User management

The Users API contains, among other things, basic CRUD operations for user objects. It is to be noted that when dealing with user objects, the userId is the id assigned by the onePortal platform, not the ID that is used when signing in to the system. The user id can be, for example, 5b0cfba4e2bb230ff8b535e6.

The user management API description here is used as a general example on how to access the Management API. The manual found in the /apidoc endpoint is meant to be easy to understand and contain all the information needed to use the API.

Listing users

As an example, Listing 1 shows how to list all users. The example program will issue a GET request to the /user endpoint of the API. The request should include startIndex and count as parameters. The startIndex is a 0-based index of the first result in the current set of results. The count parameter tells the maximum number of items to return per page.

As mentioned before, the API is subject to changes. Therefore the example code in the Listing 1 will have to be modified to fit your environment. By minimum, the client id and secret will have to be changed.

import requests

# define client ID and secret
CLIENT_ID = '9939129297585525'
CLIENT_SECRET = 'change_me'

uri = "https://fi.trivoreid.com/api/rest/v1/user"

# add query parameters
payload = {'startIndex': '0', 'count': '50'}

# make the request
r = requests.get(uri,auth=(CLIENT_ID, CLIENT_SECRET),params=payload)

Creating new user accounts

New user accounts can be created with a POST method HTTP request to the /user endpoint. Listing 2 shows an example on how to do this.

When creating new users, the username will be generated based on the namespace’s username policy unless the policy states that the username may be freely given as part of the request.

Basically, the only required parameter is the username, but some of the username policies require some of the user attributes to be given during the creation request. For example if the username will be the user’s email address, the email address will have to be provided in the request. The same goes for situations where the username is based on the user’s first, middle and last names. In that case the at least one of the names is required.

If the requirements of the username generation are not met, the endpoint will return an error.

import  requests

# change client id and secret to accommodate your setup
CLIENT_ID = '6932851777483210'
CLIENT_SECRET = 'change_me'

uri = "https://fi.trivoreid.com/api/rest/v1/user"

# always use these headers
HEADERS = {'Content-Type': 'application/json', 'accept': 'application/json'}

new_user = {
"username": "john.doe",
"email": "john@t5,fi",

'names': {
'givenName': "John",
'familyName': "Doe"
}
}

r = requests.post(uri,json=new_user, auth=(CLIENT_ID, CLIENT_SECRET), headers=HEADERS)

print(r.status_code)
print(r.headers)
print(r.content)

Deleting user accounts

User accounts can be deleted with a delete method request sent to the user endpoint.