Here's a quick answer that will hopefully point you in the right direction.
- Step 1: Download Postman
- Step 2: Open up Postman, paste your url into the GET field
- Step 3: Click the "Headers" tab.
TRN-Api-Key
is going to be the key
, your API key will be the value. It'll look like this.

Play around with Postman making calls, make sure they work, etc. It's a lot easier than ****ing around with Xcode setting breakpoints, etc. Once you've got it figured out what calls you're going to be making...then go to Xcode.
Once you've got your GET
s kicking back JSONs, then cut and paste it into something like https://app.quicktype.io. For the endpoint in the example URL I'm using, it gives you this:
struct Response: Codable {
let items: [Item]
}
struct Item: Codable {
let metadata: [Metadatum]
}
struct Metadatum: Codable {
let key: String
let value: String?
}
Step 4: Look into URLComponents
. That'll be how you create URLs to feed to your URLRequests
.
Step 5: When you make URLRequests
, you'll need to dump in the API key name and the API key value into the header. It should be pretty self-explanatory on the documentation page.
Here's the basic "physics for poets" code:
let fortniteChallengesURL = URL(string: "https://api.fortnitetracker.com/v1/challenges")
if let unwrappedURL = fortniteChallengesURL {
var request = URLRequest(url: unwrappedURL)
request.addValue("YOUR API KEY", forHTTPHeaderField: "TRN-Api-Key")
// Make your request and handle the response
}
Alternatively (perhaps preferably), you can use a networking framework like Alamofire for making requests. Here's how to customize headers in Alamofire.
Here's how to use the Codable
structs in parsing the returns from your endpoint.
Here's the finished product using Apple's "factory" classes, if you want to dump it into a playground. You'll need your own API key.
struct Response: Codable {
let items: [Item]
}
struct Item: Codable {
let metadata: [Metadatum]
}
struct Metadatum: Codable {
let key: String
let value: String?
}
let fortniteChallengesURL = URL(string: "https://api.fortnitetracker.com/v1/challenges")
if let unwrappedURL = fortniteChallengesURL {
var request = URLRequest(url: unwrappedURL)
request.addValue("YOUR API KEY HERE", forHTTPHeaderField: "TRN-Api-Key")
let dataTask = URLSession.shared.dataTask(with: request) { (data, response, error) in
// you should put in error handling code, too
if let data = data {
do {
let json = try JSONDecoder().decode(Response.self, from: data) as Response
// HERE'S WHERE YOUR DATA IS
print(json.items.count)
} catch {
print(error.localizedDescription)
}
}
}
dataTask.resume()
}