In this article, we will show you how you can get started setting up an API call using the Python client library. We will cover the most basic steps you need to be able to set up an API call.
In some cases, you might want to extract data from Usabilla or structure the data in a certain way, different from the default Usabilla reports that you can generate from your Usabilla account. In that case, you can use our API to extract the data you need.
- Basic understanding of the Terminal
- Basic understanding of the concept of an API
- A code editor (we will be using Visual Studio Code)
Table of content:
- Check if Python 2.7 is already installed
- Install Python 2.7
- Test if everything works
- Install Usabilla API
- Execute Usabilla API calls
1. Check if Python 2.7 is already installed
To make anything work, we first need to make sure that Python is installed on your computer. Some computers already have Python pre-installed. Let’s first check if you already have Python installed:
- Open Terminal on your computer
Based on the output, we can see that Python 2.7 is installed. If you have version 2.7 (or 2.7.x), you’re lucky and you can continue to step 3! If not, we will have to install Python 2.7 before we can proceed.
If you’re on a Windows computer, you might first have to navigate to the folder where Python is installed for the Python interpreter to work.
2. Install Python 2.7
- Navigate to www.python.org/downloads
- Click on the download link for version 2.7
- Follow the installation instructions
- Open your Terminal and type the command
pythonagain. The Python interpreter should respond with the version number.
3. Test if everything works
Now, let’s create a Python file that we can use to test if everything is set up correctly.
1. Create a working directory (folder) we can store all our Python code in
2. Open Visual Studio Code and create a new file
3. In the Visual Studio Code editor for the new file type:
4. Save the file as
test.py in your Python folder we created in step 1.
5. Open Terminal and move into the folder where we saved our Python test file
Note: You can move from one directory to another one by typing
cd followed by the path to move into the specific folder:
6. Execute the Python file by typing:
7. If everything has been set up correctly you should get Hello World! printed out in your terminal output:
4. Install Usabilla API
Now that you have Python installed, it’s time to install the Usabilla python library on your computer.
1. Open Terminal
2. Install pip on by running
pip install usabilla-api
Note: If you're using a Windows OS or have difficulties installing pip in step 2, then follow the guidelines here: https://pip.pypa.io/en/stable/installing. If you're running into issues in step 3, then type
sudo pip install usabilla-api on Mac. For Windows, run the terminal as Administator and then try again.
5. Execute Usabilla API calls
To get you started with doing API calls towards our servers, we will walk you through an example on how you can retrieve all Usabilla buttons from your account.
1. Start by creating a file in your Python folder which we will name
2. In the code editor, paste this code into your file:
"Python API - List out all Usabilla buttons with ID and name" import usabilla as ub import json # Create an API client with access key and secret key api = ub.APIClient('ACCESS-KEY', 'SECRET_KEY') # Get all buttons in account buttons = api.get_resource( api.SCOPE_LIVE, api.PRODUCT_WEBSITES, api.RESOURCE_BUTTON, '*', iterate=True) parsed_json = json.loads(json.dumps([item for item in buttons])) # Print out all buttons with ID and name for record in parsed_json[:]: btn_id = record['id'] btn_nm = record['name'] print("Button ID: " + btn_id + " || Button name: " + btn_nm)
3. Change the access key and secret key and then save the file.
4. Open Terminal and move into the Python folder by using
5. Once you’re in your python folder with the
getAllButtons.py file, you can execute it from the Terminal by typing:
python getAllButtons.py in the Terminal window.
Congratulations: Here's your trophy for making your API call! We have several example codes on our GitHub page that you now can start experimenting with: https://github.com/usabilla/api-python/tree/master/example