Beginners Guide to connecting to API with Swift

Hello, I recently got an API key and want to start developing a new third party app for Infinite flight. But their is so much to do and don’t know where to start.

How do I begin coding in Swift to connect to the API? What is the best way to design a map which will show all this data? Any other beginner steps that can help not just me but others to.


Live API is just a json API, so unless you want to get crazy with Combine, you can be set with just Data(contentsOf: URL) and a JSONDecoder().decode, as well as a struct that conforms to Codable which has the same parameters as the API response (except maybe for Get User Stats where you need to make a POST request). There’s plenty of tutorials on how to do that, I recommend whatever HackingWithSwift has. For Get User Stats where you need a POST request, which I personally do by making a URLRequest and then sending it using URLSession. I made some garbage for that exact purpose some weeks ago which, funnily enough, I was actually in the process of writing something for when your post appeared. I can’t guarantee it is the best way to do it, but it works, so I’m not complaining. If you will try to use it though, please wait a few hours till I push something, found some bad stuff there.

In terms of storage, this may vary between whether you use SwiftUI or Storyboards, but I personally just use a singleton class (whether through static var or @EnvironmentObject) that holds all of the data. I can’t the rest well, but in my current SwiftUI app it is something like:

class AppDataModel: ObservableObject {
    private var client: LiveApiClient
    @Published var someDataCache: [ String : SomeData ] = [:]()
    public func getSomeData(forIdentifier id: String) {
        if let data = try? client.getSomeData(id) {
            someDataCache[id] = data

Where id can be, for example, sessionId if someData is from Get Flights request, or it could be a userId if, for example, you do a Get USer Grade/Stats request.

As for the maps, are you planning to use MapKit or some other option? Only thing I know is a bit of mapkit.

I ain’t no swift pro by any means, but I am in the process of making something map-related with the API myself, so hey, maybe I can help somehow, my PMs are open.


Hello! Thank you for your answer. Do you think you can help me understand json API a little more and help coding the first parts of creating an app in swift that uses MapKit to display all flights across different servers. Allow you to select any plan on the map and view it’s flight plan, altitude, cal sign, departure and arrival airport.

Or for more simpler explanations:

  • List available servers for pilots and ATC in-game.
  • List active flights for each server.
  • List active ATC for each server.
  • Retrieve flight plans for each flight.
  • Retrieve stats for users.

I want all this in the app. So how do I begin coding an app in Swift for IOS and iPadOS devices? Love any help you provide!

1 Like

I would guess UI talk could be reserved to pms, although if you plan on using storyboards I’m probably not your guy. And if you plan on doing it as a project anyone could use, you might want to think about how you’ll do your backend as well.

As an example, let’s do this on Get Sessions endpoint:

  1. To start I would suggest using a generic struct like this, as all responses would come in this shape, so might as well write it once.
struct LiveApiResponse<T: Codable>: Codable {
    let errorCode: Int
    let result: T
  1. Then you write up a struct that replicates the structure of the json data, so here we’re replicating the SessionInfo object
struct Session: Codable {
    let maxUsers: Int
    let id: String
    let name: String
    let userCount: Int
    let type: Int

(If you don’t like the names from the api, you can always use CodingKeys)

  1. We got all the structs, so now we could get to actually getting the data. If we want to handle errors using do/catch, we can do something like this:
do {
    // We know this URL should work, thus an unsafe unwrap from URL? to URL with the exclimation mark
    let url = URL(string: "")!
    let rawData = try Data(contentsOf: url)
    let data = try JSONDecoder().decode(LiveApiResponse<[Session]>.self, from: rawData)
    let whatWeCameFor = data.result
} catch {
    // Something went wrong, so now we need to learn from our mistakes

Keep in mind, that if you get the json data, but it would be an api error like this

    "errorCode": 4,
    "result": "API Key is invalid. Contact @cameron on the Infinite Flight Community."

You will not get the api error in your catch statement, instead, you will get JSONDecoder error because it expects an array of Session objects and it got a String.

That’s like the absolute basics. If you want to get started with coding in swift as a whole, I suggest 100 days of Swift or 100 days of SwiftUI on, probably the best course out there and also a free one, and if you want to get started with Live API I wrote a whole Swift Package for it here and it is better than terrible, so it might help you understand some stuff.