Use PHP SDKs generated in Postman
This guide provides instructions for using PHP 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.
SDK structure
The PHP SDK includes the following key files:
src/Client.php— The main SDK client, instantiated with named constructor arguments.src/Services/— Service classes with methods for each API endpoint.src/Models/— Data models (plain PHP classes with typed properties, enums, andfromArray/jsonSerializesupport).src/Exceptions/— Exception classes (ApiExceptionand typed error model exceptions).src/Environment.php— Environment constants with base URL values.src/Retry.php— Guzzle retry middleware.composer.json— Package manifest and autoloading configuration.
The following is an example of the typical structure of a generated PHP SDK:
Example usage
Each generated SDK includes an example/ directory with a working script demonstrating SDK usage.
The example includes the following features:
- SDK initialization with named arguments
- Calling an API endpoint
- Constructing nested model inputs
- Exception handling
Location
Run the example
Example code structure
example/index.php
Import the PHP SDK locally
You can import the generated SDK using Composer from a local path or from Packagist. Below are instructions for each method.
Import using a local path
To import an SDK that hasn’t yet been published to Packagist, add a path repository to your project’s composer.json:
Then run:
Import using Packagist
Once your SDK is published to Packagist, add it as a standard dependency:
Or add it manually to your composer.json:
Then run composer install.
Publish to Packagist
To share your generated PHP SDK with the community, you can publish it to Packagist. This allows other developers to install it using composer require.
Prerequisites
To publish your SDK to Packagist, you need the following:
- A public Git repository (for example, GitHub or GitLab).
- A Packagist account.
- A valid
composer.jsonwith anamefield invendor/packageformat.
Initial setup
-
Push your SDK to a public repository.
-
Log in to Packagist and click Submit.
-
Enter your repository URL and submit. Packagist validates your
composer.jsonand creates the package listing. -
Configure a GitHub webhook so Packagist automatically picks up new versions. In your repository settings, follow the instructions shown on your Packagist package page under Set up GitHub Hook.
Configure composer.json
Your SDK’s composer.json should include the following fields for a proper Packagist listing:
Publish your PHP SDK to Packagist
To publish your SDK, do the following:
-
Tag a release using semantic versioning.
-
Packagist picks up the tag automatically if the webhook is configured. Otherwise, trigger a manual update from your package page.
-
Verify the publication at
https://packagist.org/packages/your-vendor/your-sdk.
Publish updates
To publish updates to your SDK, do the following:
Best practices for publishing PHP SDKs
Use the following best practices when publishing your PHP SDK to Packagist:
- Use semantic versioning.
- Include a comprehensive
README.mdwith installation and usage instructions. - Tag releases in Git rather than relying on branch-based version constraints.
- Declare a specific PHP version constraint in
composer.json(for example,"php": "^8.1"). - Run your test suite before tagging a release.
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. Configure authentication when instantiating the SDK client using named constructor arguments.
The SDK includes authentication support based on the security schemes defined in your API specification. The following examples show the possible authentication methods.
API Key
Bearer Token
Basic Auth
OAuth 2.0
To update credentials after initialization, use the corresponding setter methods. The setApiKey, setAccessToken, setBasicAuth, setClientId, and setClientSecret methods are each only generated when the corresponding auth type is in the specification.
Error handling
The SDK raises ApiException for API errors, which includes details about the status code, response body, and headers. You can catch this exception to handle errors gracefully in your application.
To handle errors, do the following:
To handle specific status codes, do the following:
When the API specification defines error response models, the SDK generates typed exception classes (for example, ValidationErrorException) that extend ApiException and include the deserialized error model. Access the model using getError().
Environment management
The SDK supports multiple environments (for example, production, staging) with different base URLs.
Available environment constants are defined in src/Environment.php.
Hierarchical configuration
PHP supports four levels of configuration: SDK (through the constructor or setBaseUrl()), service, method, and request. Each level overrides the previous, with request being most specific.
Supported configuration keys depend on the auth type defined in the specification:
Timeouts and retries
To set a global timeout, do the following:
To configure retries, pass a retryConfig array to the constructor:
To turn off retries entirely:
Optional and nullable field handling
The SDK uses PHP’s native nullable type syntax (?Type) to represent optional and nullable fields. Optional fields default to null in model constructors.
PHP 8.1 native backed enums are used for fields with a fixed set of allowed values:
Additional resources
Consider the following resources for using and customizing your PHP SDK:
- SDK documentation — Check the
docs/directory in your SDK for per-service and per-model reference pages. - Example usage — Refer to the
example/directory for a working script demonstrating basic SDK usage. - Dependencies — The SDK uses the following dependencies:
- GuzzleHTTP — HTTP client and middleware.
- Symfony Serializer — JSON serialization.
- Symfony PropertyInfo and PropertyAccess — Model introspection.
- PHPDocumentor Reflection Docblock — Type resolution for deserialization.