Swiftpack.co - Package - cloudant/swift-cloudant


Version Platform Build Status

Applications use swift-cloudant to store, index and query remote JSON data on Cloudant or CouchDB.

Swift-Cloudant is an Apache CouchDB™ client written in Swift 3. It is built by Cloudant and is available under the Apache 2.0 license.


This is an early-release version of the library, with support for the following operations:

  • Getting documents by doc ID.
  • Updating and deleting documents.
  • Creating and deleting databases.
  • Changes Feed operations
  • Creating, updating and deleting of attachments.
  • Querying views.
  • Creating, deleting and querying indexes.

We will be rounding out the feature set in upcoming releases.

Currently it does not support being called from Objective-C.


SwiftCloudant is supported, however since it is an early release it is on a "best effort" basis.


Currently Swift Cloudant supports:

  • macOS
  • Linux (with Swift 4.x); known issues from the Swift Linux runtime:
    • issue #176 with GetChangesOperation.
    • issue #178 authentication fails because cookies are not saved.

Swift Cloudant is unsupported on:

  • iOS (should work, but hasn't been tested)
  • tvOS
  • watchOS

Using in your project

SwiftCloudant is available using the Swift Package Manager and CocoaPods.

To use with CocoaPods add the following line to your Podfile:

pod 'SwiftCloudant', :git => 'https://github.com/cloudant/swift-cloudant.git'

To use with the swift package manager add the following line to your dependencies in your Package.swift:

.Package(url: "https://github.com/cloudant/swift-cloudant.git")

Overview of the library

import SwiftCloudant

// Create a CouchDBClient
let cloudantURL = NSURL(string:"https://username.cloudant.com")!
let client = CouchDBClient(url:cloudantURL, username:"username", password:"password")
let dbName = "database"

// Create a document
let create = PutDocumentOperation(id: "doc1", body: ["hello":"world"], databaseName: dbName) {(response, httpInfo, error) in
    if let error = error {
        print("Encountered an error while creating a document. Error:\(error)")
    } else {
        print("Created document \(response?["id"]) with revision id \(response?["rev"])")

// create an attachment
let attachment = "This is my awesome essay attachment for my document"
let putAttachment = PutAttachmentOperation(name: "myAwesomeAttachment",
    data: attachment.data(using: NSUTF8StringEncoding, allowLossyConversion: false),
    documentID: "doc1"
    revision: "1-revisionidhere",
    databaseName: dbName) { (response, info, error) in
        if let error = error {
            print("Encountered an error while creating an attachment. Error:\(error)")
        } else {
            print("Created attachment \(response?["id"]) with revision id \(response?["rev"])")
client.add(operation: putAttachment)

// Read a document
let read = GetDocumentOperation(id: "doc1", databaseName: dbName) { (response, httpInfo, error) in
    if let error = error {
        print("Encountered an error while reading a document. Error:\(error)")
    } else {
        print("Read document: \(response)")

// Delete a document
let delete = DeleteDocumentOperation(id: "doc1",
    revision: "1-revisionidhere",
    databaseName: dbName) { (response, httpInfo, error) in
    if let error = error {
        print("Encountered an error while deleting a document. Error: \(error)")
    } else {
        print("Document deleted")


Currently they are no third party dependencies.



Contributing to the project





Stars: 31
Help us keep the lights on



0.8.0 - Mar 20, 2018

  • [NEW] Runs on Swift 4.x on Linux with some limitations (see note).
  • [FIXED] Set the default Swift language version in the SwiftCloudant.podspec to 3.1.
  • [FIXED] Fixed warning for non-exhaustive switch statement.
  • [NOTE] The GetChangesOperation is not currently supported by the Linux platform, see issue #176.

0.7.0 - Apr 10, 2017

  • [NEW] Implement convenience init for creating client from VCAP services metadata.
  • [NEW] Allow server to generate document IDs if they are set as nil in the documents being saved.
  • [FIXED] Access level warning in the ViewPager class.
  • [FIXED] Crash on deallocate for unused client.
  • [FIXED] Resolved issue where an infinite retry loop would be triggered if a 401: Forbidden response was always received from an endpoint.

0.6.0 - Sep 16, 2016

  • [NEW] Support for Swift 3.0 Release for Darwin Platforms.
  • [NEW] New PutBulkDocsOperation API.
  • [NEW] Support for backing off when a 429 status code is encountered, optionally enabled by using ClientConfiguration struct.
  • [NEW] New GetChangesOperation API.
  • [NEW] New ViewPager API.
  • [FIXED] Correctly handle differences between CouchDb and Cloudant for cookie expiration.

0.5.0 - Aug 25, 2016

  • [NEW] Support Swift SNAPSHOT 2016-07-26

0.4.0 - Jul 28, 2016

  • [NEW] New GetAllDocs API.
  • [BREAKING] skip and limit properties on QueryViewOperation are now UInt
  • [BREAKING] All APIs have changed to match the API guidelines laid down by the swift community. To see the full list of APIs which have changed, see issue #96