Postman Code Generation and Sharing

Postman Code Generation and Sharing

Convert Postman requests into code for any programming language. Learn to generate cURL, Python, JavaScript, and more, plus sharing and collaboration best practices.

Postman Code Generation and Sharing

One of Postman's most powerful features is code generation — converting any request you build into working code in dozens of programming languages. This bridges the gap between API testing and actual application development.


1. Generating Code from Requests

How to Generate Code

  1. Build and test your request in Postman
  2. Click the Code button (shown as </> icon, near the Send button)
  3. Select your target language from the dropdown
  4. Copy the generated code

Supported Languages

Postman supports code generation for 15+ languages:

LanguageLibraryUse Case
cURLcURLCommand line, scripts
PythonRequestsBackend, scripts
JavaScriptFetch / AxiosFrontend, Node.js
JavaOkHttp / HttpClientAndroid, enterprise
Gonet/httpBackend services
PHPcURL / GuzzleWeb applications
RubyNet::HTTPRails apps
C#RestSharp.NET applications
SwiftURLSessioniOS apps
KotlinOkHttpAndroid apps

2. Code Examples

Let us see how the same POST request looks in different languages.

The Postman request:

  • Method: POST
  • URL: https://jsonplaceholder.typicode.com/posts
  • Header: Content-Type: application/json
  • Body: {"title": "Test", "body": "Content", "userId": 1}

cURL

curl --location 'https://jsonplaceholder.typicode.com/posts' \
--header 'Content-Type: application/json' \
--data '{
    "title": "Test",
    "body": "Content",
    "userId": 1
}'

Python (requests)

import requests
import json

url = "https://jsonplaceholder.typicode.com/posts"
payload = json.dumps({
    "title": "Test",
    "body": "Content",
    "userId": 1
})
headers = {
    'Content-Type': 'application/json'
}

response = requests.post(url, headers=headers, data=payload)
print(response.json())

JavaScript (Fetch)

const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");

const raw = JSON.stringify({
    "title": "Test",
    "body": "Content",
    "userId": 1
});

const requestOptions = {
    method: "POST",
    headers: myHeaders,
    body: raw,
};

fetch("https://jsonplaceholder.typicode.com/posts", requestOptions)
    .then(response => response.json())
    .then(result => console.log(result))
    .catch(error => console.error("Error:", error));

JavaScript (Axios)

const axios = require('axios');

const data = JSON.stringify({
    "title": "Test",
    "body": "Content",
    "userId": 1
});

const config = {
    method: 'post',
    url: 'https://jsonplaceholder.typicode.com/posts',
    headers: { 'Content-Type': 'application/json' },
    data: data
};

axios(config)
    .then(response => console.log(response.data))
    .catch(error => console.error(error));

3. Sharing Collections

Share via Email/Link

  1. Click the Share button on your collection
  2. Choose Via Link or Via Email
  3. Set permissions: View Only or Edit
  4. Send the link

Share via Export

  1. Right-click your collection
  2. Select Export
  3. Choose Collection v2.1 format
  4. Save the .json file
  5. Share via Slack, email, or Git

Import from Team

  1. Click Import in the top bar
  2. Drop the .json file
  3. All requests, folders, variables, and tests are restored

4. Team Workspaces

For teams, Postman offers shared workspaces:

Workspace TypeVisibilityBest For
PersonalOnly youPrivate experiments
TeamTeam membersShared API testing
PublicAnyoneOpen-source projects

Team Workflow

graph TD
    A["Developer A"] --> W["Shared Workspace"]
    B["Developer B"] --> W
    C["QA Engineer"] --> W

    W --> D["Shared Collections"]
    W --> E["Shared Environments"]
    W --> F["Shared Documentation"]

    D --> G["Everyone sees the same requests"]
    E --> H["Dev, Staging, Prod configs"]
    F --> I["Auto-updated API docs"]

    style W fill:#4f46e5,color:#fff
    style G fill:#059669,color:#fff
    style H fill:#059669,color:#fff
    style I fill:#059669,color:#fff

5. Postman API (Using Postman Programmatically)

Postman itself has a REST API that you can use to manage collections programmatically:

# Get all collections
curl -H "X-Api-Key: PMAK-your-key-here" \
  https://api.getpostman.com/collections

# Get a specific collection
curl -H "X-Api-Key: PMAK-your-key-here" \
  https://api.getpostman.com/collections/COLLECTION_ID

# Get all environments
curl -H "X-Api-Key: PMAK-your-key-here" \
  https://api.getpostman.com/environments

This enables:

  • CI/CD integration
  • Automated collection updates
  • Custom dashboards

6. Newman: Running Postman Collections in CI/CD

Newman is the command-line companion for Postman. It runs collections without the GUI:

# Install Newman
npm install -g newman

# Run a collection
newman run my-collection.json

# Run with an environment
newman run my-collection.json -e production.json

# Run with reporters
newman run my-collection.json -r cli,html

This lets you run Postman tests in CI/CD pipelines (GitHub Actions, Jenkins, etc.):

# .github/workflows/api-tests.yml
name: API Tests
on: push
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: npm install -g newman
      - run: newman run postman/collection.json -e postman/env.json

Summary and Key Takeaways

  1. Code generation converts Postman requests to Python, JavaScript, cURL, and 15+ other languages.
  2. Export/import JSON files to share collections with anyone.
  3. Team workspaces let multiple developers collaborate on the same collection.
  4. Newman runs Postman collections from the command line for CI/CD integration.
  5. Postman's own API enables programmatic access to collections and environments.
  6. The workflow is: Test in Postman → Generate code → Implement in app → Automate with Newman.

Lesson Review Quiz

?Knowledge Check

What is Newman?

?Knowledge Check

How do you convert a Postman request into Python code?

?Knowledge Check

What is the best way to integrate Postman tests into a CI/CD pipeline?

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn