Create custom monitoring dashboards for SP-API apps in Postman

by Tsuneki U., Solution Reliability Engineer, Selling Partner Developer Services | January 9, 2023

Overview

This blog describes how to use Postman to create a custom monitoring dashboard for Selling Partner API (SP-API) apps. The monitoring feature in Postman allows you to set up regular, automated checks on your API endpoints and provides a convenient and effective way to keep track of the health and performance of the API.

Benefits

Creating a custom monitoring dashboard can help you simplify the following tasks:

  • Automate periodic SP-API app health and performance checks
  • Receive outage notifications promptly
  • Identify API specification response changes
  • Test complex, multi-step workflows
  • Gain visibility into API performance over time and identify trends

The following images show an example dashboard its test results.

526

Monitoring dashboard example

1001

Test results example

Tutorial

This tutorial takes approximately 1 hour to complete.

Prerequisites

Step 1. Create a workflow for an API operation

Note: In this blog, we use CatalogItems API v2022-04-01 as an example.

  1. Open your Postman Collection.
  2. From the top-right corner, select Environment quick look.
  3. Choose Add.
  4. Enter the name for your Environment.
728

Create the environment for the health dashboard

  1. From the top-right corner, select Manage Environments.
  2. Choose your Environment.
  3. Choose Edit.
  4. Enter the variables from the following Environment values table.
  5. Choose Save.

📘

Note:

The secret variable type allows masking of initial and current values which reduces manual copy/paste effort and prevents the unintentional disclosure of sensitive information. Setting a variable to the secret type masks its value to all workspace members. You can use the environment variables in your requests by enclosing the variable name in double curly braces (for example, {{variable_name}}). When you send a request, Postman will substitute the variable with the corresponding value from your environment.

Environment values:

VariableTypeInitialValue and CurrentValue
IamUserAccessKeySecretAWS IAM User AccessKey
IamUserSecretKeySecretAWS IAM User SecretKey
IamRoleARNSecretAWS IAM Role ARN
AppClientIdSecretLWA client ID of your application
AppClientSecretSecretLWA client secret of your application
sts-urlDefault
https://sts.amazonaws.com
auth-urlDefaulthttps://api.amazon.com/auth/o2/token
sp-api-url-prodDefaulthttps://sellingpartnerapi-na.amazon.com
1017

Example environment variables

Step 2. Create an AWS Security Token Service (AWS STS) request.

  1. Select the view more menu to the right of your new collection.
  2. Choose Add request.
  3. Enter a name for the request.
  4. Choose GET.
  5. In the request URL field, enter the URL for the STS endpoint. Note: The endpoint should use the {{ sts-url }} format.
  6. Choose the Params tab.
  7. Enter the values from the following Params table.
  8. Choose the Authorization tab.
  9. Enter the values from the following Authorization table.
  10. Choose Send.
752

Example of STS request Params tab

Params:

KEYVALUE
Version2011-06-15
ActionAssumeRole
RoleSessionNameSP-APIHealthDashBoard
RoleArn{{IamRoleARN}}
DurationSections3600

Authorization:

TypeAWS Signature
AccessKey{{IamUserAccessKey}}
SecretKey{{IamUserSecretKey}}
AWS Regionus-east-1
Service Namests
  1. Choose the Tests tab.
    1. Include a script that sets the temporary AccessKeyId, temporary SecretKey, and SessionToken to the Local variables to be used in the getCatalogItem call.

The local variable is scoped to single request or collection run and becomes unavailable after the request or run is complete.

713

Example Tests tab

Example test:

var jsonObject = xml2Json(responseBody);
pm.variables.set("SessionToken", jsonObject.AssumeRoleResponse.AssumeRoleResult.Credentials.SessionToken);
pm.variables.set("TempAccessKeyId", jsonObject.AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId);
pm.variables.set("TempSecretKey", jsonObject.AssumeRoleResponse.AssumeRoleResult.Credentials.SecretAccessKey);
  1. Select the view more menu to the right of your new collection.
  2. Choose Add request.
  3. Enter a name for the request.
  4. Select GET.
  5. Enter the URL of the getCatalogItem endpoint, which should be in the format of {{ sp-api-url-prod }}/catalog/2022-04-01/items/:asin. Note: The getCatalogItem endpoint requires query parameters, path parameters, authorization and access token in the header.
  6. Enter the following values into the Params tab, Authorization tab, and Header tab.

