Welcome back to another MSP REST API Tutorial!
In this tutorial, you’ll learn how to retrieve (GET) a volunteer record using the MSP REST API via PHP and Python!
As with all tutorials, be sure you have thoroughly reviewed the “Getting Started” page and checked all the boxes on tutorial requirements.
Whether you’re a tech enthusiast or new to programming, I’ll guide you through the process step-by-step. I’ll start with PHP as the primary language and then explore how to achieve the same task using Python.
Let’s get started and retrieve a volunteer record!
Overview of the Volunteers Collection
We all know that volunteers are the very heartbeat of every organization. One of the most accessed collections of the MSP REST API is the volunteers. To access its data, the Volunteers collection consists of the following endpoints:
- CREATE uses this endpoint, /volunteers, which allows API requests to create a single volunteer record.
- GET uses this endpoint, /volunteers/{volunteerId}, which allows API requests to retrieve and access a single volunteer record based on the unique volunteer’s MSP ID.
- PUT uses this endpoint, /volunteers/{volunteerId}, which allows API requests to access and update a single volunteer record based on the unique volunteer’s MSP ID.
- DELETE uses this endpoint, /volunteers/{volunteerId}, which allows API requests to access and delete a single volunteer record based on the unique volunteer’s MSP ID.
- POST uses this endpoint, /volunteers/list, which allows API requests to retrieve and access a list of volunteer records.
There are nearly 30+ Volunteer properties to consider (see codebase below or MSP REST API documentation) when accessing the Volunteers collection.
In this tutorial, we’ll focus our efforts on mastering the GET endpoint of the Volunteers collection.
As I know you’re eager to get started, so let’s dive in and explore the world of retrieving a volunteer record using MSP REST API without further ado.
How to retrieve a volunteer record using PHP.
First, I’ll share with you how to retrieve a volunteer record via MSP REST API using PHP. And no worries if PHP is not your cup of tea.
Toward the conclusion of this tutorial, I’ll share how to do the same task, but instead of using PHP, you’ll use Python. But for now, let’s rock and roll with PHP.
Setting up the API Variables in PHP.
First, open your preferred text editor or use the editor in cPanel File Manager, name and save the following PHP file: get_volunteer.php.
Let’s code now!
First, you must define the MSP REST API variables, such as the MSP API Key, base URL, and volunteer ID.
Oh yeah, and don’t forget to replace 'Enter key area'
with your actual MSP API Key, and set the volunteerID
to the unique record ID of the volunteer you want to retrieve.
1 2 3 4 |
$mspApiKey = 'Enter key area'; // Your MSP API Key $apiBaseUrl = "https://api.ministryschedulerpro.com/2/"; // MSP API base URL $volunteerID = 31; // Unique record ID of the volunteer $endPoint = 'volunteers/'.$volunteerID; // MSP API Endpoint |
Making the MSP REST API Request Using PHP cURL.
It’s time to make the API request. Should you need to, review how to make an MSP REST API request using PHP cURL to understand the mechanics and inner workings of the API request.
Let’s make the API request using cURL by passing the API key, content type, and volunteer id as JSON in the request.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$url = $apiBaseUrl.$endPoint; $header = array( "Authorization: Bearer $mspApiKey", "Content-Type: application/json" ); $ch = curl_init(); $timeout = 60; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); $result = curl_exec($ch); curl_close($ch); $rez = json_decode($result, true); var_dump($rez); |
Time to Test PHP and Review the Results.
Okay, it’s now time to test your technical prowess. Resembling the code example below, upload your get_volunteer.php file to your web directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?PHP /*********************************** **** **** get_volunteer.php **** **** This file allows for authentication using your MSP API Key and retrieving a volunteer record by volunteer id. **** ***********************************/ // define MSP API variables $mspApiKey = 'Enter key area'; // Your MSP API Key $apiBaseUrl = "https://api.ministryschedulerpro.com/2/"; // MSP API base URL $volunteerID = 31 // MSP Volunteer Unique Record ID for person $endPoint = 'volunteers/'.$volunteerID; // MSP API Endpoints // if the endpoint is not defined and still equals the default of `Enter API Endpoint`, then create an empty $endpoint variable. if($endPoint == "Enter API Endpoint"){ $endPoint = ''; } $url = $apiBaseUrl.$endPoint; // set your MSP API Key. Also uncomment the content type if you are attempting to instantiate a request using an MSP API endpoint. $header = array( "Authorization: Bearer $mspApiKey", "Content-Type: application/json" ); // Time to initiate cURL request using MSP API variables $ch = curl_init(); $timeout = 60; // set the url and other options for curl curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); // Values: GET, POST, PUT, DELETE, PATCH, UPDATE //curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); // uncomment if you are using MSP API endpoint query parameters -- see lines 24-30 // curl_setopt($ch, CURLOPT_POST, true); // uncomment if the previous line is uncommented. ;) curl_setopt($ch, CURLOPT_HTTPHEADER, $header); // execute call and return response data. $result = curl_exec($ch); // close curl connection curl_close($ch); // decode the json response $rez = json_decode($result, true); var_dump($rez); ?> |
Then access the file using a web browser. This executes the PHP script. The response should be displayed on the page.
Can you access and retrieve the desired volunteer’s record and it displays on the web browser page? Fingers crossed, and a prayer was said!
You’ll see the volunteer record details printed as a PHP array if successful.
And there you have it! Accessing and retrieving a volunteer record via MSP REST API using PHP is easy. Any questions?
How to retrieve a volunteer record using Python.
I sense that PHP may not be your cup of tea, but fear not, Python is here to save the day! No judgments whatsoever—both languages are equally capable of getting the job done.
So, without any more delay, let’s dive into the world of Python and get things rolling!
Setting up the API Variables in Python.
First, open your preferred text editor or use the editor in cPanel File Manager, name and save the following Python file: get_volunteer.py.
And before we start Python coding, don’t forget that you’ll need to have the ‘requests’ library installed. If you don’t already have it installed, you can install it using ‘pip install requests’ from Terminal (Mac) or your preferred command-line interface.
PYTHON HELP :: For more assistance with Python, including downloads and documentation, please visit https://www.python.org/.
Let’s code now! Let’s import the necessary modules and define the API variables as we did in the PHP tutorial.
1 2 3 4 5 6 |
import requests msp_api_key = 'Enter key area' # Your MSP API Key api_base_url = "https://api.ministryschedulerpro.com/2/" # MSP API base URL volunteer_id = 31 # Unique record ID of the volunteer endpoint = "volunteers/{volunteer_id}" # MSP API Endpoint |
Making the MSP REST API Request Using Python.
It’s time to make the API request. Should you need to, review how to make an MSP REST API request using Python to understand the mechanics and inner workings of the API request.
Let’s make the API request using Python’s ‘requests’ library and pass the API key, content type, and volunteer data as JSON to retrieve the volunteer record.
1 2 3 4 5 6 7 8 9 10 |
url = api_base_url + endpoint headers = { "Authorization": "Bearer {msp_api_key}", "Content-Type": "application/json" } response = requests.get(url, headers=headers) result = response.json() print(result) |
Time to Test Python and Review the Results.
Okay, it’s now time to test your technical prowess. Resembling the code example below, save, upload, and execute your get_volunteer.py file to your web directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import requests msp_api_key = 'Enter key area' # Your MSP API Key api_base_url = "https://api.ministryschedulerpro.com/2/" # MSP API base URL volunteer_id = 31 # Unique record ID of the volunteer endpoint = "volunteers/{volunteer_id}" # MSP API Endpoint url = api_base_url + endpoint headers = { "Authorization": "Bearer {msp_api_key}", "Content-Type": "application/json" } response = requests.get(url, headers=headers) result = response.json() print(result) |
As with the PHP code, when executed from a web browser, the Python script sends the API request and prints the response, allowing you to review the volunteer’s recorded details.
And there you have it! Retrieving a new volunteer record using Python via the MSP REST API is easy. Any questions?
Tutorial Summary
Congratulations! Whether you followed the PHP or Python approach, you’ve successfully learned how to access and retrieve a volunteer record using the MSP REST API.
Feel free to explore further by customizing the MSP volunteers’ collection data or incorporating additional fields as needed.
And goes without saying, don’t forget to add error handling to your code.
In addition, refer to the MSP API documentation for more advanced functionalities and options.
If you have any questions or encounter technical roadblocks along the way, don’t hesitate to ask for help.
Happy volunteering, and stay tuned for the follow-up tutorials covering the remaining MSP REST API Volunteer endpoints!