← All SDKs

iOS (Swift) SDK

LIKYLY shows each user of your app the products they are most likely to want: “You may also like” on a product page, “Recommended for you” on the home screen. It learns from what your users do, browsing included.

This SDK runs on your users’ phones, so it uses the public key and covers what that key allows. It asks what to show, reports what users do and what happens to the recommendations, and keeps an anonymous id on the device. Your catalog is sent from your server, with a server SDK and the secret key.

iOS / iPadOS 15+, macOS 12+, tvOS 15+, watchOS 8+ · Swift 5.9+ (Xcode 15+) · async/await · no dependencies

Installation

// Xcode: File › Add Package Dependencies… › https://github.com/likyly/likyly-swift
// or in Package.swift:
.package(url: "https://github.com/likyly/likyly-swift", from: "1.0.0")
// target: .product(name: "Likyly", package: "likyly-swift")

Install with Swift Package Manager, straight from GitHub. The package is also listed on the Swift Package Index.

The public key, never the secret key

Anything shipped in an app can be extracted from it. Use the public key (pk_…, Account page, “Public key”): all it can do is ask for recommendations and record events. The SDK rejects a secret key (sk_) or a developer key (lk_) as soon as it is created: you see the error right away, instead of shipping it in a release.

Quick start

1. One client and one session for the whole app

import Likyly

let likyly = try Likyly(apiKey: "pk_live_xxx")   // the PUBLIC key
let session = LikylySession()                     // anonymous id, kept in UserDefaults

2. Show a placement, and report what was seen and tapped

let recs = try await likyly.recommend(
    "pdp-related",                                // placement set up once in LIKYLY
    context: PlacementContext(itemId: "SKU-456", userId: currentUser?.id, sessionId: session.anonymousId),
    limit: 8)

// A card becomes visible (once per card):
try await likyly.events.impression(EventInput(itemId: recs.items[0].itemId, sessionId: session.anonymousId,
                                              recommendationId: recs.recommendationId, placement: recs.placement))
// A card is tapped:
try await likyly.events.click(EventInput(itemId: recs.items[0].itemId, sessionId: session.anonymousId,
                                         recommendationId: recs.recommendationId, placement: recs.placement))

A placement (pdp-related, home-for-you…) is set up once in LIKYLY, by your AI assistant or in the console. Send the recommendationId back with the impression and the click: LIKYLY learns which recommendations work, and you measure what they bring in.

3. What your users do elsewhere in the app

try await likyly.events.view(EventInput(itemId: "SKU-456", sessionId: session.anonymousId))
try await likyly.events.addToCart(EventInput(itemId: "SKU-456", sessionId: session.anonymousId, quantity: 1))
// A purchase: eventId makes sure a retry never counts it twice
try await likyly.events.purchase(EventInput(itemId: "SKU-456", userId: user.id, eventId: "purchase_\(order.id)_SKU-456",
                                            quantity: 1, properties: ["price": 129.9, "currency": "EUR"]))
try await likyly.events.track("favorite", EventInput(itemId: "SKU-456", userId: user.id)) // your own types

Browsing alone is useful: a user who never buys still teaches LIKYLY which products go together, and gets recommendations. Set an eventId on purchases.

4. On sign-in

try await session.identify(likyly, userId: user.id)   // on sign-in
session.reset()                                       // on sign-out

The device’s anonymous history then counts toward the user’s account.

Full example: A complete SwiftUI screen.

If LIKYLY doesn't answer

Placement answers are kept in memory for an hour: coming back to a screen costs nothing. After a network error, a timeout, a 429 or a 5xx error, placement calls fail right away for 30 seconds (a “paused” error): the screen shows its own content instead of waiting. Both can be tuned or turned off:

do {
    recs = try await likyly.recommend("home-personalized", context: ctx)
} catch LikylyError.paused {
    showFallback()                                     // your best sellers, or nothing
}

let likyly = try Likyly(apiKey: "pk_live_xxx", options: LikylyOptions(
    cache: RecommendCacheOptions(ttlSeconds: 600, pauseSeconds: 15)))   // or cache: nil
await likyly.clearRecommendationCache()

Retries: 429, 502, 503, 504 and dropped connections are retried with a growing delay (a Retry-After of up to 60 s is honored). An event without an eventId is never retried after an ambiguous failure, so it is never recorded twice.

Errors

Everything thrown is a LikylyError:

ErrorWhen
validationInvalid call, caught before sending, or a 422 from the API
networkNo HTTP response: offline, DNS, dropped connection
timeoutThe request timed out
pausedLIKYLY failed a few seconds ago: show your fallback content
authentication401: missing, invalid or revoked key
permissionDenied403: for example an operation the public key does not allow
notFound404: for example an unknown placement
rateLimit429: see retryAfter
apiAny other error status (statusCode, requestId)

Configuration

SettingOptionDefault
Catalogcatalogyour only catalog
Timeouttimeout (seconds, per attempt)10
RetriesmaxRetries2
User-Agent suffixuserAgentnone
Recommendation cachecache1 hour; 30 s pause after a failure

Notes

  • Every method also takes options: RequestOptions(timeout:maxRetries:) for a single call, and honors task cancellation (a cancelled .task throws CancellationError).
  • LikylySession is a random id stored in your app's UserDefaults: it is not the IDFA and is never shared with other apps. No App Tracking Transparency prompt is needed; declare "Product Interaction" data in your App Store privacy label.

Package: Likyly on Swift Package Manager. Source code: github.com/likyly/likyly-swift.