Swift quick start
Use the Fauna Core HTTP API to query e-commerce demo data using Swift.
-
Create a database with demo data
Log in to the Fauna Dashboard, and create a database with the following configuration:
-
Region group: Choose your preferred region group, which determines where your data resides.
-
Use demo data: Enabled.
-
Enable backups: Disabled.
Leave the other options as is.
-
-
Create an authentication secret
Fauna supports several types of authentication secrets. Create a key, which is a type of secret, with the built-in
server-readonly
role:-
In the Dashboard’s Explorer page, select your demo database.
-
In the database’s Keys tab, click Create Key.
-
Choose a Role of server (read-only).
-
Click Save.
-
Copy the Key Secret.
-
-
Set the FAUNA_SECRET environment variable
Set the
FAUNA_SECRET
environment variable to your key’s secret.export FAUNA_SECRET=<KEY_SECRET>
-
Set up a Swift project
Create and navigate to a new directory for your app. Then initialize a Swift project:
mkdir app cd app swift package init --type executable
-
Create a basic app
In the
app
directory, replace the content ofSources/app/main.swift
with the following code:import Foundation // Define the Query API endpoint and headers. let url = URL(string: "https://db.fauna.com/query/1")! let faunaSecret = ProcessInfo.processInfo.environment["FAUNA_SECRET"]! var request = URLRequest(url: url) request.httpMethod = "POST" request.addValue("Bearer \(faunaSecret)", forHTTPHeaderField: "Authorization") request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.addValue("simple", forHTTPHeaderField: "X-Format") // Define the FQL query. let queryPayload: [String: Any] = [ "query": "Product.sortedByPriceLowToHigh() { name, description, price }" ] do { // Set the request body with query. let jsonData = try JSONSerialization.data(withJSONObject: queryPayload, options: []) request.httpBody = jsonData } catch { print("Failed to encode the JSON body: \(error)") exit(1) } // Make the HTTP request. let task = URLSession.shared.dataTask(with: request) { data, response, error in if let error = error { print("Error: \(error)") return } guard let data = data else { print("No data received") return } do { // Output the response. let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any] let prettyJsonData = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted) if let prettyJson = String(data: prettyJsonData, encoding: .utf8) { print(prettyJson) } } catch { print("Failed to parse response: \(error)") } } // Start the task. task.resume() // Keep the program running to wait for the async response. RunLoop.main.run()
-
Run the app
Run the app from the
app
directory.swift run
The app prints the Query API’s response. The
data
property contains the results of the query:{ "summary" : "", "data" : { "data" : [ { "name" : "single lime", "price" : 35, "description" : "Conventional, 1 ct" }, { "name" : "cilantro", "price" : 149, "description" : "Organic, 1 bunch" }, { "name" : "limes", "price" : 299, "description" : "Conventional, 16 oz bag" }, { "name" : "organic limes", "price" : 349, "description" : "Organic, 16 oz bag" }, { "name" : "avocados", "price" : 399, "description" : "Conventional Hass, 4ct bag" }, { "name" : "pizza", "price" : 499, "description" : "Frozen Cheese" }, { "name" : "cups", "price" : 698, "description" : "Translucent 9 Oz, 100 ct" }, { "name" : "taco pinata", "price" : 2399, "description" : "Giant Taco Pinata" }, { "name" : "donkey pinata", "price" : 2499, "description" : "Original Classic Donkey Pinata" } ] }, ... }