Calling APIs with URLSession
Fetch data using async/await.
Calling APIs with URLSession is a free SwiftUI Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What URLSession Does
URLSession is Apple framework for talking to the network. You give it a URL and it fetches the response for you. 🌐
Building a URL
Start with a valid URL. The initializer is optional because text can be malformed, so unwrap it before using.
let url = URL(string: "https://api.example.com/users")!The async Fetch Call
The modern way to fetch is async/await. You await data from a URL and your code reads top to bottom, like normal.
let (data, response) = try await
URLSession.shared.data(from: url)You Get Data and Response
The call returns two things: the raw data bytes and a response describing status. You usually decode the data next.
Combining Fetch and Decode
Fetch then decode in two short steps. await the bytes, then hand them to JSONDecoder to get a typed value.
let (data, _) = try await URLSession.shared.data(from: url)
let users = try JSONDecoder().decode([User].self, from: data)Async Functions
Wrap network work in an async function. Marking it async lets you use await inside and keeps the UI responsive.
func loadUsers() async throws -> [User] { }Why throws Matters
Networking can fail, so fetch functions are throws. Callers must try, which forces you to think about errors.
Checking the Status Code
Cast the response and read its statusCode. A value of 200 means success, while 404 or 500 signal problems.
let http = response as? HTTPURLResponse
if http?.statusCode == 200 { }Reusing the Shared Session
For most apps, URLSession.shared is all you need. It is a ready-made session with sensible default settings.
Custom Requests
For headers or POST bodies, build a URLRequest. Set the method and fields, then pass the request to the session.
var request = URLRequest(url: url)
request.httpMethod = "GET"Keep Networking Off the UI
Always do fetching in an async context, never on the main thread directly, so taps and scrolling stay smooth.
Quick Check
Quick check on fetching data from a URL.
Recap: URLSession
You can now await data from a URL with URLSession, check the status code, and decode the bytes inside an async throwing function. 🚀
Frequently asked questions
Is the “Calling APIs with URLSession” lesson free?
Yes — the full text of “Calling APIs with URLSession” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Calling APIs with URLSession”?
Fetch data using async/await. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Calling APIs with URLSession” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Decoding JSON with Codable
- Calling APIs with URLSession
- The .task Modifier
- Loading & Error States