Member-only story

Avoid writing network requests by adopting Swift OpenAPI generator

Bruno Lorenzo
5 min readFeb 20, 2025

--

Photo by Taylor Vick on Unsplash

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:

  1. https://github.com/apple/swift-openapi-generator — Make sure the option Add to target is None. This is because the package provides a build plugin that we need to configure in the Build phases section later.
  2. https://github.com/apple/swift-openapi-runtime
  3. 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.

--

--

Bruno Lorenzo
Bruno Lorenzo

Written by Bruno Lorenzo

Software Engineer | Co-creator of https://carry-on.app - A travel planner app | Former iOS Tech Lead | I write about iOS, tech, and producitivy

No responses yet

Write a response