Search by

sdk-generator / commercetools-sdk

dmorenof

Unofficial PHP SDK for the commercetools Composable Commerce HTTP API: products, carts, orders, customers, discounts, stores and the rest of a project. Generated from commercetools' published RAML.

Package info

github.com/sdk-generator/commercetools-sdk

pkg:composer/sdk-generator/commercetools-sdk

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.1.1 2026-12-03 20:11 UTC

This package is auto-updated.

Last update: 2026-12-03 22:17:14 UTC


README

License: MIT PHP 8.4+ PHPStan level max Packagist

The commercetools Composable Commerce API as typed PHP, from its own RAML.

Products, product projections, categories, carts, orders, payments, customers, discounts, stores, business units, subscriptions and the rest of a commercetools project, from the HTTP API, with OAuth 2.0 client credentials handled for you.

Unofficial. Not affiliated with, endorsed by or supported by commercetools GmbH. See NOTICE.md.

Why this SDK

  • Generated from a large RAML description. commercetools publishes its API reference in RAML 1.0 (hundreds of types, traits and resource types); the generator reads it as published, discriminated update actions and messages included.
  • Polymorphism kept. RAML discriminators (update actions, messages, price modes...) become discriminated models: the SDK reads each object as its own subtype.
  • OAuth 2.0 client credentials built in. Give the API client's id and secret; the runtime fetches, caches and renews the token.
  • Generated from commercetools' published RAML, never edited by hand. src/ is produced by sdk-generator from the RAML 1.0 API reference described in contract/SOURCE.md, on top of the shared sdk-runtime: when the source changes you regenerate, you do not patch.
  • Typed end to end. 828 operations and 3555 typed request, answer and model classes. A request is a model whose fields are checked where you assign them (PHP 8.4 property hooks); an answer is a model; a list is a typed collection. PHPStan at the maximum level reports no error on the whole SDK.
  • One way to fail. An HTTP answer the contract does not declare as success is an ApiException that keeps the raw response and, when the API documents it, the decoded error as a model. Network failures keep their own type.
  • Production controls included. PSR-18 HTTP client (Guzzle by default), timeouts (30 s / 10 s by default), retries with Retry-After, redirect policy, middleware and per-environment servers, all in one Configuration object.

Install

PHP 8.4 or later.

composer require sdk-generator/commercetools-sdk

Quick start

<?php
use CommercetoolsSDK\ByProjectKey\Categories;
use CommercetoolsSDK\CommercetoolsComposableCommerceAPISDK;
use CommercetoolsSDK\Configuration;
use Dmorenof\SdkRuntime\Security\{OAuth2Client, OAuth2ClientCredentials, OAuth2Endpoints, OAuth2Options, SecurityCredentials};

$region = 'europe-west1.gcp';
$projectKey = getenv('CTP_PROJECT_KEY') ?: 'my-project';

$configuration = new Configuration();
$configuration->serverVariables = ['region' => $region];
$configuration->securityCredentials = new SecurityCredentials([
    'oauth_2_0' => new OAuth2ClientCredentials(
        new OAuth2Endpoints("https://auth.$region.commercetools.com/oauth/token"),
        new OAuth2Client(getenv('CTP_CLIENT_ID') ?: '', getenv('CTP_CLIENT_SECRET') ?: ''),
        new OAuth2Options(scopes: ["view_categories:$projectKey"]),
    ),
]);
CommercetoolsComposableCommerceAPISDK::configure($configuration);

$categories = Categories::get($projectKey);
echo $categories->total ?? count($categories->results), " categories\n";

The same example is in examples/quickstart.php. Every operation is listed in docs/OPERATIONS.md.

What it covers

828 operations. The biggest areas:

Area Operations
{projectKey} 828

Left out (5 operations): the four image uploads (POST .../images of products, variants and in-store product tailoring: a binary body with several possible image types that the runtime does not send yet) and the form-encoded variant of the product projection search (POST /{projectKey}/product-projections/search, marked deprecated in the reference; the GET search is in the SDK). The generator lists them when it runs with --skip-unsupported.

Handling errors

use Dmorenof\SdkRuntime\ApiException;

try {
    // ... any call from the quick start
} catch (ApiException $e) {
    echo $e->response->status, "\n";   // the HTTP status
    echo $e->response->body, "\n";     // the raw body, always kept
    var_dump($e->error);                // the decoded error model, when the API documents one
}

Configuration

Configuration is the only place to set anything: baseUrl, securityCredentials, headers, timeout, connectTimeout, retryPolicy, redirectPolicy, middleware, httpClient (any PSR-18 client), serverVariables. Use Client::connect($configuration) instead of the static facades when you need several independent connections.

Good to know

  • You need a commercetools project and an API client (Merchant Center, Settings, Developer settings) with the scopes of what you call. Use a test project first.
  • The quick start has not been run against a live project (it needs credentials); the SDK is checked against the description: generation, PHPStan at the maximum level and loading of every class.
  • The region is a server variable (us-central1.gcp, europe-west1.gcp, eu-central-1.aws...): set the one your project lives in.
  • Version 0.1.0 is a first release: the API of the SDK follows the source description, and a regenerated one can rename classes. Pin a version.

Documentation

License

MIT, see LICENSE.md. The license covers this SDK's code, not the API, its trademarks or its terms: see NOTICE.md.