API Wrapper
We support an API Wrapper for development in the dotnet ecosystem. The wrapper uses the Client Credentials workflow, hiding all of the OAuth details, and provides methods to easily interact with the data by abstracting the REST API.
The following guide demonstrates the quickest way to consume content. It assumes development in ASP.NET using C#, although there are alternate ways to consume this data. For other approaches, see Community Resources.
1. Authorization
Create or use an API Client record in MinistryPlatform for Authorization. You will need the Client ID and Client Secret from this record to use the Client Credentials OAuth workflow. The wrapper hides the details of this workflow.
Creating an API Client
- In the navigation menu, click .
- Create a new record.
- Enter a Display Name, Client ID, Client Secret, and Client User.
- A. Client ID and Secret
- Together with the Client ID, use the Client Secret to access the API. They are like the API login and password. The secret should be very secure.Tip: We recommend using the Password Generator to generate a secure value. Click the magic wand icon
beside the field to open the Password Generator and select the options you want.
- B. Client User
- The Client User determines the permissions available through the API. Typically, this is "API User" unless permissions need to be more restricted for the API Client.
2. Add REST API Wrapper to your project
In your ASP.NET project, add the API wrapper Nuget package:
PM > Install-Package ThinkMinistry.RESTAPIWrapper
This package provides a simple wrapper around the REST API and OAuth2 implementations. It removes the need to work directly with OAuth, tokens, authentication, and so on. It provides an easy-to-configure system that relies on client credentials to use the API.
- A. Configuration
- The Nuget package adds the following folders and file:
- _DomainData (folder)
- localhost (folder)
- Customer.config (file)
- _RESTAPIWrapper (folder)
First, rename the localhost folder to match the domain you will host. For example, if your application will run at the domain "https://app.fantastic.com/", then you would rename this folder "app.fantastic.com".
Next, edit the customer.config file.- OAuthClientID: The Client ID from the API Client record.
- OAuthClientSecret: Client Secret from the API Client record.
- OAuthBaseAddress: Enter the discovery endpoint for oauth. This is the location of your MinistryPlatform installation with a subfolder named "oauth". Visit this URL in a browser to receive a chunk of JSON, starting with the issuer. To find this URL, see OAuth 2.0 > Discovery.
- ServiceAddress: The location of the API for your MinistryPlatform installation. Normally, this is the same as your Platform URL with "API" at the end. Visit in a browser to display a Platform REST API Test Page. To find this URL, see REST API.
The _RESTAPIWrapper folder has a readme file which may be helpful to get you started using the library. You can delete this folder and file when you are done with them.
3. Reading Data
- A. Query
- First, create and initialize a query object. A query without a table returns a list of tables and fields.
The query can specify a table, fields, and other parameters. Omit fields to return all table fields. You can use field aliases to rename fields. Here is a simple example which returns approved future Events:
var query = new APIQueryRequest { Table = "Events", SelectFields = "Event_ID, Event_Title, Event_Start_Date, Event_End_Date, dp_fileUniqueId AS FileID", Filter = "Event_End_Date < GETDATE() AND Cancelled = 0 AND [_Approved] = 1"};
- B. Results
- Second, retrieve the results using an API client.
var apiClient = PowerApi.CreateWebApiClient();var results = apiClient.GetCollection(query);
When you use the retrieved data, you can use the dynamic objects exposed by the wrapper or cast them to strongly-typed objects, if you prefer.
@foreach (dynamic item in results) { <li>@item.Event_Title.Value</li>}
Strongly Typed Objects
To use Strongly Typed objects rather than dynamic, cast to POCOs using the API client's DynamicListToType method. Here is an example for Events:
results = apiClient.GetCollection(query);IEnumerable<Event> events = apiClient.DynamicListToType<Event>(results);@foreach (Event event in events) { <li>@event.Event_Title.Value</li>}
4. Writing Data
- A. Update
- To update, apply changes to an object you've returned from the API. Here, we use a dynamic object.
item["Participation_Status_ID"].Value = 3; //Attended
Create an Update request.
var update = new APIUpdateRequest();
Specify the record and table name. To update multiples, use the Records collection.
update.Table = "Event_Participants";update.Record = item;
Update and return the result using the PutRecord method of the api client. This results in an updated copy of the record.
var result = apiClient.PutRecord(update);
- B. Create
- First, create a dynamic object with the properties you want. This example makes use of a Donation POCO.
Donation donation = new Donation() { Donation_Amount = (decimal)amount, Donor_ID = 1467, Payment_Type_ID = 1, Receipted = false, Donation_Date = donationDate};
Then, create a Create request.
var create = new APICreateRequest(){ Table = "Donations", Record = donation, SelectFields = "Donation_ID"}
Create and return the result using the PostRecord method of the API client.
var result = apiClient.PostRecord(create);
You can retrieve the Select Fields from the result:
donation.Donation_ID = result.Donation_ID;
5. Sample App
To further explore the API Wrapper, a sample app is available. You can download and run the samples from this repository: https://github.com/MinistryPlatform/WrapperSample.