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

# Swift

> Create user wallets from your iOS app in under 5 minutes

This quickstart integrates the Crossmint Swift SDK into an iOS app to create a non-custodial wallet for a user, fund it with testnet USDC on Base Sepolia, and send USDC to another wallet.

<Snippet file="before-you-start.mdx" />

> [card] [iOS Demo App](https://github.com/Crossmint/crossmint-swift-sdk/tree/main/Examples/SmartWalletsDemo)
>
> See a full working example with a repository to clone.

**1. Install the SDK**

Add the Crossmint Swift SDK to your project using Swift Package Manager.

In Xcode, go to **File > Add Package Dependencies** and enter the repository URL:

```text theme={null}
https://github.com/Crossmint/crossmint-swift-sdk
```

Select the `CrossmintClientSDK` product to add to your target.

### Requirements

* iOS 15.0+
* Xcode 16.0+
* Swift 5.9+

**2. Configure your API key**

The Crossmint SDK requires a client API key for authentication.
[Get your client API key](https://docs.crossmint.com/introduction/platform/api-keys/client-side.md) using the Crossmint Console.

Create a file to store your configuration:

```swift CrossmintConfig.swift theme={null}
import CrossmintClient

let crossmintApiKey = "<YOUR_API_KEY>"
@MainActor var crossmintAuthManager: CrossmintAuthManager { CrossmintSDK.shared.authManager }
```

> [!NOTE]
>
> The environment (staging vs production) is automatically determined by your API key. Staging keys start with `ck_staging_`, production keys with `ck_production_`.

**3. Initialize the Crossmint SDK**

Configure the SDK in your app's entry point and add the non-custodial signer view modifier. This example uses [Crossmint Auth](https://docs.crossmint.com/wallets/guides/crossmint-auth) but you can use [any authentication provider of your choice](https://docs.crossmint.com/wallets/guides/bring-your-own-auth.md).

```swift YourApp.swift theme={null}
import SwiftUI
import CrossmintClient

@main
struct YourApp: App {
    init() {
        CrossmintSDK.configure(apiKey: crossmintApiKey)
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
                .crossmintNonCustodialSigner()
        }
    }
}
```

The `.crossmintNonCustodialSigner()` modifier is required for transaction signing. It embeds a hidden web view that securely manages the signing process.

### Access SDK instances

Inside your views, access SDK functionality using `CrossmintSDK.shared`:

```swift theme={null}
import CrossmintClient

let sdk = CrossmintSDK.shared
let authManager = sdk.authManager
let wallets = sdk.crossmintWallets
```

**4. Authenticate users**

Actions within the SDK are user-based, so first you need to authenticate a user.
Choose your authentication method:

> [tab] Crossmint Auth (OTP)
>
> Use Crossmint's built-in OTP authentication for easy, email-based login.
>
> **1. Send OTP to user's email**
>
> ```swift theme={null}
> import CrossmintAuth
>
> let email = "user@example.com"
>
> do {
>     try await crossmintAuthManager.sendEmailOtp(email: email)
>     // OTP sent — show an input field for the code
> } catch let error as AuthManagerError {
>     // Handle error: error.message
> }
> ```
>
> **2. Verify the OTP sent to the user's email**
>
> ```swift theme={null}
> import CrossmintAuth
>
> let email = "user@example.com"
> let otpCode = "123456" // Code entered by user
>
> do {
>     let status = try await crossmintAuthManager.confirmEmailOtp(
>         email: email,
>         code: otpCode
>     )
>
>     if case .authenticationStatus(.authenticated) = status {
>         // User is authenticated, proceed to create wallet
>     }
> } catch let error as AuthManagerError {
>     // Handle error: error.message
> }
> ```
>
> > [!NOTE]
> >
> > See the [iOS Demo App](https://github.com/Crossmint/crossmint-swift-sdk/tree/main/Examples/SmartWalletsDemo) repository for a complete UI implementation example with SwiftUI views.

> [tab] Third-Party Auth (Prod)
>
> If you have an existing auth provider (Auth0, Firebase, Supabase, etc.), authenticate users with your provider and pass the JWT to Crossmint.
>
> ```swift theme={null}
> import CrossmintClient
>
> // After the user authenticates with your provider, set their JWT
> let authManager = CrossmintSDK.shared.authManager
> await authManager.setJWT(yourProviderJWT)
> // User is now authenticated with Crossmint
> ```
>
> > [!NOTE]
> >
> > Configure JWT authentication in the [Crossmint Console](https://console.crossmint.com) under API Keys > JWT Authentication before using third-party auth.

**5. Create a wallet**

After authentication, create the user's wallet. This example uses Base Sepolia (Base testnet) but you can choose any [supported chain](https://docs.crossmint.com/introduction/supported-chains.md).

```swift theme={null}
import CrossmintClient
import CrossmintAuth

let sdk = CrossmintSDK.shared

do {
    guard let email = await crossmintAuthManager.email else {
        // User not authenticated — return to the auth step
        return
    }

    let wallet = try await sdk.crossmintWallets.createWallet(
        chain: EVMChain.baseSepolia,
        recoveryMethods: [EVMSigners.email(email)]
    )

    // Wallet is ready to use!
    print("Wallet address: \(wallet.address)")
} catch let error as WalletError {
    // Handle error: error.message
}
```

> [!NOTE]
>
> Use `createWallet` for new users. For returning users, call `getWallet(chain: EVMChain.baseSepolia)` to fetch their existing wallet without creating a new one.

**6. Check wallet balance**

Before sending tokens, check the wallet balance:

```swift theme={null}
import CrossmintClient

do {
    let balance = try await wallet.balances()

    // Native token (ETH on Base Sepolia)
    print("ETH: \(balance.nativeToken.amount)")

    // USDC balance
    print("USDC: \(balance.usdc.amount)")

    // All other tokens in this wallet
    for token in balance.tokens {
        print("\(token.name): \(token.amount)")
    }
} catch let error as WalletError {
    // Handle error: error.message
}
```

**7. Fund the wallet**

Before sending USDC, you need to get some into the wallet.

**Option 1: Use the staging fund method (staging only)**

```swift theme={null}
do {
    try await wallet.fund(token: .usdxm, amount: 10)
} catch {
    // Handle error
}
```

**Option 2: Use the USDC Faucet**

Get the wallet address and use the [USDC Faucet](https://faucet.circle.com/):

```swift theme={null}
let walletAddress = wallet.address
print("Fund this address: \(walletAddress)")
```

Navigate to the faucet, select **Base Sepolia**, and enter the wallet address.

![Circle faucet - Base Sepolia selected](https://mintcdn.com/crossmint/eYDj8xN_FdfDDRqk/images/wallets/circle-usdc-faucet.png?fit=max\&auto=format\&n=eYDj8xN_FdfDDRqk\&q=85\&s=5e7b583883acb530b97680bb34acf72f)

**8. Send USDC**

Send USDC to another wallet using the `wallet.send` function:

```swift theme={null}
import CrossmintClient

let recipientAddress = "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb2"

do {
    let summary = try await wallet.send(
        recipientAddress,
        "base-sepolia:usdc",
        0.01
    )
    print("Transaction hash: \(summary.hash)")
} catch let error as TransactionError {
    switch error {
    case .userCancelled:
        // User cancelled the transaction
        break
    default:
        // Handle error: error.message
        break
    }
}
```

> [!NOTE]
>
> The Swift SDK handles transaction approval automatically inside `send()`. Unlike the Kotlin SDK, no explicit `approve()` call is needed.

The first time a wallet sends a transaction on a device, an OTP verification is required. The SDK automatically handles this by presenting an OTP input view.

To handle the OTP flow, observe the `isOTPRequired` publisher in your view:

```swift theme={null}
import SwiftUI
import CrossmintClient

struct ContentView: View {
    @State private var showOTPView = false

    var body: some View {
        YourMainView()
            .onReceive(CrossmintSDK.shared.isOTPRequired) { required in
                showOTPView = required
            }
            .sheet(isPresented: $showOTPView) {
                EmptyView() // Replace with your OTP input UI, then call CrossmintSDK.shared.submit(otp: enteredCode)
            }
    }
}
```

Following transactions sent from the same device do not need OTP verification.

And that is it! You just created a non-custodial wallet for your user and made a USDC transaction on Base Sepolia.

## Launching in Production

For production, the steps are almost identical, but some changes are required:

1. Create a developer account on the [production console](https://www.crossmint.com/console)
2. Create a production client API key on the [API Keys](https://www.crossmint.com/console/projects/apiKeys) page with the API scopes `users.create`, `users.read`, `wallets.read`, `wallets.create`, `wallets:transactions.create`, `wallets:transactions.sign`, `wallets:balance.read`, `wallets.fund`
3. Update your configuration with the production API key
4. **Use your own authentication provider**: For production applications, Crossmint recommends using [third-party authentication](https://docs.crossmint.com/wallets/guides/bring-your-own-auth.md) with providers like Auth0, Firebase, or Supabase, rather than Crossmint Auth (OTP). Configure JWT authentication in the [Crossmint Console](https://www.crossmint.com/console/projects/apiKeys) under API Keys > JWT Authentication
5. Switch your chains from testnets to their mainnet equivalents (for example, `base-sepolia` to `base`), and re-create your wallets. Staging wallet addresses do not carry over to production.
6. Replace test tokens with real assets (for example, `usdxm` to `usdc`), and plan for gas sponsorship as a real cost

See [Staging vs Production](https://docs.crossmint.com/introduction/platform/staging-vs-production.md) for the full list of differences between environments

## Learn More

> [card] [Check Balances](https://docs.crossmint.com/wallets/guides/check-balances.md)
>
> Check the balance of a wallet.

> [card] [Transfer Tokens](https://docs.crossmint.com/wallets/guides/transfer-tokens.md)
>
> Send tokens between wallets.

> [card] [Operational Signers](https://docs.crossmint.com/wallets/guides/signers/add-signers.md)
>
> Register operational signers on a wallet.

## Other Links

> [card] [Swift SDK Reference](https://docs.crossmint.com/sdk-reference/wallets/swift/classes/wallet.md)
>
> Deep dive into Swift SDK reference docs.

> [card] [Talk to an expert](https://www.crossmint.com/contact-sales)
>
> Contact the Crossmint sales team for support.
