September 27, 2020 - 2 min read
For a project where we needed to talk to the Shopware 6 API, I was looking for a PHP SDK for the the API. As it turns out, the Shopware 6 API is based on the OpenAPI v3 specification. This means that we can use swagger-api/swagger-codegen to automatically generate an SDK based on the JSON specification in a large number of languages. We’ll focus on PHP here.
You can find the JSON specification of your shop locally at https://localhost/api/v3/_info/openapi3.json. In development mode this endpoint shows all API paths. In production mode it’ll show a The resource owner or authorization server denied the request
warning; you’ll need a token to get to it.
Run this command and replace the placeholders with your domain and your administation user/pass combo.
curl -XPOST -H 'Content-Type: application/json' https://yourdomain.com/api/oauth/token -d '{
"client_id": "administration",
"grant_type": "password",
"scopes": "write",
"username": "YOUR_USERNAME",
"password": "YOUR_PASSWORD"
}'
This will return a JSON-formatted response with an access token. Replace YOUR_ACCESS_TOKEN
with your access token, set your domain and run this command;
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" -H "Content-Type: application/json" https://yourdomain.com/api/v3/_info/openapi3.json > swagger.json
Since the Shopware 6 API uses the OpenAPI v3 specification, we’ll also need the v3 version of the Swagger codegen CLI. We’ll run it through Swagger’s official Docker image so we won’t have to deal with all the Java dependencies.
Start this command in the directory where your swagger.json
from step 2 lives.
mkdir php-sdk
docker run --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli-v3 generate -i /local/swagger.json -l php -o /local/php-sdk
Congratulations, you now have a fully functional automatically generated PHP SDK for your Shopware 6 instance.
Written by Peter Jaap Blaakmeer @PeterJaap