PHP SDK
LIKYLY shows each visitor of your site the products they are most likely to want: “You may also like” on a product page, “Recommended for you” on the home page, “Complete your order” in the cart. It learns from what your visitors actually do.
This is the official PHP client for the LIKYLY API. Your code uses it to:
- Send your catalog: the products, articles or movies to recommend.
- Report what visitors do: they view a product, add it to their cart, sometimes buy it. Viewing alone is useful: a visitor who never buys still teaches LIKYLY which products go together, and gets recommendations.
- Ask what to show: the products to recommend on a page, for a visitor, or both.
- Report what happens to the recommendations: which ones were seen, which ones were clicked. LIKYLY learns what works, and you measure what they bring in.
Installation
composer require likyly/sdkPHP 8.1 or later. Depends on Guzzle 7 (guzzlehttp/guzzle); works in any framework, or none.
Quick start
1. Initialize
Create one client and reuse it. Use your secret key on the server (see Which key?).
<?php
require __DIR__ . '/vendor/autoload.php'; // Composer's autoloader
use Likyly\Exception\NotFoundException;
use Likyly\Exception\RateLimitException;
use Likyly\LikylyClient;
// Backend only: the secret key gives access to your catalog and your users.
$likyly = new LikylyClient(
apiKey: getenv('LIKYLY_SECRET_KEY'),
);2. Send your catalog
// Send your catalog once (and whenever it changes). Ids are YOUR ids: SKUs, UUIDs, Shopify gids...
$likyly->items()->upsertMany([
['itemId' => 'SKU-1', 'title' => 'Nike Air Max', 'description' => 'Running shoe with air cushioning', 'properties' => ['category' => 'shoes', 'brand' => 'Nike', 'price' => 129.9]],
['itemId' => 'SKU-2', 'title' => 'Adidas Ultraboost', 'description' => 'Responsive running shoe', 'properties' => ['category' => 'shoes', 'brand' => 'Adidas', 'price' => 149]],
['itemId' => 'SKU-3', 'title' => 'Nike Pegasus', 'description' => 'Everyday running shoe', 'properties' => ['category' => 'shoes', 'brand' => 'Nike', 'price' => 119]],
]);
// Users are optional: describe them if you want the profile to travel with their events.
$likyly->users()->upsert('user_123', properties: ['country' => 'FR', 'segment' => 'premium']);The ids are yours: any string (SKU-123, a UUID, gid://shopify/Product/123). properties is free-form; category and description improve similarity, the rest is stored as is.
3. Track events
// Tell LIKYLY what your visitors do.
$likyly->events()->view(userId: 'user_123', itemId: 'SKU-1');
// Purchases carry an eventId: replaying the call can never count the sale twice.
$likyly->events()->purchase(
eventId: 'purchase_order_9281_SKU-1',
userId: 'user_123',
itemId: 'SKU-1',
quantity: 1,
properties: ['price' => 129.9, 'currency' => 'EUR', 'orderId' => 'order_9281'],
);4. Get recommendations
// Ask what to show. You never pick an algorithm: LIKYLY chooses the best strategy for what it knows.
$recs = $likyly->recommendations()->get(userId: 'user_123', placement: 'homepage', limit: 3);
echo "strategy: {$recs->strategy}\n";
foreach ($recs->items as $item) {
echo $item->itemId, ' ', $item->title, "\n";
}You never pick an algorithm: LIKYLY selects the best strategy from what it knows about the visitor and tells you which one it used (strategy: hybrid, content, collaborative, session or popular). With no user, no session and no item, you still get a useful answer: what is popular.
5. Placements: the recommended way
// The recommended way: a placement ("pdp-related"...) is configured once in LIKYLY - strategy,
// fallback, filters - and your page only says where it is and what it knows.
$related = $likyly->placements()->recommend('pdp-related', context: ['itemId' => 'SKU-123', 'sessionId' => 'sess_abc'], limit: 4);
echo "strategy used: {$related->strategyUsed}\n";
// When an anonymous visitor signs in, attach their session's history to their account.
$linked = $likyly->events()->identify(userId: 'user_123', sessionId: 'sess_abc');
echo "linked: {$linked->linkedInteractions}\n";A placement (pdp-related, home-for-you, cart-cross-sell…) is set up once in LIKYLY, by your AI assistant through the MCP server or in the console: strategy, fallback, filters, number of items. Your code just names it and passes what it knows (product shown, user, session); the setup can then change without a redeploy. identify attaches an anonymous visitor’s history to their account as soon as they sign in, to keep recommendations personal.
6. Close the loop: attribution
// Send the recommendationId back: LIKYLY measures which recommendations get seen, clicked and bought.
$likyly->events()->impression(userId: 'user_123', itemId: $recs->items[0]->itemId, recommendationId: $recs->recommendationId, placement: 'homepage');
$likyly->events()->click(userId: 'user_123', itemId: $recs->items[0]->itemId, recommendationId: $recs->recommendationId, placement: 'homepage');Send the recommendation id back on the events that follow (impression on display, click, add_to_cart, purchase): that is how LIKYLY measures which recommendations actually convert.
Which key?
| Secret key | Public key | |
|---|---|---|
| Where to use it | on your server only | anywhere, including a web page |
| Recommendations | yes | yes |
| Record events | yes | yes |
| Items and users (create, read, delete) | yes | no (PermissionDeniedException) |
debug explanations | yes | no |
The public key can only read recommendations and write events, so it can be exposed. Never put the secret key in code shipped to a browser or a mobile app.
// Where the code is exposed to visitors, use the PUBLIC key: it can only read recommendations and record events.
$front = new LikylyClient(
apiKey: getenv('LIKYLY_PUBLIC_KEY'),
);
// A visitor who is not logged in: use an anonymous session id (any string you generate and keep in a cookie).
$front->events()->view(sessionId: 'sess_abc', itemId: 'SKU-2');
$forVisitor = $front->recommendations()->get(sessionId: 'sess_abc', viewedItemIds: ['SKU-2'], placement: 'product_page', limit: 3);Events
track is the single mechanism; impression, view, click, add_to_cart, remove_from_cart and purchase are shortcuts to it. Event types are free-form strings: use your own (favorite, share, video_played...) as freely as the official ones.
// Any string is a valid event type: track what matters to your business.
$likyly->events()->track('favorite', itemId: 'SKU-2', userId: 'user_123', properties: ['list' => 'wishlist']);
// Up to 1000 events per call, each with its own type.
$likyly->events()->trackMany([
['type' => 'view', 'userId' => 'user_123', 'itemId' => 'SKU-3'],
['type' => 'add_to_cart', 'userId' => 'user_123', 'itemId' => 'SKU-3', 'quantity' => 1],
]);- An event carries an item and a user, a session, or both (both link the anonymous session to the user once signed in).
- Anonymous visitors: generate a session id (any string) and keep it in a cookie; no sign-in required.
propertiesis a free-form object (price, currency, order number...). Its keys are stored exactly as you send them and never renamed by the SDK.- The event id (idempotency key): replaying an event with the same id records nothing and returns
duplicate: true. Set it on purchases, for examplepurchase_<order>_<item>. placementis a free-form label for where something happened or will be shown (homepage,product_page,cart...).
Advanced recommendations
recommendations.get is the normal way. To pick the strategy yourself:
// Advanced Recommendations: one strategy at a time, when you want to choose.
$similar = $likyly->recommendations()->similar(itemId: 'SKU-1', limit: 3);
$hybrid = $likyly->recommendations()->hybrid(userId: 'user_123', itemId: 'SKU-1', alpha: 0.7, limit: 3);
$session = $likyly->recommendations()->session(viewedItemIds: ['SKU-1', 'SKU-2'], limit: 3);| Strategy | What it does |
|---|---|
popular | The most popular items: the fallback for a visitor with no history |
similar | Items similar to an item (content similarity), no user needed |
collaborative | What users with similar histories liked. Needs a trained model, otherwise a “not found” error |
hybrid | Similar items personalized for a user; alpha (0 to 1) weighs collaborative against content |
session | Weighted by recency, from the items viewed or a user's history (exactly one of the two) |
These endpoints carry the ids in the URL path: an id containing / (like gid://shopify/Product/1) or, for the session list, ,, is rejected with a validation error. In that case use recommendations.get, which takes the ids in the request body.
Errors
Errors are typed (all of them are LikylyException). Catch the ones you care about:
| Error | When |
|---|---|
ValidationException | Invalid request: caught by the SDK before anything is sent, or a 422 from the API (the message names the faulty fields) |
AuthenticationException | 401: missing, invalid or revoked API key |
PermissionDeniedException | 403: for example a public key used for an operation reserved to the secret key, or a plan limit |
NotFoundException | 404 |
RateLimitException | 429: see the retry-after value |
ApiException | Any other error status from the API (the four rows above are subtypes of it) |
NetworkException | No HTTP response: DNS, dropped connection, TLS |
TimeoutException | The request timed out |
Errors from the API carry: $statusCode, $requestId, $retryAfter (seconds), $body. Share the request id when you contact support.
try {
$likyly->items()->get('does-not-exist');
} catch (NotFoundException $e) {
echo 'no such item, request ', $e->requestId, "\n";
} catch (RateLimitException $e) {
echo 'slow down, retry in ', $e->retryAfter, " s\n";
}Retries and timeouts
The SDK retries transient failures for you (2 retries by default, exponential backoff from 0.5 s capped at 8 s, with full jitter):
- 429: always retried, honoring
Retry-After. ARetry-Afterabove 60 s is not waited for: the rate limit error is returned. - 502, 503, 504, dropped connections and timeouts: retried only when replaying the call is safe: reads, upserts, deletes, events that carry an event id, and batches where every event has one.
- An event without an event id is never retried after an ambiguous failure, so a flaky network can never record a purchase twice. Set the id on the events that matter and they become retryable.
- Other 4xx errors are never retried.
Configuration
| Setting | Option | Default |
|---|---|---|
| API key | apiKey | required |
| Base URL | baseUrl | https://api.likyly.com |
| Catalog | catalog | your only catalog |
| Timeout | timeout (seconds) | 10 |
| Retries | maxRetries | 2 |
| User-Agent suffix | userAgent | none |
| Custom HTTP | httpClient (a Guzzle ClientInterface) | a new Guzzle client |
$tuned = new LikylyClient(
apiKey: getenv('LIKYLY_SECRET_KEY'),
timeout: 5.0, // seconds per attempt (default 10)
maxRetries: 3, // automatic retries on 429 / 502 / 503 / 504 / network errors (default 2, 0 disables)
userAgent: 'my-shop/1.4', // appended to the SDK's User-Agent
);Every method takes a last array $options = [] argument: ['timeout' => 2.0, 'maxRetries' => 0].
Reference
Key: S = secret key only, P = public or secret key.
| Method | Effect | Key |
|---|---|---|
$likyly->items()->get | One item by your own id | S |
$likyly->items()->list | One page of the catalog (limit / offset), with the catalog's total | S |
$likyly->items()->upsert | Create or replace an item (idempotent) | S |
$likyly->items()->delete | Remove an item (events already recorded for it are kept) | S |
$likyly->items()->upsertMany | Batch upsert, 1 to 1000 items | S |
$likyly->items()->import | Alias of the batch upsert above (JSON batch) | S |
$likyly->items()->deleteMany | Batch delete, 1 to 1000 ids; unknown ids are reported, not raised | S |
$likyly->users()->get | One user profile | S |
$likyly->users()->list | One page of users (limit / offset) | S |
$likyly->users()->upsert | Create or replace a profile (free-form properties) | S |
$likyly->users()->delete | Erase the profile and every event recorded for that user | S |
$likyly->users()->import | Batch upsert of users, 1 to 1000 | S |
$likyly->events()->track | Record an event of any type (open string) | P |
$likyly->events()->trackMany | Up to 1000 events in one call, each with its own type | P |
$likyly->events()->impression | The item was displayed to the visitor | P |
$likyly->events()->view | The visitor looked at the item | P |
$likyly->events()->click | The visitor clicked the item | P |
$likyly->events()->addToCart | The visitor added the item to their cart | P |
$likyly->events()->removeFromCart | The visitor removed the item from their cart | P |
$likyly->events()->purchase | The visitor bought the item (set an event id) | P |
$likyly->events()->identify | An anonymous visitor signed in: attach their session's history to their user id | P |
$likyly->placements()->recommend | Recommendations for a named placement configured in LIKYLY (the recommended way) | P |
$likyly->recommendations()->get | Recommendations with the automatic strategy: user, session, item or viewed items | P |
$likyly->recommendations()->popular | Advanced: the most popular items | P |
$likyly->recommendations()->similar | Advanced: items similar to one item | P |
$likyly->recommendations()->collaborative | Advanced: what similar users liked (needs a trained model) | P |
$likyly->recommendations()->hybrid | Advanced: similar items personalised for a user (alpha) | P |
$likyly->recommendations()->session | Advanced: from viewed items or a user's view history | P |
Lists are paginated with limit and offset (the API’s default page holds 100 items); the total comes with the page. Users have a batch import but no batch delete: the API has no such endpoint. The items’ import is an alias of the batch upsert (CSV import is a dashboard feature).
PHP notes
- Use named arguments (PHP 8):
events()->view(userId: 'user_123', itemId: 'SKU-1'). Identifiers are strings and are never coerced: withdeclare(strict_types=1)an integer is aTypeError. - Batch methods (
upsertMany,trackMany,users()->import) take arrays of arrays with the samecamelCasekeys as the named arguments. The keys insidepropertiesare yours and are never renamed.
Package: likyly/sdk on Packagist. Full REST reference: api.likyly.com/docs.