ClickCease
Exporting data from API to CSV with Python

How to Export your Community User Data from API to CSV using Python

Italo Orihuela
Engineer
Android
iOS
Web
Aug 4, 2023

In this tutorial we will explore how to use Python to extract user data from an API and export it to a CSV (Comma Separated Values) file. For this purpose, we will use Amity API for getting users' profile information (V3). Find the the list of available APIs here.

Amity Social Cloud is a popular platform that offers SDKs and API for building social features in applications. By using the requests library for making API requests and the CSV module for handling CSV files, we can seamlessly export user data and perform further analysis or reporting.

Pre-requisites

  • An Amity Account
  • An Amity API Key

Before proceeding, ensure you have Python installed on your system. Additionally, you need the requests library, which you can install using pip:

Note: If you haven’t already registered for an Amity account, we recommend following our comprehensive step-by-step guide on how to create your new network.

Step 1: Set Up Amity API Endpoint and Authorization

To get started, you will need to have an Amity API endpoint from which we can fetch user data. In the provided code, you need to replace {HERE} in the URL variable with the appropriate Amity API endpoint. Additionally, make sure you have an authorization token to access the API securely. The Authorization header must contain the Bearer token obtained from Amity’s authentication process.

Step 2: Configure the CSV File

We will create a CSV file named “users.csv” to store the extracted user data. The CSV file will have a header row containing field names such as user ID, name, email, etc., followed by rows of user data.

Step 3: Make API Requests and Save Data

The provided code contains a while loop that makes requests to the Amity API and handles pagination if there are multiple pages of user data. The Amity API may use pagination to limit the number of results per request. To retrieve all user data, we need to loop through each page until there are no more pages left.

Step 4: Let’s dive into the critical parts of the code

  • The while loop continues until has_more_pages is set to False, indicating that there are no more pages left to fetch.
  • The requests.get() method is used to send a GET request to the Amity API. We include the appropriate headers, including the authorization token, and the params variable to pass additional parameters like the page token for pagination.
  • If the response status code is 200 (indicating a successful response), we extract the user data from the JSON response.
  • On the first page, we extract the field names from the first user object and write them as the CSV header using <span id="greylight" class="greylight">csv_writer.writerow(field_names)</span>
  • We then iterate through each user and write their data to the CSV file using <span id="greylight" class="greylight">csv_writer.writerow(user.values())</span>
  • We keep track of pagination using next_page and page_token. If there is a next_page, we update page_token to fetch the next page in the next iteration of the loop.
  • If an error occurs during the API request, we display the error message and exit the loop.

Step 5: Close the CSV File

After the while loop finishes, we close the CSV file using <span id="greylight" class="greylight">csv_file.close()</span>, ensuring that the data is saved properly.

Final Thoughts

In this tutorial, we have demonstrated how to use Python to fetch user data from the Amity API and export it to a CSV file, you can find the GitHub repository here.

The provided code allows you to access Amity’s user data programmatically, facilitating analysis, reporting, and further data processing. Whether you are building a community platform, social application, or any other application with user engagement, this approach empowers you to seamlessly integrate Amity’s data into your workflows.