Templates: Reusable Building Blocks for Complex API Monitoring

The API Science API includes a templates API. A template is the code that represents “a single URL request.” In a sense, then, a template is the equivalent of a software subroutine or function. It is called to perform an action that produces a specified output based on a specific set of input parameters. The template is a subroutine that embeds a call to another “subroutine” that is located on a remote server: our template’s HTTP call to that API sends the input data, and the template gathers the returned result, typically in the form of a JSON or XML response.

All programmers appreciate the value of subroutines and functions. It’s highly inefficient to embed code that performs virtually the same operation in multiple places within a single program or across multiple related programs. So we make libraries of common utilities that can be imported into multiple applications.

API Science templates are similar. You can create a module that sends a request with given input parameters to a specific resource and then receives the response. You can embed this template into any number of your API Science monitors. Thus, rather than manually recreating the same API call in multiple monitors, you can import your template wherever you need it.

A template is a data item that consists of the following elements:

  • id: the integer numerical ID of the template
  • name: the string name of the template
  • method: the HTTP method employed by the template (GET, POST, PUT, PATCH, or DELETE)
  • url: the Uniform Resource Locator (URL) to which the template sends its request
  • headers: an array of keyword/value pairs that inform the URL about the type of response that is requested
  • urlParameters: an array of keyword/value pairs that describe to the URL the exact information you are requesting
  • body: the body text that is sent in the request
  • preProcessScript: a JavaScript string that will be executed prior to this template’s call to the API; in the context of a multi-step monitor, the results of the previous call are available to this JavaScript context (ex: thus enabling you to extract and use data or variables from previous step prior to running this step
  • validations: an array of validation objects to be executed on the response returned by the API call
  • followRedirects: a boolean indicator as to whether the call should automatically follow HTTP redirects (the default is False)
  • createdAt: the time stamp (date/time) for when the template was created
  • updatedAt: the time stamp (date/time) for when the template was last updated

The API Science API lets you retrieve the information about a specific template using code like this:

curl 'https://api.apiscience.com/v1/monitors/34143289/templates/8897123' 
-H 'Authorization: Bearer '

You can retrieve all the templates for a given monitor using code like this:

curl 'https://api.apiscience.com/v1/monitors/34143289/templates' 
-H 'Authorization: Bearer <YOUR_API_KEY>'

And you can integrate a new template into a monitor using code like this:

curl 'https://api.apiscience.com/v1/monitors/1922876/templates' 
-H 'Content-Type: application/json' -H 'Authorization: Bearer ' 
--data-binary $'{\n  "name": "My Cool Template",\n  
"method": "POST",\n  "href": "http://apiscience.com",\n  
"headers": [\n    { "Authorization": "none" },\n    
{ "Authorization": "oauth2" },\n    
{ "Dynamic": "{{foobar}}" }\n  ],\n  "urlParameters": [\n    
{ "foobar": "cow" }\n  ],\n  "body": null,\n  
"preProcessScript": null,\n  "validations": []\n}\n'

Thus, the API Science templates API facilitates creating reusable API calls, including validations, that can be employed in multiple API monitors. API Science templates are the equivalent of subroutines in the major programming languages: they call a resource given a set of input data, and they receive the resultant response. They can be utilized across any number of monitors that assess the uptime and performance of your product.

–Kevin Farnham