Forum Discussion

Kurt_Huffman's avatar
Kurt_Huffman
Former Employee
7 years ago

Accessing the LogicMonitor REST API with Postman and LMv1 API Token Authentication

Introduction

Postman is widely used for interacting with various REST APIs such as LogicMonitor's. However, there is no out-of-the-box support for the LMv1 authentication method which we recommend as a best practice. This document describes how to configure Postman to use LMv1 authentication when interacting with our REST API.

Overview

Postman's pre-request script functionality provides the ability to generate the necessary Authorization header for LMv1 authentication. As the name suggests, the pre-request script runs immediately before the request is made to the API endpoint. We set the pre-request script at the collection level in Postman so that it will run automatically for every request that is part of the collection.

The script requires three input parameters: a LogicMonitor API token (or ID), its associated key, and the full request URL. These parameters are made available to the script by creating a Postman environment and setting the values as environment variables. If you need to access multiple LogicMonitor accounts (portals), create a separate environment for each to store the applicable API and URL information. Since all API requests to a given account use the same base URL (https://<account>.logicmonitor.com/santaba/rest) it is convenient to store this as an environment variable.

The output of the script is the value of the Authorization header. The script writes the header value to an environment variable which is then inserted as the Authorization header value in the request.

Instructions

  1. 1. Download and install Postman.
     
  2. 2. Launch Postman and create a new collection that will be used for all LogicMonitor API requests.
     
  3. 3. In the create collection dialog, select the "Pre-request Scripts" section and paste in the following code.
     
// Get API credentials from environment variablesvar api_id = pm.environment.get('api_id');var api_key = pm.environment.get('api_key');   

// Get the HTTP method from the requestvar http_verb = request.method;

// Extract the resource path from the request URLvar resource_path = request.url.replace(/(^{{url}})([^\?]+)(\?.*)?/, '$2');

// Get the current time in epoch formatvar epoch = (new Date()).getTime();

// If the request includes a payload, included it in the request variablesvar request_vars = (http_verb == 'GET'||http_verb == 'DELETE') ?http_verb + epoch + resource_path : http_verb + epoch + request.data + resource_path;

// Generate the signature and build the Auth headervar signature = btoa(CryptoJS.HmacSHA256(request_vars,api_key).toString());var auth = "LMv1 " + api_id + ":" + signature + ":" + epoch;

// Write the Auth header to the environment variablepm.environment.set('auth', auth);

4. Create a new environment. Create the environment variables shown below. You do not need to provide a value for the "auth" variable since this will be set by the pre-request script. Be sure to use the api_id, api_key, and url values appropriate for your LogicMonitor account.

22308ab1f541cb4e67638269f16af79f.png

5. Create a request and add it to the collection with the pre-request script. A sample request is shown below with the necessary parameters configured.

      1.      1. Set the environment for the request,
      2.      2. Set the HTTP method for the request.
      3.      3. Use {{url}} to pull the base URL from the environment variable. Add the resource path and any request parameters your API request may require.
      4.      4. Add the Authorization header and set the value to {{auth}} to pull the the value from the environment variable.
      5.      5. POST, PUT, and PATCH requests only: if your request includes JSON data, be sure to select the Body tab and add it.

99c498cad6947e23d15b27032aa5badd.png

6. Press Send to send the request. The response will appear below the request in Postman.

34f41380ab54db71c39970488f12bd17.png

Troubleshooting

You receive the response "HTTP Status 401 - Unauthorized"

Confirm the following:

     • The proper environment has been specified for the request.
     • The necessary environment variables have been set and their values are correct. Note that the script relies on the specific variable names used in this document: "api_id", "api_key", "url", and "auth".
     • The request is a member of the collection configured with the pre-request script.

Postman reports "Could not get any response" or "There was an error in evaluating the Pre-request Script:  TypeError: Cannot read property 'sigBytes' of undefined"

Make sure you have set the proper environment for the request and all necessary environment variables and values are present.

 
 
 
  • Are you specifying v2 or v3 in the headers/parameters? If not, you’re probably defaulting to v1 which doesn’t work the same.

    Thank you for the quick reply. Just tried it like this:

    Now i’m getting 401.

  • Anonymous's avatar
    Anonymous

    Ok, that should work. I’d check your variables to make sure they don’t have a space or tab in them that’s making them invalid. Also check that you actually saved your token in LM when you generated it.

  • Would anyone be able to update the pre-request script? I'm very very new to Postman and using APIs, I've recently started getting warnings that some parts of this script are deprecated when I run it:
    Using "request" is deprecated. Use "pm.request" instead.
    Using "CryptoJS" is deprecated. Use "require('crypto-js')" instead.
    Using "btoa" is deprecated. Use "require('btoa')" instead.

    I tried to just start updating it with what it says to use but somehow it's not working completely correctly:
    TypeError: pm.request.url.replace is not a function

    I'm not familiar enough with this to know if there's a better way to do this instead of what looks like forcing it to not complain about using deprecated functions (that is what those two "requires" are doing yeah?)
    Thank you!

    • Anonymous's avatar
      Anonymous

      I haven't been getting those errors and I'm running 11.2.0 of postman.

      Here's my pre-request script:

      var http_verb = request.method;
      var resource_path = request.url.replace(/(^{{url}})([^\?]+)(\?.*)?/, '$2');
      var epoch = (new Date()).getTime();
      var request_vars = (http_verb == 'GET'||http_verb == 'DELETE') ?
      http_verb + epoch + resource_path : http_verb + epoch + request.data + resource_path;
      var signature = btoa(CryptoJS.HmacSHA256(request_vars,pm.environment.get('api_key')).toString());
      var auth = "LMv1 " + pm.environment.get('api_id') + ":" + signature + ":" + epoch;
      pm.environment.set('auth', auth);

      You have it in the pre-req script not the post-req right?

      You should be able to use a bearer token with v3 of the API, so that's another option. I haven't switched yet because mine's working fine.

      • pgordon's avatar
        pgordon
        Icon for Advisor rankAdvisor

        Yes the script is in the Pre-req section for the collection I set up and my script matches what you've shared.

        The script still seems to work, I'm only seeing these warnings if I open up the console. I did do a small recent update it had pending so it probably has something to do with that. I don't know enough to know if these warnings would be because of a change in Postman or a change with Javascript