commercetools/commercetools-api-reference
Official API reference for the commercetools platform. Includes models, endpoints, schemas, and examples to help you explore and integrate commercetools services, keeping your client implementations aligned with the latest API definitions.
Installation Clone or include the package via Composer (if available) or use the official commercetools API reference docs as a standalone guide. Since this is a documentation package, no direct Composer installation exists—focus on integrating the API reference into your workflow:
# No direct install, but reference the docs:
composer require --dev commercetools/commercetools-api-reference # Hypothetical (check for updates)
First Use Case
GET /{projectKey}/products.Http::get()) to call the API with your auth token:
$response = Http::withToken($commercetoolsAuthToken)
->get("https://api.europe-west1.gcp.commercetools.com/{projectKey}/products");
Where to Look First
spatie/laravel-commercetools for SDK-level integration (if available).CRUD Operations
$productData = [
'name' => ['en' => 'Laravel Hoodie'],
'masterVariant' => ['sku' => 'LARAVEL-001', 'prices' => [...]],
];
$response = Http::post("/{projectKey}/products", $productData);
$patchOps = [
['op' => 'replace', 'path' => '/name/en', 'value' => 'Updated Hoodie']
];
$response = Http::patch("/{projectKey}/products/{productId}", $patchOps);
Pagination & Filtering
?limit=100&where=name=en%22Laravel%22 in your Laravel requests.$products = Http::get("/{projectKey}/products", [
'limit' => 50,
'where' => 'categories.id=some-category-id'
]);
Webhooks for Real-Time Updates
// In a service class
public function handleWebhook($payload) {
event(new \App\Events\CommercetoolsWebhookReceived($payload));
}
// app/Providers/CommercetoolsServiceProvider.php
public function register() {
$this->app->singleton('commercetools', function () {
return new CommercetoolsClient(config('commercetools'));
});
}
$products = Cache::remember("commercetools_products_{$projectKey}", now()->addHours(1), function () {
return Http::get("/{projectKey}/products")->json();
});
try {
$response = Http::get('/products')->throw();
} catch (\Illuminate\Http\Client\ConnectionException $e) {
throw new \App\Exceptions\CommercetoolsApiException('Connection failed', $e);
}
Authentication Quirks
// Refresh token if expired
if (Carbon::parse($token['expires_at'])->isPast()) {
$token = $this->refreshCommercetoolsToken();
}
{projectKey} in endpoints—404 errors often stem from this.Rate Limiting
429 Too Many Requests:
$response = Http::retry(3, 100)->get('/products'); // Retry with delay
Localization Pitfalls
en-US) match your data model. Use setlocale() or constants:
const LOCALE_EN_US = 'en-US';
Http::withOptions(['debug' => true])->get('/products');
Custom API Clients
X-Commercetools-Api-Version: 2023-05).// app/Http/Middleware/CommercetoolsAuth.php
public function handle($request, Closure $next) {
$request->headers->set('Authorization', 'Bearer '.$this->getToken());
return $next($request);
}
Local Development
commercetools/sdk for local testing:
composer require commercetools/sdk
Http::fake():
Http::fake([
'api.commercetools.com/*' => Http::response(['data' => [...]], 200),
]);
Webhook Validation
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
$signature = $request->header('X-Commercetools-Signature');
$expected = hash_hmac('sha256', $request->getContent(), config('commercetools.webhook_secret'));
if (!hash_equals($signature, $expected)) {
abort(401);
}
How can I help you explore Laravel packages today?