New Admins: Register for our new Pure Lecture Series!
Pure's logos
Pure Help Center for Pure Administrators

If you are a researcher, or other non-admin at your institution, click here.

  • Home
  • Announcements
  • Release Notes
  • Technical user guides
  • Training
  • Events
  • Support
  • Contact Us
  • Home
  • Training
  • Technical user guides
  • Pure API

How Can We Help?

Search Results

Filter By Category

Contact Us

If you still have questions or prefer to get help directly from an agent, please submit a request.
We’ll get back to you as soon as possible.

Contact us

Python API Requests: Fundamental Coding ExamplesPython API Requests: Fundamental Coding Examples

Introduction

Through practical demonstrations and step-by-step examples, you will discover how to use Python to interact with the Pure API, enabling access to its wide array of functionality. Whether you are a beginner looking to kickstart your API integration journey or an experienced developer seeking to enhance your skills, this guide will equip you with the knowledge and coding techniques to built API requests using Python.


Requirements

  1. Valid API Key
     
  2. API Documentation
     
  3. Python: Make sure that Python is installed on your system. You can download Python from the official website and follow the installation instructions.
     
  4. Code Editor: Use a code editor or Integrated Development Environment (IDE) such as Visual Studio Code, PyCharm, Jupyter Notebook, etc. for writing and running Python code.
     
  5. Python Libraries: Familiarize yourself with Python libraries commonly used for making API requests, such as requests or http.client

 

Expertise Level

Intermediate - Prior experience working with a programming language is recommended. 

 

Building a Request

Making a GET Request

In the following script, you can see how to create a GET request using the requests library to access and retrieve research outputs from your Pure. Ensure that you substitute the placeholder values <Your_URL> and <Your_API_Key> with your specific Pure API URL and API Key.

By executing this script, you will be able to initiate a GET request to retrieve research outputs from your Pure and access the response data for further analysis or processing. 

# Importing the necessary library for making HTTP requests
import requests

# Global variables for the Pure API URL and API key
PURE_URL = "<Your_URL>"  # Replace <Your_URL> with your actual Pure API URL
API_KEY = "<Your_API_Key>"  # Replace <Your_API_Key> with your own API Key

# Function to retrieve the required headers for the API request
def get_headers():
    return {
        "api-key": API_KEY,
    }

# Function to make a GET request to the Pure API endpoint
def make_request():
    # Constructing the URL for the API endpoint
    url = 'https://{}/ws/api/research-outputs/'.format(PURE_URL)
    
    # Retrieving the headers for the request
    headers = get_headers()
    
    # Making a GET request to the specified URL with the headers
    response = requests.get(url, headers=headers)

    # Printing the response status code and content
    print(response.status_code)
    print(response.text)

# Main program execution
if __name__ == '__main__':
    # Calling the function to make the API request
    make_request()
   


Making a PUT Request

In the following script, you can see how to create a PUT request using the requests library to create a person within your Pure instance. 

# Importing the necessary library for making HTTP requests
import requests

# Global variables for the Pure API URL and API key
PURE_URL = "<Your_URL>"  # Replace <Your_URL> with your actual Pure API URL
API_KEY = "<Your_API_Key>"  # Replace <Your_API_Key> with your own API Key

# Function to retrieve the required headers for the API request
def get_headers():
    return {
        "api-key": API_KEY,
    }

# Function to make a PUT request to update data in the Pure API
def make_put_request():
	# Constructing the URL for the API endpoint
    url = 'https://{}/ws/api/research-outputs/'.format(PURE_URL)
    
    # Sample data for the JSON body to be sent in the PUT request
    data = {
        "key1": "value1",
        "key2": "value2"
    }

	# Retrieving the headers for the request
    headers = get_headers()
    
    # Making a PUT request to update data with the specified JSON body
    response = requests.put(url, json=data, headers=headers)
    
    # Printing the response status code and content
    print("Response Status Code:", response.status_code)
    print("Response Content:")
    print(response.text)

