swift-openapi-generator is a free, open source api development & testing project written in Swift and released under Apache-2.0. It has 1,971 GitHub stars, 182 forks and 132 open issues, and was last pushed 17 days ago. On this registry it ranks #111 of 178 tracked projects in API Development & Testing, with 5 head-to-head comparisons available.

What is swift-openapi-generator?

Swift OpenAPI Generator is an Apache-2.0 licensed Swift package plugin and command-line tool from Apple that reads an OpenAPI document written in YAML or JSON and generates the type-safe Swift client and server code needed to make HTTP calls or implement HTTP services, and it is aimed at Swift developers building API clients for Apple platforms and at server-side Swift developers implementing HTTP APIs. It is an SSWG incubating project.

What it is

The project lives in the Swift ecosystem and is distributed through Swift Package Manager as a build plugin, with a command-line interface available as well. It is split across several repositories to keep dependencies small and the design extensible: apple/swift-openapi-generator provides the plugin and CLI, apple/swift-openapi-runtime provides the runtime library that the generated code depends on, and separate transport packages supply the concrete HTTP plumbing. apple/swift-openapi-urlsession provides a ClientTransport built on URLSession, swift-server/swift-openapi-async-http-client provides one built on AsyncHTTPClient, and frameo-net/swift-okhttp provides one built on OkHttp.

The concrete problem it solves is the hand-written ceremony code that every HTTP integration otherwise requires: request construction, response decoding, and the model types that mirror an API's schema. That code drifts from the specification as soon as the specification changes. Because the generator runs at build time, its output is always in sync with the OpenAPI document and does not need to be committed to the source repository, so the document remains the single source of truth and the generated networking layer replaces the manually maintained one.

Key capabilities

  • Generates Swift code at build time from an OpenAPI document, so the generated output stays in sync with the specification and is not committed to the repository.
  • Supports OpenAPI Specification versions 3.0 and 3.1, with preliminary support for version 3.2.
  • Handles streaming request and response bodies, enabling use cases such as JSON event streams and large payloads without buffering.
  • Supports JSON, multipart, URL-encoded form, base64, plain text, and raw bytes, represented as value types with type-safe properties.
  • Provides client, server, and middleware abstractions that decouple the generated code from the HTTP client library and the web framework through the ClientTransport and ServerTransport protocols.
  • Exposes a generated Client type with one method per HTTP operation, usable with any transport, as in Client(serverURL:transport:) paired with URLSessionTransport().
  • Lets a server implement the generated APIProtocol, with each operation returning a typed output such as Operations.GetGreeting.Output, and register handlers on a transport such as VaporTransport.

Who uses it and how

  • Apple-platform teams, reflected in the ios-swift topic, generate a client from a backend's OpenAPI document instead of hand-maintaining request and model code.
  • Server-side Swift teams, reflected in the server-side-swift topic, implement the generated APIProtocol and register handlers with a web framework such as Vapor through VaporTransport.
  • Teams that build with SwiftPM add the generator as a package plugin so generation happens as part of an ordinary build.
  • Middleware authors rely on the middleware abstraction to insert cross-cutting behaviour without touching the generated code.
  • Services that need event streams or large payloads use the streaming body support rather than buffering whole messages.

Getting started

Add the swift-openapi-generator Swift package plugin to a SwiftPM project, or use its command-line interface, and depend on swift-openapi-runtime alongside a transport such as swift-openapi-urlsession. Step-by-step tutorials and the full documentation are hosted on the Swift Package Index page.

How it compares

No comparable or competing tools are named in the facts provided for this project, and no list of paid products it replaces is given. It therefore stands alone in this registry on the evidence available.

When to use it — and when not to

Because it is a build-time tool, a self-hoster operates no database, object storage, or mail service; the work instead lies in wiring a transport, a web framework, and the runtime library together correctly. Teams outside the Swift ecosystem should not pick it, and teams unwilling to split their dependency graph across the generator, runtime, and transport repositories may find the arrangement adds moving parts. Weaker signals worth noting are the 132 open issues, the merely preliminary support for OpenAPI 3.2, and a README excerpt that documents features and examples more thoroughly than it documents installation specifics.

project readme (upstream, from github) — read inline

Swift OpenAPI Generator

Generate Swift client and server code from an OpenAPI document.

Overview

OpenAPI is a specification for documenting HTTP services. An OpenAPI document is written in either YAML or JSON, and can be read by tools to help automate workflows, such as generating the necessary code to send and receive HTTP requests.

Swift OpenAPI Generator is a Swift package plugin that can generate the ceremony code required to make API calls, or implement API servers.

The code is generated at build-time, so it's always in sync with the OpenAPI document and doesn't need to be committed to your source repository.

