Export FastAPI applications to Postman collections
Project description
FastAPI Postman Exporter
A Python package to export FastAPI applications to Postman collections using the OpenAPI specification.
Features
- 🚀 Export any FastAPI app to a Postman collection
- 🔧 CLI interface for easy integration
- 📦 Simple Python API for programmatic use
- 🎯 Automatic OpenAPI spec generation
- ✅ Validation and error handling
Prerequisites
Before using this package, you need to install the openapi2postmanv2 CLI tool:
Install Node.js and npm
- Download and install Node.js from nodejs.org
- Verify installation:
node --version npm --version
Install openapi2postmanv2
npm install -g openapi2postmanv2
Verify the installation:
openapi2postmanv2 --version
Installation
From PyPI (recommended)
pip install fastapi-postman-exporter
From source
git clone https://github.com/Akshya107/fastapi-postman-exporter.git
cd fastapi-postman-exporter
pip install -e .
Usage
CLI Usage
The package provides a command-line interface for easy export:
# Basic usage
fastapi-postman-exporter export --app-path main.py --app-name app --output collection.json
# With custom app name
fastapi-postman-exporter export --app-path api/main.py --app-name api_app --output postman_collection.json
# With verbose output
fastapi-postman-exporter export --app-path main.py --app-name app --output collection.json --verbose
CLI Options
--app-path/-p: Path to your FastAPI app file (e.g.,main.py)--app-name/-n: Name of the FastAPI app object in the file (default:app)--output/-o: Path where the Postman collection JSON will be saved--verbose/-v: Enable verbose output
Check CLI Tool Installation
fastapi-postman-exporter check
Python API Usage
You can also use the package programmatically:
from fastapi import FastAPI
from fastapi_postman_exporter import generate_postman_from_app
# Your FastAPI app
app = FastAPI(title="My API", version="1.0.0")
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int):
return {"item_id": item_id}
# Export to Postman collection
generate_postman_from_app(app, "my_api_collection.json")
Example FastAPI App
Here's a simple example of a FastAPI app that can be exported:
# main.py
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI(
title="Sample API",
description="A sample API for demonstration",
version="1.0.0"
)
class Item(BaseModel):
name: str
price: float
is_offer: bool = None
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
@app.post("/items/")
def create_item(item: Item):
return item
@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
return {"item_id": item_id, **item.dict()}
Export this app:
fastapi-postman-exporter export --app-path main.py --app-name app --output sample_api_collection.json
Project Structure
fastapi_postman_exporter/
├── __init__.py # Package initialization
├── exporter.py # Core export functionality
├── cli.py # CLI interface
└── main.py # CLI entry point
Development
Setup Development Environment
git clone https://github.com/Akshya107/fastapi-postman-exporter.git
cd fastapi-postman-exporter
pip install -e ".[dev]"
Run Tests
pytest
Code Formatting
black fastapi_postman_exporter/
isort fastapi_postman_exporter/
Type Checking
mypy fastapi_postman_exporter/
How It Works
- OpenAPI Generation: Uses FastAPI's built-in
openapi()method to generate the OpenAPI specification - Temporary File: Writes the OpenAPI spec to a temporary JSON file
- Conversion: Uses the
openapi2postmanv2CLI tool to convert the OpenAPI spec to a Postman collection - Cleanup: Removes the temporary file and outputs the Postman collection
Error Handling
The package provides helpful error messages for common issues:
- CLI tool not installed: Clear instructions to install
openapi2postmanv2 - App loading errors: Detailed error messages for file not found, invalid app name, etc.
- Conversion errors: Captures and displays subprocess errors from the conversion tool
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
If you encounter any issues or have questions:
- Check the Issues page
- Create a new issue with detailed information about your problem
- Include your FastAPI app code and the exact error message
Changelog
0.1.0
- Initial release
- CLI interface with export and check commands
- Python API for programmatic use
- Support for dynamic FastAPI app loading
- Comprehensive error handling
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_postman_exporter-0.1.1.tar.gz.
File metadata
- Download URL: fastapi_postman_exporter-0.1.1.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a7ea955700b1508a652c67262f16f62692d15f8b111de3afde904b744935eb9f
|
|
| MD5 |
8c0771d5f765b9b64772610013c01e19
|
|
| BLAKE2b-256 |
7a986d73d6d23278371f642683a1554681bd470e13d78712e6ce6ff40d402418
|
File details
Details for the file fastapi_postman_exporter-0.1.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_postman_exporter-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc310b9c530c8915d2e854ae189383d1ff82acd18fa18ab837405a1ca89dca4c
|
|
| MD5 |
25ec73cff81ad15117a4c320e212aeba
|
|
| BLAKE2b-256 |
d7e2249ffedb5116c1d197993448972398f7f11274a15e33aec2473a8dfc2e87
|