Install
Quickstart
Create an item, then list stock on hand:Authentication
Pass an API key or an OAuth access token as the first argument. Create an API key in the app under Settings > Access > API keys. The key acts as the member who made it, and a call needs both the member’s permission and one of the key’s scopes. For an OAuth token that expires, pass a closure. The client calls it before each request:
The client and factories are found with php-http/discovery when you leave them out.
Making calls
Each area of the API has a method, such ascatalog() or inventory(), and each operation a method on it. Path parameters come first, then the body, then the query string:
GET /catalog/items/{item}. You can also call a path directly:
null, a CSV export its text and a file the PSR-7 response.
To check your own code against the types, import them from Schemas in a PHPStan doc comment:
Pagination
A list that can grow returns one page at a time. Each paged list has a second method ending inAll that follows the cursors and yields each record:
$stockshift->paginate('/catalog/items') does the same by path. To page by hand, send each page’s next_cursor back as the after query parameter until it is null.
Errors
A failed call throws anApiException that holds the RFC 9457 problem details body. Branch on errorCode, which is stable. The detail is written for people and may change.
ApiException also has status, problem and response. A call that gets no answer, because the network failed, throws a ConnectionException. Both extend StockShiftException.
Retries and idempotency keys
The client retries a429 after the time in Retry-After. It does this for every method, because the API did not run the call. If Retry-After is longer than maxRetryAfter, it throws instead.
A 5xx or a network failure is retried for GET, PUT and DELETE, with a short backoff. A POST or PATCH is retried only when you give it an idempotency key, so a retry cannot make the change twice:
Webhooks
Check theStockShift-Signature header before you trust a delivery. Webhook::verify() needs the raw body, exactly as it arrived:
tolerance, in seconds, to change that. Retries of an event keep its id, so store it and skip events you have handled.
Each event has a PHPStan type in Schemas, such as InventoryStockAdjustedEvent. To test your own receiver, Webhook::sign() signs a payload the same way StockShift does.
Versions
The major version moves when the API changes in a way that can break your code, such as a removed field or a new required parameter. New endpoints and fields come in minor versions.StockShift\Sdk\Generated\Api::VERSION holds the version of the API reference the package was built from.