API Reference / API Methods / The Swift API client

Algolia’s Swift API client lets you use Algolia’s APIs from your Swift application. Compared to using the APIs directly, using the Swift API client has these benefits:

  • Network retry strategy. The API client automatically retries connecting to another Algolia server, if the connection fails. Thanks to that, using the API clients is covered by Algolia’s SLA.

  • Reindexing. The API clients let you reindex your records in a single operation.

Algolia’s Swift API client is open source. You can find the source code on GitHub.

Additional reference documentation

You can browse the automatically generated reference documentation.

Install the Swift API client

The Algolia Swift API client requires Swift version 5.0 or higher and supports iOS, macOS, tvOS, and watchOS platforms.

Install via Swift Package Manager

To add the API client to your Swift project, follow these steps:

  1. Open your project in Xcode.
  2. Go to File > Add Packages. In the search, enter the URL to the GitHub repository of the API client:

    1
    
    https://github.com/algolia/algoliasearch-client-swift
    

    Select the package algoliasearch-client-swift and click Copy Dependency.

  3. If you have a Package.swift file, add the repository as a dependency:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    let package = Package(
      dependencies: [
        .package(
          url: "https://github.com/algolia/algoliasearch-client-swift",
          from: "8.0.0"
        )
      ],
      targets: [
        // Add the client as dependency to your targets
        .executableTarget(
          name: "algolia-example",
          dependencies: [
            .product(
              name:"AlgoliaSearchClient",
              package: "algoliasearch-client-swift"
            )
          ]
        )
      ]
    )
    

Install via CocoaPods

To install the Algolia Swift API client with CocoaPods, add the following line to your Podfile:

1
pod 'AlgoliaSearchClient', '~> 8.0'

Then, run the following command:

1
pod update

Install via Carthage

To install the Algolia Swift API client with Carthage, add the following line to your Cartfile:

1
github "algolia/algoliasearch-client-swift" ~> 8.0.0

In your project directory, run these commands:

1
2
3
carthage update
./Carthage/Checkouts/algoliasearch-client-swift/carthage-prebuild
carthage build

Test your installation

If you haven’t already, create an Algolia account and create a new Algolia app to get started.

To test whether you can connect to Algolia, run a simple program that adds a record to a new index, searches the index, and print the results.

  1. Copy the following code in your text editor. If you’re signed in, the code samples below show your Algolia application ID and API key. If you’re not signed in, replace YourApplicationID with your Algolia application ID and YourAdminAPIKey with your Admin API key. You can find both in your Algolia account.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    // main.swift
    import AlgoliaSearchClient
    
    // A simple record for your index
    struct Record: Encodable {
       let objectID: ObjectID
       let name: String
    }
    
    // Add the client to the dependencies of your targets
    let client = SearchClient(appID: "YourApplicationID", apiKey: "YourAdminAPIKey")
    let index = client.index(withName: "test_index")
    
     // Create a new index and add a record
    let record: Record = .init(objectID: "1", name: "test_record")
    let indexing: ()? = try? index.saveObject(record).wait()
    
     // Create a new index and add a record
    let results = try index.search(query: "test_record")
    print(results.hits[0])
    
  2. Build and run the project:

    1
    
    swift run
    
  3. If the program ran successfully, you should see:

    1
    2
    3
    4
    5
    
    Hit<JSON>(objectID: 1, object: {
      name: "test_record",
      objectID: "1",
      ...
    }
    

You can inspect your index now in the Algolia dashboard.

Next steps

Now you can interact with the Algolia Search API, you can look at the available methods, for example, for search or indexing.

Other APIs, for example for Algolia Recommend or Analytics, come with their own clients. To get an overview, see Initialize the API client.

Did you find this page helpful?