
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
- Build and test your request in Postman
- Click the Code button (shown as
</>icon, near the Send button) - Select your target language from the dropdown
- Copy the generated code
Supported Languages
Postman supports code generation for 15+ languages:
| Language | Library | Use Case |
|---|---|---|
| cURL | cURL | Command line, scripts |
| Python | Requests | Backend, scripts |
| JavaScript | Fetch / Axios | Frontend, Node.js |
| Java | OkHttp / HttpClient | Android, enterprise |
| Go | net/http | Backend services |
| PHP | cURL / Guzzle | Web applications |
| Ruby | Net::HTTP | Rails apps |
| C# | RestSharp | .NET applications |
| Swift | URLSession | iOS apps |
| Kotlin | OkHttp | Android 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
- Click the Share button on your collection
- Choose Via Link or Via Email
- Set permissions: View Only or Edit
- Send the link
Share via Export
- Right-click your collection
- Select Export
- Choose Collection v2.1 format
- Save the
.jsonfile - Share via Slack, email, or Git
Import from Team
- Click Import in the top bar
- Drop the
.jsonfile - All requests, folders, variables, and tests are restored
4. Team Workspaces
For teams, Postman offers shared workspaces:
| Workspace Type | Visibility | Best For |
|---|---|---|
| Personal | Only you | Private experiments |
| Team | Team members | Shared API testing |
| Public | Anyone | Open-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
- Code generation converts Postman requests to Python, JavaScript, cURL, and 15+ other languages.
- Export/import JSON files to share collections with anyone.
- Team workspaces let multiple developers collaborate on the same collection.
- Newman runs Postman collections from the command line for CI/CD integration.
- Postman's own API enables programmatic access to collections and environments.
- 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?