Query Params:

KEYVALUE
marketplaceIdsATVPDKIKX0DER
includeDatasummaries

Path Params:

KEYVALUE
asinB00QSR9URI

Authorization:

TypeAWS Signature
AccessKey{{TempAccessKeyId}}
SecretKey{{TempSecretKey}}
AWS Regionus-east-1
Service Nameexecute-api
Session Token{{SessionToken}}

Headers:

KEYVALUE
x-amz-access-token{{currentAccessToken}}
  1. Choose the Pre-request tab.
  2. Enter the following script. This script references the Environment variables and sets the LWA access token to “currentAccessToken” local variable which is referred by x-amz-access-token in the Headers tab.

Pre-request Script:

var clientId = pm.environment.get('AppClientId');
var clientSecret = pm.environment.get('AppClientSecret');
var refreshToken = pm.environment.get('AppRefreshToken');
var authURL = pm.environment.get('auth-url');
 
const echoPostRequest = {
  url: authURL,
  method: 'POST',
  header: 'Content-Type:application/json',
  body: {
    mode: 'application/json',
    raw: JSON.stringify(
        {
            grant_type: 'refresh_token',
            refresh_token: refreshToken,
            client_id: clientId,
            client_secret: clientSecret
        })
  }
};
 
var token = pm.variables.get('currentAccessToken');
var tokenExpiry = pm.variables.get('accessTokenExpiry');
 
if (!token || !tokenExpiry || tokenExpiry <= (new Date()).getTime()) {
    pm.sendRequest(echoPostRequest, function (err, res) {
      console.log(err ? err : res.json());
 
      if (err === null && res.status != 'Unauthorized') {
          console.log('Saving new token and expiry');
 
          var responseJson = res.json();
          pm.variables.set('currentAccessToken', responseJson.access_token)
 
          var expiryDate = new Date();
          expiryDate.setSeconds(expiryDate.getSeconds() + responseJson.expires_in);
          pm.variables.set('accessTokenExpiry', expiryDate.getTime());
      }
    });
}
else {
    console.log("Token still good");
}

Step 3. Write a test for the API operation

  1. Choose the Tests tab.
  2. Enter the following test code snippets. This code tests if the response has a status code of 200 and includes expected attributes for ItemSummaries.
620

Test script for getCatalogItem

Tests:

pm.test('Status code should be 200', ()=> {
  pm.response.to.have.status(200);
})
 
var jsonData = pm.response.json()
pm.test('getCatalogItem summary includes expected attributes', ()=> {
 
pm.expect(jsonData.summaries[0]).to.deep.include.keys([
    'marketplaceId',
    'brand',
    'browseClassification',
    'itemName',
    'manufacturer',
    'modelNumber',
    'partNumber',
    'size'])
})
  1. Select the view more menu next to the collection.
  2. Choose Run collection.
  3. Confirm that the test passes.

Step 4. Start the monitor in Postman

  1. Select the view more menu next to the collection.
  2. Choose Monitor collection.
  3. Enter the monitor name, frequency for running the monitor, and email address for notifications.
  4. Specify the environment for the monitor.
  5. Choose Create monitor.
366

Create a monitor

382

Monitor customizations

Step 5. Check API status

The monitor screen allows you to view the results of each API call as well as the workflow execution log. If there are variables you wish to check, we recommend using console.log.

526

Monitoring screen

1001

Test results

Conclusion

This blog introduced how to use Postman's testing and monitoring features to periodically examine the health of the SP-API. It also explained the benefits of monitoring the health of the SP-API on the client side, which can speed up debugging and make application operations and maintenance more efficient.

For more information, refer to:

👍

Have feedback on this post?

If you have questions or feedback on this post, we'd like to hear from you! Please vote and leave a comment using the tools at the bottom of this page.

Subscribe to updates via RSS feed.