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



Show first post

31 replies

Userlevel 7
Badge +18

The code should look like this: 

// 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);

 

I’m getting a 200 response and the body bellow for any get requests I have done. 

{
"data": null,
"errmsg": "Authentication failed",
"status": 1401
}

Also to clarify:

api_id = access ID

api_key = access key

I have set up everything but still i’m getting the response above. My api key has administrator role.

Userlevel 7
Badge +18

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

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.

Userlevel 7
Badge +18

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.

Userlevel 7
Badge +18

FYI, if you want to use Postman for posting to the LM Logs ingestion API endpoint, you’ll have to make some changes.

Cause:

LM decided to omit the /santaba/ portion of the URL. This means that when the pre-request script runs, it tries to pull out the endpoint using the url variable, which still contains the /santaba/ portion. So, authentication will fail. Gotta love non-standard URLs for the API.

Resolution:

All you need to do is override the pre-request script for this one request. Technically, you could modify the url variable in your environment’s variable list, but if you do this, you’ll break all other requests.

Use the following pre-request script for just the POST into LM Logs:

var api_id = pm.environment.get('api_id');
var api_key = pm.environment.get('api_key');
var http_verb = request.method; // this is always POST for this request
var resource_path = '/log/ingest';
var epoch = (new Date()).getTime();
var request_vars = http_verb + epoch + request.data + resource_path;
var signature = btoa(CryptoJS.HmacSHA256(request_vars,api_key).toString());
var auth = "LMv1 " + api_id + ":" + signature + ":" + epoch;
pm.environment.set('auth', auth);

Then configure the url like this:

https://<account>.logicmonitor.com/rest/log/ingest

Replacing <account> with your account name. Select POST and put the logs in the body. This works in my Postman, so I’d be interested to know if this is the only change required to post logs into LM Logs from Postman.

Kudos go to @Allen Van for discovering the root of this issue.

Reply