Start with Swift

Learn how to setup your first Swift project powered by Appwrite.

Server SDK

This tutorial is for the Swift Server SDK, meant for server and backend applications. If you're trying to build a client-side app, like an iOS, macOS, watchOS or tvOS app, follow the Start with Apple guide.

1

Create project

Head to the Appwrite Console.

If this is your first time using Appwrite, create an account and create your first project.

Create project screen

Create project screen

Then, under Integrate with your server, add an API Key with the following scopes.

Server integrations

Server integrations

CategoryRequired scopesPurpose
Databasedatabases.writeAllows API key to create, update, and delete databases.
collections.writeAllows API key to create, update, and delete collections.
attributes.writeAllows API key to create, update, and delete attributes.
documents.readAllows API key to read documents.
documents.writeAllows API key to create, update, and delete documents.

Other scopes are optional.

Project settings screen

Project settings screen

2

Create Swift project

Create a Swift CLI application by opening XCode > Create a new XCode project

macOS > Command Line Tool.

Follow the wizard and open your new project.

3

Install Appwrite

Install the Swift Appwrite SDK by going to File > Add Packages... and search for the repo url https://github.com/appwrite/sdk-for-swift and select sdk-for-swift. Specify version as 4.0.0 with rule Up to Next Major Version.

4

Import Appwrite

Find your project ID in the Settings page. Also, click on the View API Keys button to find the API key that was created earlier.

Project settings screen

Project settings screen

Open the file main.swift and initialize the Appwrite Client. Replace <YOUR_PROJECT_ID> with your project ID and <YOUR_API_KEY> with your API key.

Swift
import Foundation
import Appwrite
import AppwriteModels

let client = Client()
    .setEndpoint("https://cloud.appwrite.io/v1")
    .setProject("<YOUR_PROJECT_ID>")
    .setKey("<YOUR_API_KEY>")
5

Initialize database

Once the Appwrite Client is initialized, create a function to configure a todo collection.

Swift
let databases = Databases(client)

func prepareDatabase() async -> (Database?, Collection?) {
    let todoDatabase = try? await databases.create(
        databaseId: ID.unique(),
        name: "TodosDB"
    )
    let todoCollection = try? await databases.createCollection(
        databaseId: todoDatabase!.id,
        collectionId: ID.unique(),
        name: "Todos"
    )
    try? await databases.createStringAttribute(
        databaseId: todoDatabase!.id,
        collectionId: todoCollection!.id,
        key: "title",
        size: 255,
        xrequired: true
    )
    try? await databases.createStringAttribute(
        databaseId: todoDatabase!.id as! String,
        collectionId: todoCollection!.id as! String,
        key: "description",
        size: 255,
        xrequired: false,
        xdefault: "This is a test description."
    )
    try? await databases.createBooleanAttribute(
        databaseId: todoDatabase!.id as! String,
        collectionId: todoCollection!.id as! String,
        key: "isComplete",
        xrequired: true
    )
    
    return (todoDatabase, todoCollection)
}
6

Add documents

Create a function to add some mock data into your new collection.

Swift
func seedDatabase(todoDatabase: Database?, todoCollection: Collection?) async {
    let testTodo1: [String: Any] = [
        "title": "Buy apples",
        "description": "At least 2KGs",
        "isComplete": true
    ]

    let testTodo2: [String: Any] = [
        "title": "Wash the apples",
        "isComplete": true
    ]

    let testTodo3: [String: Any] = [
        "title": "Cut the apples",
        "description": "Don't forget to pack them in a box",
        "isComplete": false
    ]

    try? await databases.createDocument(
        databaseId: todoDatabase!.id,
        collectionId: todoCollection!.id,
        documentId: ID.unique(), 
        data: testTodo1
    )
    try? await databases.createDocument(
        databaseId: todoDatabase!.id,
        collectionId: todoCollection!.id,
        documentId: ID.unique(), 
        data: testTodo2
    )
    try? await databases.createDocument(
        databaseId: todoDatabase!.id,
        collectionId: todoCollection!.id,
        documentId: ID.unique(),
        data: testTodo3
    )
}
7

Retrieve documents

Create a function to retrieve the mock todo data.

Swift
func getTodos(todoDatabase: Database?, todoCollection: Collection?) async {
    let todos = try? await databases.listDocuments(
        databaseId: todoDatabase!.id as! String,
        collectionId: todoCollection!.id as! String
    )
    for document in todos?.documents ?? [] {
        if let todo = document.data as? [String: Any] {
            print("Title: \(todo["title"] ?? "")\n"
                + "Description: \(todo["description"] ?? "")\n"
                + "Is Todo Complete: \(todo["isComplete"] ?? "")\n\n"
            )
        }
    }
}

let (todoDatabase, todoCollection) = await prepareDatabase()
await seedDatabase(todoDatabase: todoDatabase, todoCollection: todoCollection)
await getTodos(todoDatabase: todoDatabase, todoCollection: todoCollection)
8

All set

Run your project with XCode and see the results in the console.