This SDK targets Appwrite server version 2.5.x as shipped on Appwrite Cloud. Self-hosted releases can lag behind Cloud β if you run an older self-hosted build, use a matching older SDK from previous releases when APIs differ.
This is the Swift SDK for integrating with Appwrite from your Swift server-side code. If you're looking for the Apple SDK you should check appwrite/sdk-for-apple
Appwrite is an open-source backend as a service server that abstracts and simplifies complex and repetitive development tasks behind a very simple to use REST API. Appwrite aims to help you develop your apps faster and in a more secure way. Use the Swift SDK to integrate your app with the Appwrite server to easily start interacting with all of Appwrite backend APIs and tools. For full API documentation and tutorials go to https://appwrite.io/docs
Requires Swift 6.1 or later (Xcode 16.3 or later). Supported platforms are iOS 15, macOS 12, tvOS 15 and watchOS 8 or later, plus Linux.
- In Xcode, select File > Add Package Dependencies...
- Enter
https://github.com/appwrite/sdk-for-swiftin the search field and selectsdk-for-swift. - Choose a dependency rule, for example Up to Next Major Version from
23.1.0, and click Add Package. - Add the
Appwritelibrary to your app target and click Add Package.
Add the package to the dependencies in your Package.swift:
dependencies: [
.package(url: "https://github.com/appwrite/sdk-for-swift", from: "23.1.0"),
],Then add the Appwrite product to your target:
targets: [
.target(
name: "YourAppTarget",
dependencies: [
.product(name: "Appwrite", package: "sdk-for-swift"),
]
),
],Initialize the SDK with your Appwrite API endpoint, project ID and an API key. You can find your project ID on your project's Settings page in the Appwrite Console. Create an API key in the Console with the scopes your code needs. API keys grant access to your project's data, so only use this SDK in server-side code and never ship a key in a client app.
import Appwrite
let client = Client()
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API endpoint
.setProject("<PROJECT_ID>") // Your project ID
.setKey("<API_KEY>") // Your secret API keyIf you are self-hosting Appwrite, set the endpoint to https://<HOSTNAME_OR_IP>/v1. Call .setSelfSigned() only during development, when your server uses a self-signed SSL certificate.
Once your client is initialized, create a service object and call any of its methods. Every method is async and throws. Full documentation for each method is in the API references.
let users = Users(client)
let user = try await users.create(
userId: ID.unique(),
email: "email@example.com",
password: "password",
name: "Walter O'Brien"
)
print(user.name)Methods that return rows, such as listRows and getRow, accept a nestedType parameter so you can decode each row's data into your own Codable type.
struct Book: Codable {
let name: String
let author: String
let releaseYear: Int?
let genre: [String]?
let isCheckedOut: Bool
}
let tablesDB = TablesDB(client)
let result = try await tablesDB.listRows(
databaseId: "<DATABASE_ID>",
tableId: "<TABLE_ID>",
nestedType: Book.self
)
for row in result.rows {
let book = row.data
print("\(book.name) by \(book.author)")
}You can use the appwrite types command of the Appwrite CLI to generate these models from your database schema. Learn more about type generation.
All models provide helpers for converting data:
toMap()converts a model to a dictionary, which is useful for debugging or manual data manipulation.from(map:)creates a model from a dictionary.- Models conform to
Codable, so you can encode them withJSONEncoder.
let user = try await users.get(userId: "<USER_ID>")
print(user.toMap())
let copy = User<[String: AnyCodable]>.from(map: user.toMap())
let json = try JSONEncoder().encode(user)
print(String(decoding: json, as: UTF8.self))When Appwrite returns an error, the SDK throws an AppwriteError with message, code, type and response properties. Network failures, such as a timeout or a lost connection, are thrown as other errors, so add a general catch as well.
do {
let list = try await users.list()
print(list.total)
} catch let error as AppwriteError {
print(error.message)
print(error.code ?? 0)
print(error.type ?? "")
} catch {
print(error.localizedDescription)
}This library is auto-generated by Appwrite custom SDK Generator. To learn more about how you can help us improve this SDK, please check the contribution guide before sending a pull-request.
Please see the BSD-3-Clause license file for more information.
