Adding users programmatically
In certain cases, you may need to add users programmatically instead of using the Agents page on your dashboard. Listed below are the 2 reasons you might need to do so:
Addition of users without email verification and/or
Addition of users in bulk
The solution is to add users via REST API call for creation of agents. This call is the obvious choice if only a few agents need to be created. You can simply make this call from tools like Shell/Terminal/Postman. There are no invite or activation emails sent out to the users if they are added through the REST API.
But, if the number of users to be added to an account are greater than 20, then doing it programmatically is more efficient. To do so, you can either use a tool like Postman and create a loop of POST requests from your list of users' information, or you can create a script to do the same.
Below is a sample script that you can use for this purpose.
Note: You need to have experience using command line interface on your computer. (Terminal/Shell). You can create your own script or use the one below. It is a python script and you will need a few packages/modules installed on your machine. The following should suffice: Python, PIP, Request module.
Step 1. Make sure you have the below data in .csv
format
The fourth column "role" is optional. If not specified, the role "agent" is applied to all the users.
Step 2. Once you have this sheet ready you can use the below script with minor changes
- Please update the api_key
of the company you want to add users to
- Please update the file name 'USER_LIST.csv'
with the file name from the previous step (case sensitive)
- If the script is to be run for a private server, then surfly.com
will need to be changed to your-domain.com
import csv
import requests
url = 'https://surfly.com/v2/agents/?api_key=9deXXXXXXXXXXXXXXXXXXX318'
with open('USER_LIST.csv', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
print(row)
create_agent = requests.post(url, data = row)
print(create_agent.text)
Step 3. Run the updated script!
User already existing in the Surfly database will be skipped with the below message:
{"error": "Invalid input: A user with this email already exists"}