Skip to content

Quick Start

Husnain Ali edited this page Sep 27, 2026 · 3 revisions

Quick Start

import SwiftNetworkKit

// 1. Configure once, inject everywhere.
let client = NetworkClient(
    configuration: NetworkConfiguration(
        baseURL: "https://api.example.com",
        headers: ["Accept": "application/json"]
    )
)

// 2. One type per API operation.
struct GetProfile: Endpoint {
    typealias Response = User
    var path: String { "/me" }
}

// 3. Call it.
let user: User = try await client.request(GetProfile())

That's the whole minimal setup. Everything else, token refresh, pinning, caching, retry, offline queueing, is opt-in through NetworkConfiguration or per-endpoint overrides. See Defining Endpoints for the full Endpoint shape, or jump straight to Authentication and Token Refresh if you need auth right away.

Making requests

Async/await

let user: User = try await client.request(GetProfile())

// convenience helpers
let raw:  Data   = try await client.data(for: GetProfile())
let text: String = try await client.string(for: GetREADME())
try await client.send(DeleteAccount())        // ignores the body

Completion handler

client.request(GetProfile()) { (result: Result<User, NetworkError>) in
    switch result {
    case .success(let user): ...
    case .failure(let error): ...
    }
}

Combine

client.publisher(for: GetProfile())
    .receive(on: DispatchQueue.main)
    .sink(receiveCompletion: { ... }, receiveValue: { user in ... })
    .store(in: &cancellables)

See Combine and SwiftUI for the rest of the reactive surface.

With an explicit request ID (for cancellation)

let id = RequestID()
async let profile = client.request(GetProfile(), id: id)
// later, from anywhere:
await client.cancel(id)

See Request Management for cancellation, deduplication, and concurrency limits.

Clone this wiki locally