# Main program execution
if __name__ == '__main__':
    # Calling the function to make the API request
    make_put_request()


Next Steps: 

As you continue to explore the capabilities of the Pure API and Python integration, consider adapting the provided code snippets to suit your requirements and extend the functionality to meet your project needs. Dive deeper into the Pure API documentation, experiment with different endpoints, and leverage Python's versatility to enhance your API interactions.


Practical example 

In this example, we will use the API request structure outlined in the previous section to retrieve a list of all persons and export this data to an Excel file. The script code provided can be easily adapted to extract and handle various types of content from your Pure instance.

# Importing the necessary libraries 
import requests
import pandas as pd

# Global variables for the Pure API URL and API key
PURE_URL = "<Your_URL>"  # Replace <Your_URL> with your actual Pure API URL
API_KEY = "<Your_API_Key>"  # Replace <Your_API_Key> with your own API Key
SIZE = 100 # Replace with the desired amount of items

# Function to retrieve the required headers for the API request
def get_headers():
    return {
        "api-key": API_KEY,
    }

# Function to make a GET request to the Pure API endpoint to retrieve persons
def get_persons():
    # Constructing the URL for the API endpoint to retrieve persons
    url = 'https://{}/ws/api/persons/?size={}'.format(PURE_URL, SIZE)
    
    # Retrieving the headers for the request
    headers = get_headers()
    
    # Making a GET request to the specified URL with the headers
    response = requests.get(url, headers=headers)

    # Returning the JSON response if the request is successful
    if response.status_code == 200:
        return response.json()
    else:
        return f"Error: {response.status_code}"


# Function to save the person data to an Excel file with each key inside "items" as columns and each object as a new row
def save_to_excel(person_data):
    if isinstance(person_data, dict) and "items" in person_data:
        items_data = person_data["items"]
        if items_data:
            df = pd.DataFrame(items_data)
            df.to_excel("persons_data.xlsx", index=False)
            print("Items data saved to persons_data.xlsx")
        else:
            print("No items data to save")
    else:
        print("Data is not in the expected dictionary format or does not contain 'items'")

# Main program execution
if __name__ == '__main__':
    # Calling the function to retrieve persons from the Pure API
    persons_data = get_persons()
    
    # Saving the person data to an Excel file
    save_to_excel(persons_data)
 

As you continue to explore and experiment with Python and the Pure API, remember to refer to the official documentation for detailed information on available endpoints and parameters. Embrace the opportunity to customize the code, integrate it into your projects, and unlock the full potential of API-driven interactions.

 

 

 

 


 

Published at November 21, 2024

Download
Table of Contents
  1. Introduction
  2. Requirements
  3. Expertise Level
  4. Building a Request
  5. Making a GET Request
  6. Making a PUT Request
  7. Practical example
Related Articles
  • Getting started: Pure API user guide
  • Pure API: Access definitions for content and field filtering
  • Testing the Pure API: A Beginner's Guide
  • Testing the Pure API Using RapiDoc
  • Pure API: Inspiration
Keywords
  • pure api
  • python
  • api request
  • get request
  • put request

Was this article helpful?

Yes
No
Give feedback about this article

    About Pure

  • Announcements

    Additional Support

  • Events
  • Client Community
  • Training

    Need Help?

  • Contact Us
  • Submit a Support Case
  • My Cases
  • Linkedin
  • Twitter
  • Facebook
  • Youtube
Elsevier logo Relx logo

Copyright © 2025 Elsevier, except certain content provided by third parties.

  • Terms & Conditions Terms & Conditions
  • Privacy policyPrivacy policy
  • AccesibilityAccesibility
  • Cookie SettingsCookie Settings
  • Log in to Pure Help CenterLog in to Helpjuice Center

Knowledge Base Software powered by Helpjuice

Expand