Swagger/OpenAPI Generation for Swift: Tools and Libraries

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Swagger/OpenAPI Generation for Swift (iOS/macOS): A Complete Overview of Tools



Developing mobile applications for iOS/macOS often requires integration with REST APIs. Writing the networking layer manually is time-consuming and error-prone, especially with frequent specification changes. The solution is generating Swift code from OpenAPI (formerly Swagger) specifications. This article covers the main tools, their capabilities, and usage examples.



What is OpenAPI and Why Does a Swift Developer Need It?



OpenAPI is a standard for describing REST APIs in YAML or JSON format. It contains information about all endpoints, parameters, data models, and response codes. Generating code from such a description allows you to:

  • Automatically create typed data models (struct, enum)
  • Generate safe API clients with correct URLs and HTTP methods
  • Avoid typos and inconsistencies between documentation and code
  • Save hours of routine work during API updates


Main Tools for Swift Generation



Currently, the three most popular approaches are:



1. Apollo iOS

Apollo is a powerful GraphQL client, but its toolchain also supports OpenAPI via the apollo-ios-openapi plugin. However, this solution is more oriented towards GraphQL, so for pure REST, it is better to consider other options.



2. CreateAPI

CreateAPI is a modern generator written in Swift. It supports OpenAPI 3.0 and 3.1, generates clean Swift code using async/await, Combine, or closure-based API. This is the best choice for new projects.



3. Swagger Codegen / OpenAPI Generator

Classic solutions from the community. They support many languages, including Swift 5. Generation is done via CLI or plugins for Gradle/Maven. The result includes data models, a networking layer (based on URLSession or Alamofire), and protocols.



Example of Using CreateAPI



Let's look at generating a client for a simple REST API (e.g., a list of users).



Step 1. Installation

Add CreateAPI to Package.swift or install it via Mint:

// Package.swiftdependencies: [    .package(url: "https://github.com/CreateAPI/CreateAPI", from: "0.2.0")]


Step 2. Prepare the OpenAPI Specification

Example api.yaml:

openapi: "3.0.0"info:  title: Users API  version: "1.0"paths:  /users:    get:      summary: Get all users      responses:        '200':          description: Success          content:            application/json:              schema:                type: array                items:                  $ref: '#/components/schemas/User'components:  schemas:    User:      type: object      properties:        id:          type: integer        name:          type: string        email:          type: string      required:        - id        - name


Step 3. Generate Swift Code

Create a configuration file .createapi.yml:

spec: api.yamloutput: ./Generatedmodule: UsersAPI


Run the generator:

swift run create-api generate


Step 4. Using the Generated Code

As a result, you will get the files UsersAPI.swift and Models.swift. Example usage in an application:

import UsersAPI

let client = Client(baseURL: URL(string: "https://api.example.com")!)

Task { do { let users: [User] = try await client.users.get() for user in users { print("\\(user.name) (\\(user.email))") } } catch { print("Error: \\(error.localizedDescription)") }}


Setting Up Swagger

Recommendations