# Swift SDK

> One page of the LogDuck documentation. Every page is listed in https://logduck.com/llms.txt, and https://logduck.com/llms-full.txt is all of them in one file.

The official Swift client, for iOS 13+, macOS 10.15+, tvOS, watchOS and Linux. Distributed through Swift Package Manager — there is no registry, the git tag is the release.

## Installation

Add it to the `dependencies` of your `Package.swift`, or use File → Add Package Dependencies in Xcode:

```swift
dependencies: [
    .package(url: "https://github.com/Log-Duck/logduck-swift.git", from: "2.0.0")
]
```

## Minimal example

The fewest settings that work — an API key and a source.

```swift
import LogDuck

let logduck = LogDuckClient(
    options: LogDuckOptions(apiKey: "ld_your_key_here", source: "ios-app")
)

try await logduck.send(LogDuckEvent(type: "order.placed"))
```

## Complete example

Every option, with its default. Only the first two are required; the rest are shown set to the value you get by omitting them.

```swift
import Foundation
import LogDuck

let logduck = LogDuckClient(
    options: LogDuckOptions(
        apiKey: "ld_your_key_here",             // required - your key, starts with `ld_`
        source: "ios-app",                      // required - which app sent this, max 200 chars
        throwOnError: false,                    // optional, default false - true throws instead of returning nil
        retryEnabled: true,                     // optional, default true  - retry once on 5xx, network errors and 429
        maxRetryDelay: 10,                      // optional, default 10    - never block your caller longer, seconds
        timeout: 30,                            // optional, default 30    - per-request timeout, seconds
        baseURL: URL(string: "https://api.logduck.com")!  // optional - only for a self-hosted deployment
    )
)

let orderId = "order_9127"                      // your own identifiers
let sessionId = "sess_8f21c"

let response = try await logduck.send(
    LogDuckEvent(
        type: "order.placed",                   // required - max 100 chars, naming rule below
        subject: orderId,                       // optional - what the event is about, max 500 chars
        sessionId: sessionId,                   // optional - groups related events, max 256 chars
        message: "Order placed on the Pro plan", // optional - the push notification body, max 500
        time: Date(),                           // optional - defaults to when the server received it
        data: ["total": 4999, "currency": "NOK"],  // optional - JSONValue, see the repo README
        emoji: "🛒"                              // optional - shown next to the event, max 10 chars
    )
)

// nil when the send failed and throwOnError is false (the default).
print(response?.eventId ?? "not sent")
```

## Fire and forget

When a failed log line should never affect anything, discard the result — `send` returns `nil` rather than throwing unless you ask it to:

```swift
import LogDuck

let logduck = LogDuckClient(
    options: LogDuckOptions(apiKey: "ld_your_key_here", source: "ios-app")
)

_ = try? await logduck.send(LogDuckEvent(type: "screen.viewed"))
```

> **Note: Why the explicit _ =**
> `send` is `@discardableResult`, but `try?` wraps its result in a second Optional and that attribute does not carry through — so without the discard the compiler warns.

Full reference and changelog: [github.com/Log-Duck/logduck-swift](https://github.com/Log-Duck/logduck-swift)
