The Dart API clients let you interact with Algolia’s APIs from your Dart backend.

If you’re building a Flutter app, see Algolia for Flutter for more information.

Install the library

All API clients are part of the algoliasearch package, which you can install with the dart pub add command

1
dart pub add algoliasearch:'^1.0'

Test your installation

To test your installation, create a new dart project, for example, with dart create hello-algolia and run a short program that adds a record to a test index, searches your index, and prints the results.

  1. If you haven’t already, create an Algolia account.

  2. Copy the following code into the file bin/hello_algolia.dart. Replace ALGOLIA_APPLICATION_ID and ALGOLIA_API_KEY with values from your account. Make sure to use an API key with addObject and search permissions.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    // File: hello_algolia.dart
    import 'package:algoliasearch/algoliasearch.dart';
    
    Future<void> main() async {
      final appID = "ALGOLIA_APPLICATION_ID";
      final apiKey = "ALGOLIA_API_KEY";
      final indexName = "test-index";
    
      final client = SearchClient(appId: appID, apiKey: apiKey);
    
      // Create a new record
      final record = {'objectID': 'object-1', 'name': 'test record'};
    
      // Add the record to an index
      final saveResp = await client.saveObject(indexName: indexName, body: record);
    
      // Wait until the indexing is done
      await client.waitTask(indexName: indexName, taskID: saveResp.taskID);
    
      // Search for 'test'
      final searchResp = await client.search(
        searchMethodParams: SearchMethodParams(
          requests: [SearchForHits(indexName: indexName, query: 'test')]));
    
      print(searchResp.toJson());
    }
    

    In production, don’t include your credentials in your code. Use environment variables instead.

  3. Run the program with dart run.

If the command is successful, you’ll see the API response.

Next steps

You can view your new index in the Algolia dashboard.

Did you find this page helpful?