Better Practices

For individual engineers to the largest teams, Better Practices is intended to distill knowledge from the Postman community. This is a place to learn about modern software practices together! Read more: https://medium.com/better-practices/introducing-better-practices-e9cf14cf0c88

APIOps: The art of mixing APIs

Kaustav Das Modak
Better Practices
Published in
11 min readMay 28, 2019

--

Photo by Barn Images on Unsplash

API-first Ops is for everyone

At Postman, we’re (web)hooked to Slack

Searching for “Slack” in the Templates listing inside the app shows templates that do something with Slack. You can access this by Clicking “New” → “Templates” and typing in “Slack” in the search bar.

Four aspects of APIOps

APIOps with Postman

A template for TL;DR

Documentation for the APIOps 101 collection

Collections — running and sharing them

Bootcamp is an in-built learning tool built into the Postman app. The left sidebar in the image shows the collections organised in the current workspace.

Variables and their scopes

Variable scopes in Postman

Use local variables to transfer data between requests

// Prepare the body for the Slack message
const slackBody = {
"text": "*Title here*",
"mrkdwn": true,
"attachments": [
{
"title": "Field name",
"text": "Some value"
},
{
"title": "Another field name",
"text": "Some more value"
}
]
};
pm.variables.set("slackBody", JSON.stringify(slackBody));
Setting the {{slackBody}} variable in the body of the request

Use environment with Postman API to store states

Postman API documentation page showing the “Update Environment” endpoint.
A pre-request script to convert the current environment keys into the data structure that the Postman API will accept for the “Update Environment” endpoint.

Control execution order with postman.setNextRequest()

// Assuming "results" in a stringified array
const results = JSON.parse(pm.environment.get("results"));
// Get the last item of the array
const currentResult = results.pop();
for (const item of currentResult) {
// ... perform your action
}
// If there are more entries, loop on the same request.
// This is the terminating condition.
if (results.length) {
// Stringify the remaining array items
pm.environment.set("results", JSON.stringify(results));
// Set the next request to the current one
postman.setNextRequest("Post to Airtable");
}
// Assuming `postsForSlack` is an array of entries,
// if there are any new posts to notify via Slack,
// call the next request, otherwise terminate collection run
if (!postsForSlack) {
postman.setNextRequest(null);
console.log("No new articles found.");
}

Write tests and set expectations

Expectations in tests using pm.test() and pm.expect() methods, with snippets on the right.

--

--

Better Practices
Better Practices

Published in Better Practices

For individual engineers to the largest teams, Better Practices is intended to distill knowledge from the Postman community. This is a place to learn about modern software practices together! Read more: https://medium.com/better-practices/introducing-better-practices-e9cf14cf0c88

Kaustav Das Modak
Kaustav Das Modak

No responses yet