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.
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, and fromArray/jsonSerialize support).src/Exceptions/ — Exception classes (ApiException and 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:
Each generated SDK includes an example/ directory with a working script demonstrating SDK usage.
The example includes the following features:
example/index.php
You can import the generated SDK using Composer from a local path or from Packagist. Below are instructions for each method.
To import an SDK that hasn’t yet been published to Packagist, add a path repository to your project’s composer.json:
Then run:
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.
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.
To publish your SDK to Packagist, you need the following:
composer.json with a name field in vendor/package format.Push your SDK to a public repository.
Log in to Packagist and click Submit.
Enter your repository URL and submit. Packagist validates your composer.json and 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.
Your SDK’s composer.json should include the following fields for a proper Packagist listing:
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.
To publish updates to your SDK, do the following:
Use the following best practices when publishing your PHP SDK to Packagist:
README.md with installation and usage instructions.composer.json (for example, "php": "^8.1").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.
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.
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.
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().
The SDK supports multiple environments (for example, production, staging) with different base URLs.
Available environment constants are defined in src/Environment.php.
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:
To set a global timeout, do the following:
To configure retries, pass a retryConfig array to the constructor:
To turn off retries entirely:
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:
Consider the following resources for using and customizing your PHP SDK:
docs/ directory in your SDK for per-service and per-model reference pages.example/ directory for a working script demonstrating basic SDK usage.