Skip to content

Swift Configuration

let client = try Flags.builder()
.withAuth(Auth(
projectId: "your-project-id",
agentId: "your-agent-id",
environmentId: "your-environment-id"
))
.withBaseURL("https://custom-api.flags.gg")
.withMaxRetries(5)
.withErrorCallback { error in
print("Flag error: \(error)")
}
.build()
Method Default Description
withAuth(Auth) — Required. Authentication credentials
withBaseURL(String) https://api.flags.gg Custom API endpoint
withMaxRetries(Int) 3 Max retry attempts
withErrorCallback((FlagsError) -> Void) — Error callback handler
withTargetingKey(String?) Install id Stable user id for percentage rollouts
withMemoryCache() Memory Use in-memory cache
withSQLiteCache(path:) flags_cache.db Use a SQLite cache, so the last values survive a restart (Apple platforms)
withCache(any Cache) — Use your own cache
withContext([String: [String]]) — User attributes for targeting rules. A [String: String] overload is also available
let client = try FlagsClient.builder()
.withAuth(auth)
.withTargetingKey(user?.id) // nil falls back to the install id
.withContext(["email": [user.email], "plan": [user.plan], "groups": user.groups])
.build()
// later, e.g. after login or an upgrade; re-fetches flags
await client.setTargetingKey(user.id)
await client.setContext(["plan": ["pro"]])

isEnabled already accounts for both. See Rollouts & Targeting Rules.

Error Description
httpError Network or HTTP errors
cacheError Cache operation errors
authError Authentication errors
apiError API response errors
builderError Configuration errors

You can use the client with only local flags (environment variables):

let client = try Flags.builder().build()
// Only checks FLAGS_* environment variables
let enabled = await client.is("my-feature").enabled()

The client uses Swift actors for thread safety. All operations are safe for concurrent access without explicit locking.