Swift SDK
Use Entitler from your Swift apps and servers.
The Swift SDK has two clients: EntitlerServer for server-side Swift, with a secret key, and EntitlerClient for apps, with a customer token or an identity token. Its source is at github.com/entitlerhq/entitler-swift.
Install
In Xcode, choose File, Add Package Dependencies, and enter https://github.com/entitlerhq/entitler-swift. In a Package.swift, add the same address as a dependency.
In your app
import Entitler
let entitler = try EntitlerClient(tokenProvider: { try await api.entitlerToken() })
let canUsePrioritySupport = await entitler.me.isEntitled(to: "priority_support", default: false)
let check = try await entitler.me.check("priority_support")
let entitlements = try await entitler.me.entitlements()
try await entitler.me.recordUsage(of: "ai_credits", amount: 1, idempotencyKey: "message-\(messageID)")tokenProvider asks your server for a customer token; the client calls it when it has none, and again when Entitler refuses the one it has. me is the customer the token was minted for.
Holds need a token with usage:write:
let summary = try await entitler.me.withHold(of: "ai_credits", amount: 500, idempotencyKey: "summary-\(jobID)") { hold in
let answer = try await summarise(document)
try hold.use(answer.tokens)
return answer.summary
}With an identity token
When your app signs users in with an identity provider registered on your project, use a publishable key and the user's identity token, and register the customer on sign-in:
let entitler = try EntitlerClient(key: publishableKey, identityTokenProvider: { try await signIn.currentIDToken() })
try await entitler.register()On your server
let entitler = try EntitlerServer(key: key)
let ada = try entitler.customer("ada")
let check = try await ada.check("priority_support")Errors
The SDK throws EntitlerError. A refusal from the API is .api(APIError), with status, code, reason, parameters, requestID and retryAfter in seconds; .connection and .timeout mean Entitler couldn't be reached. See Errors.
Here a report for a customer who isn't registered yet registers them, then sends the same report again, with the same idempotency key.
func report() async throws {
try await ada.recordUsage(of: "ai_credits", amount: 3, idempotencyKey: "run-42")
}
do {
try await report()
} catch EntitlerError.api(let error) where error.reason == .customerNotFound {
try await registerAda()
try await report()
}Feature keys
Plain strings work everywhere a feature is named. The SDK's generator plugin can also write constants for your features, such as Features.aiCredits, so a typo fails to compile.