Swift SDK
Install the Swift SDK, create a client, handle errors, and find every type and method by area.
The Sovaria client for iOS, macOS, tvOS, watchOS and Linux: async/await and Foundation only, with no third-party dependencies. It has the same surface as the Dart, Kotlin, TypeScript and Python clients.
#Install
.package(url: "https://github.com/sovaria/sovaria-swift.git", from: "1.0.0")#Create a client
Use the project URL and the anon key from the console's Connect panel. The anon key is public and safe to ship in an app. Never put the service role key in a mobile app. For a full walkthrough see iOS quickstart (Swift).
import Sovaria
let sovaria = try await Sovaria.initialize(
url: "https://<project-ref>.sovarias.in",
anonKey: "<anon-key>",
storage: KeychainStore() // your SessionStorage; the default keeps the session in memory
)
try await sovaria.auth.signInWithPassword(email: "ada@example.com", password: "Correct-Horse-9!")#Errors
Failed calls throw with the server's machine-readable code (for example PLAN_LIMIT_REACHED or INVALID_QUERY) and a readable message. See Troubleshooting.
do {
_ = try await sovaria.db.sql("select * from orders")
} catch let error as SovariaError {
print(error.code, error.message) // e.g. PLAN_LIMIT_REACHED; error.status is the HTTP status
}#Reference by area
#Client
Sovaria
The Sovaria client for Swift (iOS, macOS, Linux).
public final class Sovaria: Transport, @unchecked SendableSovaria.baseURL
A property.
public let baseURL: StringSovaria.anonKey
The project's public (anon) key: used for every request while nobody is signed in.
public let anonKey: StringSovaria.storage
A property.
public let storage: SessionStorageSovaria.sessionKey
A property.
public let sessionKey: StringSovaria.device
A property.
public let device: SovariaDevice?Sovaria.retries
A property.
public let retries: IntSovaria.retryBaseDelay
A property.
public let retryBaseDelay: TimeIntervalSovaria.refreshMargin
A property.
public let refreshMargin: TimeIntervalSovaria.api
A property.
public let api: GeneratedApiSovaria.auth
A property.
public let auth: SovariaAuthSovaria.db
A property.
public let db: SovariaDatabaseSovaria.files
A property.
public let files: SovariaStorageSovaria.functions
A property.
public let functions: SovariaFunctionsSovaria.realtime
A property.
public let realtime: SovariaRealtimeSovaria.crashes
A property.
public let crashes: SovariaCrashesSovaria.simulate
Simulation (team members): who can do what in the live project, with nothing written.
public let simulate: SovariaSimulationSovaria.push
Push notification tokens for the signed-in user.
public let push: SovariaPushSovaria.collections
Document collections (Firestore-style).
public let collections: SovariaCollectionsSovaria.offline
A property.
public var offline: SovariaOffline?Sovaria.orgId
A property.
public var orgId: String?Sovaria.projectId
A property.
public var projectId: String?Sovaria.envId
A property.
public var envId: String?Sovaria.Sovaria.init
Create an instance.
public init(url: String, anonKey: String, orgId: String? = nil, projectId: String? = nil, envId: String? = nil,
storage: SessionStorage? = nil, device: SovariaDevice? = nil, attestation: AttestationProvider? = nil,
urlSession: URLSession = .shared, retries: Int = 3, retryBaseDelay: TimeInterval = 0.2, refreshMargin: TimeInterval = 60,
storageKey: String = "")Sovaria.initialize
Create the client and restore the stored session (if any).
public static func initialize(url: String, anonKey: String, orgId: String? = nil, projectId: String? = nil,
envId: String? = nil, storage: SessionStorage? = nil, device: SovariaDevice? = nil,
attestation: AttestationProvider? = nil, urlSession: URLSession = .shared) async throws -> SovariaSovaria.setScope
public func setScope(orgId: String? = nil, projectId: String? = nil, envId: String? = nil)Sovaria.enableOffline
Turn on offline-first for the data API: writes made with no network are queued and replayed in order on reconnect; db.select/reads fall back to the last cached result.
public func enableOffline(store: OfflineStore = FileOfflineStore(), namespace: String? = nil,
onSyncError: (@Sendable (SovariaError, [String: Any]) -> Void)? = nil,
onSynced: (@Sendable (Int) -> Void)? = nil) -> SovariaOfflineReturns the controller.
The queue and cache are kept in files in Application Support by default (FileOfflineStore), so writes made offline still sync after the app is closed and reopened. Pass store: MemoryOfflineStore() to keep them for this run only.
Sovaria.scope
public func scope(_ name: String) -> String?Sovaria.resolveScope
Learn the org/project behind the project URL (), once.
public func resolveScope() asyncSovaria.call
public func call(_ method: String, _ path: String, body: Any?, query: [String: Any?]?, auth: Bool) async throws -> Any?Sovaria.request
Send one request and decode the JSON answer.
public func request(_ method: String, _ path: String, body: Any? = nil, query: [String: Any?]? = nil,
auth: Bool = true, headers: [String: String]? = nil, bytes: Data? = nil,
contentType: String? = nil, raw: Bool = false) async throws -> Any?Signs with the session (refreshed near expiry, and once more after a 401) or the anon key; retries transient failures with backoff. Returns parsed JSON, or Data when raw is true.
SovariaError
An error answer from Sovaria (or a client-side problem such as missing configuration).
Mirrors the Dart/TypeScript/Python clients: a machine-readable code (PLAN_LIMIT_REACHED, PKCE_REQUIRED, APP_ATTESTATION_REQUIRED…) plus the HTTP status (0 for network/config errors).
public struct SovariaError: Error, CustomStringConvertibleSovariaError.status
A property.
public let status: IntSovariaError.code
A property.
public let code: StringSovariaError.message
A property.
public let message: StringSovariaError.details
A property.
public let details: Any?SovariaError.traceId
A property.
public let traceId: String?SovariaError.SovariaError.init
Create an instance.
public init(status: Int, message: String, code: String = "", details: Any? = nil, traceId: String? = nil)SovariaError.isUnauthorized
A property.
public var isUnauthorized: BoolSovariaError.isPlanLimit
A property.
public var isPlanLimit: BoolSovariaError.needsAttestation
A property.
public var needsAttestation: BoolSovariaError.description
A property.
public var description: StringSovariaError.config
A client-side configuration problem (no request was made).
public static func config(_ message: String) -> SovariaErrorTransport
What the generated API needs from a client: send one request, and know the default scope.
public protocol Transport: AnyObjectTransport.call
Send method path with an optional JSON body and query; returns the decoded JSON.
func call(_ method: String, _ path: String, body: Any?, query: [String: Any?]?, auth: Bool) async throws -> Any?Transport.scope
The client's default org_id / project_id / env_id, or nil.
func scope(_ name: String) -> String?