> ## Documentation Index
> Fetch the complete documentation index at: https://docs.0xkey.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Swift SDK

> Build embedded wallets on iOS and macOS with the 0xkey Swift SDK: passkey and email OTP sign-in, sessions, wallets, signing, and policy management.

The [0xkey Swift SDK](https://github.com/0xkey-io/sdk-swift) is a Swift Package for embedded wallets on iOS and macOS. Current version: **0.1.0**.

* iOS 17+ / macOS 14+, Xcode 16+ (Swift 6 language mode)
* No third-party dependencies
* Same wire format, stamps and HPKE parameters as the JS and Kotlin SDKs, so they can be used side by side on one organization

| Product | Purpose |
| :- | :- |
| `ZeroXKey` | `ZeroXKeyContext`: sign-in, sessions, wallets, signing, import / export. Re-exports the modules below. |
| `ZeroXKeyHttp` | `ZeroXKeyClient`: every public API endpoint and Auth Proxy route, activity polling, structured errors |
| `ZeroXKeyPasskeys` | `PlatformPasskeyProvider`, `PasskeyStamper` |
| `ZeroXKeyStamper` | API key, WebAuthn and attested request stamps |
| `ZeroXKeyCrypto` | P-256, HPKE, export / import / OTP bundles |
| `ZeroXKeyEncoding` | hex, base64url, base58 |

Most apps only need the `ZeroXKey` product.

## Install

The package is public; no GitHub credentials are needed.

**Xcode:** *File → Add Package Dependencies…*, enter `https://github.com/0xkey-io/sdk-swift.git`, and choose *Up to Next Minor Version* from `0.1.0`.

**Package.swift:**

```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
dependencies: [
    .package(url: "https://github.com/0xkey-io/sdk-swift.git", .upToNextMinor(from: "0.1.0")),
],
targets: [
    .target(name: "App", dependencies: [.product(name: "ZeroXKey", package: "sdk-swift")]),
]
```

While the major version is 0, a minor release may contain breaking changes, so pin with `.upToNextMinor(from:)`. Patch releases are compatible.

## App setup

* **Passkeys:** add the Associated Domains entitlement `webcredentials:<rpId>` and serve `https://<rpId>/.well-known/apple-app-site-association` listing `<TeamID>.<bundle ID>` under `webcredentials.apps`. Your RP ID must also be enabled for your organization; if passkey calls fail with `ZeroXKeyRequestError.reason == .webAuthnRpIdNotAllowed`, contact 0xkey.
* **Email / SMS OTP, OAuth and passkey sign-up** go through the [Auth Proxy](https://docs.0xkey.io/embedded-wallets/features/auth-proxy.md): create an Auth Proxy configuration in the Dashboard and pass its ID as `authProxyConfigId`.

## Configure

```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
import ZeroXKey

ZeroXKeyContext.configure(ZeroXKeyConfig(
    authProxyConfigId: "<auth proxy config id>",
    rpId: "wallet.example.com",
    rpName: "Example Wallet",
    organizationId: "<parent organization id>"))

let ctx = ZeroXKeyContext.shared
```

`apiUrl` and `authProxyUrl` default to the 0xkey production hosts and must be `https`.

## Sign in

```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
// Passkey: new user (creates a passkey and a sub-organization), then returning user.
try await ctx.signUpWithPasskey(anchor: window, passkeyDisplayName: "Alice")
try await ctx.loginWithPasskey(anchor: window)

// Email OTP.
let otp = try await ctx.initOtp(contact: "alice@example.com", otpType: .email)
let result = try await ctx.completeOtp(
    otpId: otp.otpId, otpCode: code,
    otpEncryptionTargetBundle: otp.otpEncryptionTargetBundle,
    contact: "alice@example.com", otpType: .email)
```

> [!NOTE]
>
> **OTP login fallback.** The SDK first tries an attested login. Only if the server reports that attested login is unavailable, and `authProxyConfigId` is set, it falls back to the Auth Proxy `otp_login_v2` route; the session lifetime is then the Auth Proxy configuration's. It never falls back on **HTTP 403** (which can mean MFA is required), on an error without an HTTP status, or on an `ATTESTED_*` reason; the original error is thrown. Scoped logins never fall back.

## Wallets and signing

```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
let wallets = try await ctx.refreshWallets()
let walletId = try await ctx.createWallet(walletName: "Main")   // default accounts: Ethereum + Solana

// Unsigned transaction as hex in, signed transaction as hex out.
let signed = try await ctx.signTransaction(
    signWith: address, unsignedTransaction: unsignedHex, type: .TRANSACTION_TYPE_ETHEREUM)
```

Prefer `signTransaction` over `signRawPayload` for transactions: transaction-field policies (`eth.tx.*`, `solana.tx.*`) apply to transaction-signing activities. See [Policy language](https://docs.0xkey.io/policies/language.md#activity-parameters).

To require a passkey for each signature, stamp the request with a `PasskeyStamper`:

```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
guard let session = ctx.session, let client = ctx.client else { throw ZeroXKeyError.noActiveSession }
let passkeyClient = client
    .with(stamper: PasskeyStamper(rpId: "wallet.example.com", presentationAnchor: { window }))
    .with(organizationId: session.organizationId)

let tx = try await passkeyClient.signTransaction(TSignTransactionBody(
    signWith: address, unsignedTransaction: unsignedHex, type: .TRANSACTION_TYPE_ETHEREUM))
```

## Policies

`ZeroXKeyClient` exposes the policy endpoints, including deletion:

```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
guard let session = ctx.session, let client = ctx.client else { throw ZeroXKeyError.noActiveSession }
let orgId = session.organizationId

_ = try await client.deletePolicy(TDeletePolicyBody(policyId: policyId, organizationId: orgId))
_ = try await client.deletePolicies(TDeletePoliciesBody(policyIds: [id1, id2], organizationId: orgId))
```

A missing policy ID returns HTTP 404. See [Policy overview](https://docs.0xkey.io/policies/overview.md#managing-policies).

## Errors

| Error | When |
| :- | :- |
| `ZeroXKeyRequestError` | The server rejected the request: `httpStatus`, `code` (gRPC), `message`, `reason` |
| `ZeroXKeyActivityError` | The activity did not complete: `.failed`, `.rejected`, `.consensusNeeded`, `.authenticatorsNeeded` (MFA), `.pollingTimedOut` |
| `ZeroXKeyError` | SDK-level: `.notConfigured`, `.missingAuthProxyConfiguration`, `.noActiveSession`, … |
| `ZeroXKeyHttpError` | Transport or decoding problems |

An activity that is not `COMPLETED` is always thrown, never returned as success. Branch on `code` / `httpStatus`, not on message text; see [Errors](https://docs.0xkey.io/developer-reference/api-overview/errors.md).

## More

* README, DocC articles and the example app: [github.com/0xkey-io/sdk-swift](https://github.com/0xkey-io/sdk-swift)
* [CHANGELOG](https://github.com/0xkey-io/sdk-swift/blob/main/CHANGELOG.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.