User & Group Management API Call Examples in Python
With the release of our User and Group Management APIs, we can now create, read, update, and destroy users and groups programmatically. CRUD! Here's the official documentation.
Here are some super simple examples showing how you can test this out in Python 3.
To use these APIs, you'll need your site name, api-key, and access to the Users & Groups API (upgraded feature)
Users
- GET Users
- GET Single User
- CREATE User
- UPDATE User
- DELETE User
Groups
- GET Groups
- GET Single Group
- CREATE Group
- UPDATE Group
- DELETE Group
USERS
GET Users
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v1/users' response = requests.get(url, headers=headers) print(json.loads(response.text))
GET Single User
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json', 'HTTP-X-EMAIL': 'useremail@website.com' } url = 'https://api.periscopedata.com/api/v1/users' response = requests.get(url, headers=headers) print(json.loads(response.text))
CREATE User
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } payload = { "first_name": 'MyFirstName' , "last_name": 'MyLastName', "email": 'newuseremail@website.com', "groups": [ 'Administrators' ], "invited_by_email": 'useremail@website.com' } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v1/users?test_mode=true' response = requests.post(url, headers=headers, data=data) print(json.loads(response.text))
UPDATE User
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json', 'HTTP-X-EMAIL': 'newuseremail@website.com' } payload = { "first_name": 'newFirstName' , "last_name": 'newLastName' } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v1/users?test_mode=true' response = requests.put(url, headers=headers, data=data) print(json.loads(response.text))
DELETE User
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json', 'HTTP-X-EMAIL': 'newuseremail@website.com' } url = 'https://api.periscopedata.com/api/v1/users?test_mode=true' response = requests.delete(url, headers=headers) print(json.loads(response.text))
GROUPS
GET Groups
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v1/groups' response = requests.get(url, headers=headers) print(json.loads(response.text))
GET Single Group
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v1/groups/Administrators' response = requests.get(url, headers=headers) print(json.loads(response.text))
CREATE Group
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } payload = { "name": "myNewGroup" , "access": "discovery", "created_by_email": "useremail@website.com" } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v1/groups?test_mode=true' response = requests.post(url, headers=headers, data=data) print(json.loads(response.text))
UPDATE Group
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } payload = { "name": "myExistingGroup" , "access": "View" } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v1/groups/my-new-group?test_mode=true' response = requests.put(url, headers=headers, data=data) print(json.loads(response.text))
DELETE Group
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json', } url = 'https://api.periscopedata.com/api/v1/groups/myexistinggroup?test_mode=true' response = requests.delete(url, headers=headers) print(response.status_code)
These APIs make it super easy to add a user to a site. What if you have multiple sites and want to add a single user to all of them at once? We can do that too! Here's an example:
import requests import json def addUserToSite(firstName, lastName, email, invitedByEmail, siteName, apiKey, groups=[]): payload = { "first_name": firstName , "last_name": lastName, "email": email, "groups": [], "invited_by_email": invitedByEmail } if (groups): payload["groups"] = groups data = json.dumps(payload) auth = siteName + ':' + apiKey headers = { 'HTTP-X-PARTNER-AUTH': auth, 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v1/users?test_mode=true' response = requests.post(url, headers=headers, data=data) print(json.loads(response.text)) def addUserToSites(user, sites): for site in sites: addUserToSite(user["first_name"], user["last_name"], user["email"], user["invited_by_email"], site["site_name"], site["api_key"], user["groups"]) mySites = [ {"site_name":"mySiteName1", "api_key":"myAPIKey1"}, {"site_name":"mySiteName2", "api_key":"myAPIKey2"}, {"site_name":"mySiteName3", "api_key":"myAPIKey3"} ] myNewUser = { "first_name": 'newUserFirstName' , "last_name": 'newUserLastName', "email": 'newUserEmail@domain.com', "groups": [], "invited_by_email": 'myEmail@domain.com' } addUserToSites(myNewUser, mySites)
With this, all you need to do is update the values in the mySites and myNewUser objects. Be sure to remove "?test_mode=true" once you have it set up the way you like!
Here is a script if you have a CSV of users you want to delete in bulk
import requests import json import pandas as pd df = pd.read_csv('my_csv_of_users_to_delete.csv') emails = df.email_address email_array = emails.values email_array for i in email_array: headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key' , 'Content-Type' : 'application/json', 'HTTP-X-EMAIL': i } url = 'https://api.periscopedata.com/api/v1/users?test_mode=False' response = requests.delete(url, headers=headers) print(i) print(json.loads(response.text))
Please sign in to leave a comment.
Comments
0 comments