cancel
Showing results for 
Search instead for 
Did you mean: 
intapiuser
Community Team Member
Community Team Member
Here is a python script to that describes how to manage users with sisense api:
What you need:
  • rest api key token (not relevant for v6.5+, need to use /api/v1/authentication/login)
  • admin credentials
  • python installed
import time
import json
import urllib
import jwt
import requests

REST_API_TOKEN = '200491aab08ac9e519a751d3392a3643'  # located at yourserver/app/settings#/rest
SISENSE_BASE_URL = 'http://localhost:8081'

EMAIL = '[email protected]'
PASSWORD = 'sisense'

# Create JWT token
def create_jwt_token():
    payload = {
        "iat": int(time.time())
    }

    shared_key = REST_API_TOKEN
    payload['email'] = EMAIL
    payload['password'] = PASSWORD

    jwt_string = jwt.encode(payload, shared_key)
    encoded_jwt = urllib.quote_plus(jwt_string)  # url-encode the jwt string

    return encoded_jwt


def send_request(url, http_method, params=None, payload=None):
    encoded_jwt = create_jwt_token()
    headers = {'x-api-key': encoded_jwt}

    if payload:
        headers['Content-Type'] = 'application/json'
    if http_method.upper() == 'GET':
        response = requests.get(url, params=params, headers=headers)
    elif http_method.upper() == 'POST':
        data = json.dumps(payload)
        response = requests.post(url, data=data, params=params, headers=headers)
    elif http_method.upper() == 'PUT':
        data = json.dumps(payload)
        response = requests.put(url, data=data, params=params, headers=headers)
    elif http_method.upper() == 'DELETE':
        response = requests.delete(url, headers=headers)
    return response

# get all users
users_url = SISENSE_BASE_URL + '/api/users'
users = send_request(all_users_url, 'GET')

# add new user to the system
new_user = [{
    "email": "[email protected]",
    "active": "true",
    "hash": "cb7f9219fabe98ed5bb5b32c5ce2854c"  # password, md5 hashed
}]

add_new_user_res = send_request(users_url, "POST", payload=new_user)

# update user
user_to_update = "[email protected]"
update_info = {
    "firstName": "user"
}

update_user_url = users_url + '/' + urllib.quote(user_to_update)

update_user_res = send_request(update_user_url, "PUT", payload=update_info)


# search user
search_params = {
    'search': '[email protected]' # user to search
}
search_users_url = users_url
search_users_res = send_request(users_url, 'GET', params=search_params)

# delete user
# current delete http method api has issue with the returned status code, it works but python will throw an error, use the # following in order delete user

if search_users_res.reason == 'OK':
    current_user = json.loads(search_users_res.text)
    if current_user is not None and len(current_user) == 1:
        current_user = current_user[0]
        delete_user_res = send_request(SISENSE_BASE_URL + '/api/users/delete', "post",
                                       payload=[str(current_user.get('_id'))])

print "Status: 200 OK"
print 'Content-Type: application/json \r\n'
Rate this article:
Version history
Last update:
‎03-02-2023 09:18 AM
Updated by:
Contributors