Use Python SDKs generated in Postman
This guide provides instructions for using Python SDKs generated by Postman SDK Generator. It covers how to import the SDK into your project, run example code, and best practices for using the SDK effectively in your applications.
The Postman SDK Generator creates Python SDKs with type-safe models, service classes for API endpoints, authentication handling, error handling, and comprehensive documentation to help you integrate with your API quickly and efficiently. Models are validated using Pydantic v2, providing runtime type checking, serialization, and clear validation error messages out of the box.
SDK structure
Generated Python SDKs include the following key components:
- Core clients —
sdk.pyfor synchronous operations andsdk_async.pyfor asynchronous work - Service layer — Organized in
services/with async variants inservices/async_/ - Data models — Type-validated classes in
models/powered by Pydantic v2 - Network infrastructure — Located in
net/directory handling configuration, transport, request chains, and authentication - Dependencies — Specified in
pyproject.toml(requests, pydantic >=2.0) The directory structure includes documentation files, example implementations, and installation scripts for both Unix and Windows environments.
The following is an example of the typical structure of a generated Python SDK:
Type safety with Pydantic
Generated models extend Pydantic’s BaseModel, giving you automatic validation, serialization, and clear error messages without any additional setup.
Model validation
Fields are validated at instantiation. Passing an incorrect type raises a ValidationError with a detailed description of what went wrong:
Optional and nullable fields
The SDK distinguishes between fields that are not set and fields explicitly set to None. Only fields you provide are included in API requests:
Serialization
Models serialize to API-compatible dictionaries using model_dump_original(), which applies field aliases, excludes unset fields, and maps Python snake_case names to their original API names:
Extra fields
Models accept additional properties not defined in the schema (useful for forward compatibility). Extra fields are accessible through _kwargs but are excluded from serialization so they’re never sent to the API:
Example usage
Each generated SDK includes an examples/ directory with a working project demonstrating SDK usage.
The example includes the following features:
- Both sync and async usage patterns (async services under
services/async_/) - SDK configuration
- Type-safe API calls
- Error handling with custom exceptions
Location
Run the example
Example code structure
Async example
examples/sample.py
Import the Python SDK locally
You can import the generated SDK into your Python project using pip, Poetry, or by adding it to your PYTHONPATH. Below are instructions for each method.
Import using pip with editable install
To import the SDK using pip in editable mode, do the following:
-
Install the SDK in editable mode.
-
Use the SDK in your code.
Changes to the SDK source reflect immediately, without reinstalling.
Import using pip with file path
To import the SDK using pip with a local file path, do the following:
To import the SDK in requirements.txt, add the following line:
Import by adding to PYTHONPATH
Tom import the SDK by adding it to your PYTHONPATH, do the following:
-
Set the
PYTHONPATHenvironment variable: -
Use the SDK in your code:
Import using Poetry
To import the SDK using Poetry, add it as a dependency with a local path:
To import the SDK in pyproject.toml, add the following line to the [tool.poetry.dependencies] section:
Publish to PyPI
To share your generated Python SDK with the community, you can publish it to PyPI. This allows other developers to easily install it using pip. Follow the steps below to publish your SDK to PyPI.
Prerequisites
To publish your SDK to PyPI, you need the following:
- PyPI account (create at pypi.org)
- Twine installed:
pip install twine
Configure pyproject.toml
Ensure your SDK’s pyproject.toml is properly configured:
Publish your Python SDK to PyPI
To publish your SDK to PyPI, do the following:
-
Create PyPI account and API token:
- Register at pypi.org
- Go to Account Settings > API tokens
- Create a token with a scope of the entire account or specific project.
-
Configure the credentials. You can use Twine’s recommended method of storing credentials in
~/.pypirc: -
Build distribution packages:
This creates
dist/directory with:your-sdk-1.0.0.tar.gz(source distribution)your_sdk-1.0.0-py3-none-any.whl(wheel distribution)
-
Check the package:
-
Test on TestPyPI first (recommended):
-
Publish to PyPI:
-
Verify publication:
Publish updates
-
Update the version in
pyproject.toml:Alternatively, if using dynamic versioning, you can set up your
pyproject.tomlto read the version from your SDK package: -
Clean and rebuild:
Best practices for publishing Python SDKs
Use the following best practices when publishing your Python SDK to PyPI:
- Use semantic versioning (
MAJOR.MINOR.PATCH). - Test on TestPyPI before publishing to PyPI.
- Include a comprehensive
README.mdwith examples. - Add a
LICENSEfile. - Use type hints for better IDE support.
- Include a
py.typedfile for PEP 561 compliance. - Tag releases in Git:
git tag v1.0.0 && git push --tags. - Set up GitHub Actions for automated testing and publishing.
The following is an example GitHub Actions workflow (.github/workflows/publish.yml).
Advanced usage
You can further customize the SDK by modifying the generated code, adding new features, or integrating it with your existing codebase. Here are some advanced topics to explore.
Authentication
The SDK supports various authentication methods. You can configure authentication when initializing the SDK client.
API Key
Bearer Token
Basic Auth
OAuth 2.0 (Client Credentials)
OAuth 2.0 (Access Token)
To update the credentials after initialization, run the following:
Environment management
The SDK supports multiple environments (for example, production, staging) with different base URLs.
Error handling
The SDK raises custom exceptions for API errors, which include details about the error message, status code, and response body. You can catch these exceptions to handle errors gracefully in your application.
To handle errors, do the following:
To handle specific error codes, do the following:
Timeouts and retries
To set a global timeout, do the following:
To set a per-request timeout, do the following:
To pass the retry configuration through SdkConfig at SDK, service, or request level, do the following:
Additional resources
Consider the following resources for using and customizing your Python SDK:
- SDK documentation — Check the
documentation/directory in your SDK for detailed docs on services, models, and code snippets. - Example usage — Refer to the
examples/directory for working code samples demonstrating how to use the SDK in different scenarios. - Type hints — All methods in the SDK include type hints for better IDE support and code completion.
- Dependencies — The SDK uses
requestsfor HTTP requests andpydanticfor data validation. Make sure to install these dependencies when using the SDK.