Better Practices
Published in

Better Practices

APIOps: The art of mixing APIs

A hands-on guide to building API-driven workflows in Postman. Or, how you can combine APIs from Slack, AWS, GitHub, Dropbox and the likes to get things done.

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store