Better Practices
Published in

Better Practices

Dynamically create custom environments with code

Reduce time to first call (TTFC) for your public API via Run in Postman

Six Key Things To Know About the Run in Postman Button

You can embed a collection in this tiny, unassuming button.

Users who click on the button can instantaneously bring a collection into their own instance of Postman to start sending API calls right away.

You can bundle an environment along with the collection.

Include the collection with its associated environment so users can fork both at the same time, instead of separately.

You can use the button in your docs.

When you generate the button, choose between a Markdown-friendly or HTML-friendly code snippet to paste in your README or developer docs, for example.

You can customize the form factor.

If you’re allergic to orange or hate the border radius, update the button to suit your own brand theme, or choose any other image.

example of a stylized Run in Postman button in developer docs

The Run in Postman button has its own API.

The Run in Postman API can dynamically inject information as environment variable values into the embedded Run in Postman button. Plug in dynamically-generated data, like access tokens, using your website’s client-side code.

Dynamically Create Postman Environments

Some API publishers generate environments on the fly for their users. For example, an API publisher may already know the users’ access credentials when they are logged into a developer portal. API providers can inject this personalized data into an environment for the user, pair it with the collection, and bundle it all together in a tidy Run in Postman button.

Dynamically inject data into Postman environments

Step 1. Generate the button

In a Postman public workspace, select the collection that you want to embed. Go to the overflow menu, select “Share collection”, and tab over to “Via Run in Postman”.

Generate a live version of the Run in Postman button

Step 2. The HTML elements

In a file called index.html, we create a div element that contains our Run in Postman button. Use the code provided in the first half of the code snippet from the previous step.

<div class="postman-run-button" data-postman-action="collection/fork"  data-postman-var-1="1559979-8aa8a323-6308-429c-90ca-e85d1d0bb33e"  data-postman-collection-url="entityId=1559979-8aa8a323-6308-429c-90ca-e85d1d0bb33e&entityType=collection&workspaceId=ef3cdedd-f58c-4492-9ed0-4dcf456987b2"></div>
<form method="post" onsubmit="return createEnv();">  <label for="secretstring">Passphrase</label>  <input type="text" id="secret-string" name="secretstring"><br><br>  <input type="submit" value="Save"></form>

Step 3. The JavaScript functions

In a separate file called app.js, we define two functions. The first function called createEnv() creates an environment called Passphrase that includes the data submitted by the user in the HTML form. The environment is dynamically created using the Run in Postman API’s _pm() method.

function createEnv() {  var secretString = document.getElementById('secret-string').value,    runButtonIndex = 0,    envData = {      passphrase: secretString  };  _pm('env.create', 'Passphrase', envData, runButtonIndex);  return false;}
(function (p, o, s, t, m, a, n) {  !p[s] && (p[s] = function () { (p[t] || (p[t] = [])).push(arguments); });  !o.getElementById(s + t) && o.getElementsByTagName("head")[0].appendChild((  (n = o.createElement("script")),  ( = s + t), (n.async = 1), (n.src = m), n  ));}(window, document, "_pm", "PostmanRunObject", ""));



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