Introduction to API Science’s Global Variables

The API Science platform provides capability for saving, retrieving, and deleting global variables. The Parameters section on the API Science Help page provides this description:

You can set any number of query string, POST, and PUT parameters using the Params input fields. Each time you add a new variable a new pair of Key/Value input fields will display. You can also append parameters directly to the URL as ?param1=value1.

Setting Parameters

To illustrate setting variables, I’ll create a new monitor by going to my API Science Dashboard and clicking the “Add Monitor” button on the right side of the page; then select and enter the basic configuration items for the new monitor:

Here, I’ve named my monitor “Set Dog and Meal” and given it the tag “dogs” (the reason will become plain in the next step). My monitor will use an HTTP POST command to access the API Science variables API (https://api.apiscience.com/v1/variables), running my monitor every hour from Washington, DC.

The basics are configured, but clicking the “Test Now” button at this point would result in an HTTP 401 return code. The 401 HTTP return code indicates that access to the API is unauthorized, because authentication is required. This problem can be addressed by clicking the “Show Settings” link.

“Show Settings” lets you provide the detailed configuration for your monitor, including parameters, request body, headers, validations, and pre-process script:

To address the authorization problem, I add an HTTP header with the key Authorization and the value Bearer xxx (where xxx is my API Science API key). [Note: to obtain an API Science API Key, go to your account’s “Settings” page and click on “API Keys.”]

I also add a Content-type header, specifying that the content I am posting is in JSON format (application/json).

And, I enter the parameters I’d like to set:

  • Variable my_dog with value Fido;
  • Variable my_meal with value steak;
  • Variable my_status with value hungry.

Saving the monitor, then running it, produces a successful check:

We’ve now successfully posted three parameters to the API Science parameters API using our API Science account to confirm that:

  1. my dog’s name is Fido;
  2. my upcoming meal is steak;
  3. I’m hungry.

In upcoming posts, I’ll show how we can verify this status, then use API Science parameters to both specify and verify what happens next.

–Kevin Farnham

3 thoughts on “Introduction to API Science’s Global Variables

Leave a Reply

Your email address will not be published. Required fields are marked *