Browse documentation

Swift / iOS SDK

The official nRouter Swift SDK for iOS, macOS, watchOS, and visionOS with Swift Package Manager support, async/await, and zero external dependencies.

Last updated

The official nRouter Swift SDK (NRouter) provides zero-dependency, idiomatic Swift bindings for Apple platforms (iOS 15+, macOS 12+, tvOS 15+, watchOS 8+, and visionOS 1+). Built directly on URLSession and modern Swift async/await, it speaks to https://api.nrouter.ai/v1 with automatic x-nr-* cost and token extraction.

Installation

Swift Package Manager (Package.swift)

Add to your Package.swift dependencies:

dependencies: [
    .package(url: "https://github.com/nRouterAI/nrouter-sdk.git", from: "2.2.1")
]

And add NRouter to your target's dependencies:

.target(
    name: "YourApp",
    dependencies: [
        .product(name: "NRouter", package: "nrouter-sdk")
    ]
)

Xcode GUI

  1. Open your project in Xcode.
  2. Select File → Add Package Dependencies...
  3. Enter the repository URL: https://github.com/nRouterAI/nrouter-sdk.git
  4. Choose version rule Up to Next Major from 2.2.1.
  5. Check NRouter and add to your app target.

Setup & Authentication

Set your API key in your environment for macOS/CLI applications:

export NROUTER_API_KEY="sk-nrouter-your-key-here"

Initialize the client:

import NRouter

// Automatically reads NROUTER_API_KEY from the environment
let client = try NRouter()

On iOS where environment variables are not accessible, pass your virtual API key directly:

import NRouter

let client = try NRouter(apiKey: "sk-nrouter-your-key-here")

Chat Completion

import NRouter

let client = try NRouter()

let result = try await client.chatCompletions([
    "model": "gpt-5.4-mini",
    "messages": [
        ["role": "user", "content": "Hello, nRouter!"]
    ]
])

if let choices = result["choices"] as? [[String: Any]],
   let message = choices.first?["message"] as? [String: Any],
   let content = message["content"] as? String {
    print(content)
}

Next Steps

Was this page helpful?