Welcome back to another MSP REST API Tutorial! In this tutorial, we’ll dive a bit deeper into the complexities of programming, yet aiming to simplify the complexities.
Using the MSP REST API Tutorial, I’ve shared how to create, retrieve, and delete a record for a volunteer. Last but certainly not least, I’ll share how to update a record for a volunteer using both PHP and Python programming.
As with all tutorials, be sure you have thoroughly reviewed the “Getting Started” page and checked all the boxes on tutorial requirements.
Let’s get started updating 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 PUT endpoint of the Volunteers collection.
As I know you’re eager to get started, so let’s dive in and explore the world of updating a volunteer record using MSP REST API without further ado.
How to update a volunteer record using PHP.
Using the volunteers’ PUT endpoint, updating a volunteer record via MSP REST API using PHP resembles the API call that uses the volunteers CREATE endpoint.
Now where things can get a bit tricky with updating a volunteer record via the MSP REST API, is when it comes to the endpoint properties. In this tutorial, I’ve decided the following needs to be updated for volunteer with MSP Volunteer Unique Record ID #31:
- firstName of ‘John’ changes to ‘Gerald’
- email of ‘johndoe@gmail.com’ changes to ‘geralddoe@gmail.com’
- address of ‘340 S Lemon Ave, #3441\nWalnut, CA 91789’ changes to ‘1600 Pennsylvania Avenue\nDallas, TX 76123’
- dateModified of ” changes to ‘2023-07-04 18:20:00’
- the externalID did not change, so it’s okay to keep it as is or even comment it out (if it makes you feel better)
To simplify this tutorial, I’ve only updated four (4) endpoint properties for the UPDATE endpoint. Over 30+ options with varying details can be added and updated to this tutorial. We won’t cover those in this tutorial, but at your leisure, review the previous section and the CREATE tutorial, which displays commented-out endpoint options with their respective values.
Except for adding the endpoint properties, the only change in the codebase is cURL’s curl_setopt option of CURLOPT_CUSTOMREQUEST. Instead of ‘CREATE,’ the CURLOPT_CUSTOMREQUEST should be set to ‘UPDATE.’ That’s it!
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 62 63 64 65 |
<?php /*********************************** **** **** update_volunteer.php **** **** This file allows for authentication using your MSP API Key and updating 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 the person you want to update $endPoint = 'volunteers/'.$volunteerID; // MSP API Endpoint // 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; // Define the 'fields' and 'where' arguments as a JSON object $data = array( "firstName" => "Gerald", "lastName" => "Doe", "email" => "geralddoe@gmail.com", "address" => "1600 Pennsylvania Avenue\nDallas, TX 76123", "dateModified" => "2023-07-04 18:20:00", "externalId" => 1231 ); // 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, 'PUT'); // Values: GET, POST, PUT, DELETE, PATCH, UPDATE curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); // Uncomment if you are using MSP API endpoint query parameters -- see lines 24-30 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); // Execute the call and return response data. $result = curl_exec($ch); // Close the cURL connection curl_close($ch); // Decode the JSON response $rez = json_decode($result, true); var_dump($rez); ?> |
How to update a volunteer record using Python.
Now, let’s dive into the Python version. Python is just as powerful, so let’s see how to achieve the same result.
Using the volunteers’ UPDATE endpoint, updating a volunteer record via MSP REST API using Python resembles the API call that uses the volunteers CREATE endpoint.
And just like in the last section, I’ve only updated four (4) endpoint properties for the UPDATE endpoint in the Python code below. Again, over 30+ options with varying details can be added and updated to this tutorial.
Here’s the code for how to delete a volunteer record via MSP REST API using Python:
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 |
import requests # 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 the person you want to update endPoint = 'volunteers/{volunteerID}' # MSP API Endpoint # 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 # Define the payload data as a dictionary data = { "firstName": "Gerald", "lastName": "Doe", "email": "geralddoe@gmail.com", "address": "1600 Pennsylvania Avenue\nDallas, TX 76123", "dateModified": "2023-07-04 18:20:00", "externalId": 1231 } # Set the headers and authentication headers = { "Authorization": "Bearer {mspApiKey}", "Content-Type": "application/json" } # Send the PUT request to update the volunteer record response = requests.put(url, json=data, headers=headers) # Extract the JSON response result = response.json() print(result) |
Tutorial Summary
Congratulations! You’ve leveled up your skills and now possess the prowess to update volunteer records like a pro using PHP or Python.
Select your language of choice, follow the step-by-step instructions, and witness the magic as the volunteer records transform right before your eyes. Remember, regardless of the language you opt for, achieving the desired result truly counts.
For more advanced functionalities and options, consult the comprehensive MSP API documentation. It’s a treasure trove of knowledge waiting to be explored. Don’t hesitate to seek assistance or clarification if you encounter any questions or technical obstacles.
One important reminder: always incorporate error handling into your code, especially when performing update operations. Safety and reliability are key!
That’s all for today’s tutorial. Happy API coding!