Skip to main content
Skip table of contents

Postman

This page outlines how to configure the Postman Pre-request Script to successfully authorize every Postman request sent to the Lissi Agent API.

🚀 Getting started

Minimal Postman environment setup

Lissi_Auth.postman_environment.json

Once imported on the Postman client, the environment should be similar to the following

Go over the following attribute and personalize the environment for the targeted Lissi Agent, the remaining attributes are generated by the pre-request script.

Mandatory

  • oauth_access_token_url → https://[YOUR_DOMAIN]/auth/realms/lissi-cloud/protocol/openid-connect/token

  • oauth_client_id → Should be set by default to lissi-agent-client according to the Keycloak client configuration

  • oauth_username → Username used to access the Lissi Agent

  • oauth_password → Password used to access the Lissi Agent

Optional

  • oauth_expires_in_time → Value expressed in second, if not present the pre-request script defaults to 2 minutes

Build a Postman request

Set the current environment scope to match the environment previously described (default Lissi_Auth)

For each request built through Postman, one must undergo the two following step

Further in this guide is presented the configuration for an entire Postman collection https://docs.lissi.id/lissi-agent/postman-request-authorization#postman_collection

Request authorization

Under the Authorization menu of the built request select the option Bearer Token and set its token value to {{oauth_token}}

Pre-request Script

The following configurable script queries the application token endpoint and refreshes it if necessary. Copy this script under the Pre-request Script menu

JS
// Refresh the OAuth token if necessary
var tokenDate = new Date(2010,1,1);
var tokenTimestamp = pm.environment.get("oauth_timestamp");
if(tokenTimestamp){
  tokenDate = Date.parse(tokenTimestamp);
}
var expiresInTime = pm.environment.get("oauth_expires_in_time");
if(!expiresInTime){
    expiresInTime = 60000 * 2; // Set default expiration time to 2 minutes
}
if((new Date() - tokenDate) >= expiresInTime) 
   pm.sendRequest({
      url:  pm.environment.get("oauth_access_token_url"), 
      method: 'POST', 
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': 191
      },
      body: {
          mode: 'urlencoded',
          urlencoded: [
            {key: "grant_type", value: "password", disabled: false},
            {key: "client_id", value: pm.environment.get("oauth_client_id"), disabled: false},
            {key: "username", value: pm.environment.get("oauth_username"), disabled: false},
            {key: "password", value: pm.environment.get("oauth_password"), disabled: false}
        ]
      }
  }, function (err, res) {
        if (res.json().access_token) {
            pm.environment.set("oauth_token", res.json().access_token);
            pm.environment.set("oauth_timestamp", new Date());
            
            // Set the ExpiresInTime variable to the time given in the response if it exists
            if(res.json().expires_in){
                expiresInTime = res.json().expires_in * 1000;
            }
            pm.environment.set("oauth_expires_in_time", expiresInTime);
        }
  });

Query the Lissi Agent Endpoint

Your request should be successfully authorized !

âž• Further notes

Pre-request script for Postman collection

It is also possible to configure the pre-request script for a complete postman collection. The authorization script will be executed before each request from that collection.

  1. In the collection menu, select the desired Postman collection

  2. In the Authorization menu

    1. Select Bearer Token as an authorization type

    2. Set the Token value to {{oauth_token}}

  3. In the Pre-request Script menu copy the script presented above

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.