Member-only story
Avoid writing network requests by adopting Swift OpenAPI generator
Learn how you can reduce your network request complexity by adopting the generator
Eventually, almost every app must communicate with a backend service to retrieve and store data.
When we reach this point, we need to make network requests to our server API. Usually, we do this either by using a third-party lib or by creating our own network wrapper using URLSession. This is more than ok in most cases, however, we need to write some boilerplate code, define request and response models, maintain the code base if the server’s interface changes over time, and so on.
In WWDC 2023, Apple announced Swift Open API Generator: a set of tools to help us with this process. It relies on OpenAPI specification, an industry standard for declaring the functionality of our HTTP API.
Let’s quickly review how we can use it.
I’ll be using my Coffee Shop Demo app. If you’re interested, here’s the code.
I want to show a special section to show some deals we have for the day. So I created a simple API to return this information.
As I mentioned before, the Swift Open API generator is a project with a set of tools that help us interact with our server APIs. This means that there are several packages available, each of which serves a specific goal.
Go to Package dependencies
and add the following packages:
- https://github.com/apple/swift-openapi-generator — Make sure the option
Add to target
isNone
. This is because the package provides a build plugin that we need to configure in theBuild phases
section later. - https://github.com/apple/swift-openapi-runtime
- https://github.com/apple/swift-openapi-urlsession
To finish, go to your target’s Build phases
tab and add the OpenAPIGenerator
plug-in.

As we want to consume the server API, we’ll be using the libraries to auto-generate the needed code based on an OpenAPI document.