Forum Discussion

mray's avatar
mray
Icon for LM Conqueror rankLM Conqueror
4 months ago

Accessing the LogicMonitor REST API with Postman and Bearer Token Authentication

This is a follow-up to the classic Kurt_Huffman post: Accessing the LogicMonitor REST API with Postman and LMv1 API Token Authentication | LogicMonitor - 5143 

That post is still a treasure trove of good discussion. Definitely check it out if you haven't already! While there is passing mention that you can use bearer token auth in Postman, I figured I'd write up a dedicated post. 

Creating a bearer token

With the addition of bearer tokens, we eliminate the need for a Base64 encoded HMAC signature – so we get rid of several steps for auth. No need for any pre-request script, yay!

See this page for generating and using bearer tokens in LogicMonitor: https://www.logicmonitor.com/support/bearer-token

We do have a section in the above page on using the bearer token with Postman, but I like setting up with environment variables for better reusability.

Postman setup with bearer tokens

  1. Download and install Postman, or use http://postman.com 
  2. Launch Postman and create a new collection that will be used for all LogicMonitor API requests. 
  3. Create a new environment with the following variable. You just need one for the bearer token. You should set the type to 'secret' for sensitive credentials.
    • url – https://<portalname>.logicmonitor.com/santaba/rest
      • If you want to work with the LM Ingestion API, duplicate this environment and change the url to 'https://<portalname>.logicmonitor.com/rest' (without "santaba")
    • bearer – secret – For the current value, be sure to prepend the token with "bearer " (with space)
    • Should look like this: bearer lmb_xxx
  4. Create a request within the collection. A sample request is shown below with the necessary parameters configured.
    1. Set the environment for the request (from step 3)
    2. Set the HTTP method for the request
    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. Add the following headers:
      • Authorization: {{bearer}}
      • X-version: 3 (for latest LM API version)
      • POST, PUT, and PATCH requests only: if your request includes JSON data, be sure to select the Body tab and select raw JSON
  • Appreciate it!!

    I haven't coded since college really...(we won't get into how long that's been) and there are a lot of things that have changed and some of it just isn't intuitive to me at this point. Anything that makes learning simpler is always welcome. Thank you!

  • Anonymous's avatar
    Anonymous

    I'd love to see documentation showing how to import the swagger json file (super simple) and build the environment compatible with it. That way the building of requests is already complete, including sample parameters. 

    • Anonymous's avatar
      Anonymous

      Oh, I got it to work.

      1. In Postman, click Import and paste https://www.logicmonitor.com/swagger-ui-master/api-v3/dist/swagger.json. This should start the import process. 
      2. Before clicking the import button, click the gear to view import settings. 
      3. Make sure "Always inherit authentication" is checked on.
      4. Go to the collection root and select the auth tab.
      5. Change the auth type to "Bearer Token" and put {{bearer}} as the token.
      6. Go to the scripts tab and add this to the pre-request script: pm.request.headers.add({key: 'X-Version', value: '3'})
      7. Save the collection.
      8. Build the environment from mray's instructions above, except don't insert the word "bearer" in front of the token.

       

      The swagger collection comes with all optional query parameters enabled on each request, so you'll need to turn those off for each request or you'll get an error response with something like "invalid filter". I don't know if postman's import settings can change this.

      • mray's avatar
        mray
        Icon for LM Conqueror rankLM Conqueror

        Nice! 🔥

        And yeah the bear auth type is a good alternative for sure. Prob the "more official" way tbh. The only upside of my method of including "bearer" in the standard auth header is that you could swap between bear and LMv1 tokens more seamlessly. Could just be helpful for Support folks or people making the transition away from LMv1 tokens.