Features

  • Works with OpenAPI Specification versions 3.0 and 3.1 and has preliminary support for version 3.2.
  • Streaming request and response bodies enabling use cases such as JSON event streams, and large payloads without buffering.
  • Support for JSON, multipart, URL-encoded form, base64, plain text, and raw bytes, represented as value types with type-safe properties.
  • Client, server, and middleware abstractions, decoupling the generated code from the HTTP client library and web framework.

To see these features in action, check out the list of example projects.

Usage

Swift OpenAPI Generator can be used to generate API clients and server stubs.

Below you can see some example code, or you can follow one of the step-by-step tutorials.

Using a generated API client

The generated Client type provides a method for each HTTP operation defined in the OpenAPI document^example-openapi-yaml and can be used with any HTTP library that provides an implementation of ClientTransport.

import OpenAPIURLSession
import Foundation

let client = Client(
    serverURL: URL(string: "http://localhost:8080/api")!,
    transport: URLSessionTransport()
)
let response = try await client.getGreeting()
print(try response.ok.body.json.message)

Using generated API server stubs

To implement a server, define a type that conforms to the generated APIProtocol, providing a method for each HTTP operation defined in the OpenAPI document^example-openapi-yaml.

The server can be used with any web framework that provides an implementation of ServerTransport, which allows you to register your API handlers with the HTTP server.

import OpenAPIRuntime
import OpenAPIVapor
import Vapor

struct Handler: APIProtocol {
    func getGreeting(_ input: Operations.GetGreeting.Input) async throws -> Operations.GetGreeting.Output {
        let name = input.query.name ?? "Stranger"
        return .ok(.init(body: .json(.init(message: "Hello, \(name)!"))))
    }
}

@main struct HelloWorldVaporServer {
    static func main() async throws {
        let app = try await Application.make()
        let transport = VaporTransport(routesBuilder: app)
        let handler = Handler()
        try handler.registerHandlers(on: transport, serverURL: URL(string: "/api")!)
        try await app.execute()
    }
}

Package ecosystem

The Swift OpenAPI Generator project is split across multiple repositories to enable extensibility and minimize dependencies in your project.

Repository Description
apple/swift-openapi-generator Swift package plugin and CLI
apple/swift-openapi-runtime Runtime library used by the generated code
apple/swift-openapi-urlsession ClientTransport using URLSession
swift-server/swift-openapi-async-http-client ClientTransport using AsyncHTTPClient
frameo-net/swift-okhttp ClientTransport using OkHttp
vapor/swift-openapi-vapor ServerTransport using Vapor
hummingbird-project/swift-openapi-hummingbird ServerTransport using Hummingbird
awslabs/swift-openapi-lambda ServerTransport using AWS Lambda

Requirements and supported features

Generator versions Supported OpenAPI versions
1.0.0 ... main 3.0, 3.1, 3.2 (preliminary)

See also Supported OpenAPI features.

Supported platforms and minimum versions

The generator is used during development and is supported on macOS, Linux, and Windows.

The generated code, runtime library, and transports are supported on more platforms, listed below.

Component macOS Linux, Windows iOS tvOS watchOS visionOS Android
Generator plugin and CLI ✅ 10.15+ ✖️ ✖️ ✖️ ✖️ ✖️
Generated code and runtime library ✅ 10.15+ ✅ 13+ ✅ 13+ ✅ 6+ ✅ 1+

Documentation and example projects

To get started, check out the documentation, which contains step-by-step tutorials.

You can also experiment with example projects that use Swift OpenAPI Generator and integrate with other packages in the ecosystem.

Or if you prefer to watch a video, check out Meet Swift OpenAPI Generator from WWDC23.

```yaml
openapi: '3.1.0'
info:
  title: GreetingService
  version: 1.0.0
servers:
  - url: https://example.com/api
    description: Example service deployment.
paths:
  /greet:
    get:
      operationId: getGreeting
      parameters:
        - name: name
          required: false
          in: query
          description: The name used in the returned greeting.
          schema:
            type: string
      responses:
        '200':
          description: A success response with a greeting.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Greeting'
components:
  schemas:
    Greeting:
      type: object
      description: A value with the greeting contents.
      properties:
        message:
          type: string
          description: The string representation of the greeting.
      required:
        - message
```
</details>

Frequently asked questions

Is swift-openapi-generator free to use?

swift-openapi-generator is open source under the Apache-2.0 licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does swift-openapi-generator do?

Generate Swift client and server code from an OpenAPI document.

What is swift-openapi-generator written in?

swift-openapi-generator is primarily written in Swift. Its source is publicly available at https://github.com/apple/swift-openapi-generator, and it has 1,971 GitHub stars.