Algolia Python API Client

repository·main·Indexed 42 days ago

https://github.com/algolia/algoliasearch-client-python

A high-performance, low-level HTTP client for interacting with the Algolia API in Python projects. Version 4.47.0 supports Python 3.8+ and provides capabilities for search, indexing, and A/B testing via the SearchClient and AbtestingV3Client (including a synchronous AbtestingV3ClientSync). Key features include record management with save_object, asynchronous task polling with wait_for_task, and comprehensive A/B test lifecycle management including estimation, variant setting capture, and timeseries data retrieval.

Tokens
18.4K
Snippets
78
Records
114
Agent score
50%

What's inside Algolia

  1. Handle response objects and JSON serialization

    main

    The client returns response objects that can be used directly or serialized to JSON. Use the .to_json() method on the response object to get a JSON string representation of the result.

    # Example of using the response object
    print(response)
    
    # Example of converting the response to a JSON string
    print(response.to_json())
  2. Manage A/B tests with the A/B Testing V3 client

    main

    The A/B Testing V3 client allows you to manage experiments, including listing, retrieving, estimating, and stopping A/B tests. Most operations require the analytics or editSettings API Key ACLs.

    Key Operations:

    • List and Retrieve: Use list_ab_tests to find configured tests and get_ab_test to fetch specific details by ID.
    • Estimation: Use estimate_ab_test to predict sample size and duration based on traffic and expected effect size (requires analytics ACL).
    • Lifecycle Management: Use stop_ab_test to end an experiment or delete_ab_test to remove it entirely.
    • Settings Management: Use save_variant_settings to capture settings from a winning variant and get_ab_test_settings to retrieve them.
  3. Access raw API responses with _with_http_info methods

    main

    For all major composition operations (search, save_rules, search_composition_rules, search_for_facet_values, and update_sorting_strategy_composition), there is a corresponding method suffixed with _with_http_info.

    Use these methods when you need to access the raw ApiResponse[str] object (containing headers, status codes, etc.) instead of the deserialized response object. This is useful for debugging or handling specific HTTP-level metadata.

  4. Manage A/B test settings: Save and Apply variants

    main

    Once an A/B test has reached at least 80% of its planned duration, you can capture the settings of a winning variant and apply them to your control index.

    1. Save Variant Settings

    Captures the settings of a specific variant and the control. This stops the A/B test.

    • Required ACLs: analytics, editSettings
    • Note: synonyms and enableRules are NOT captured.
    • Errors: Returns 400 if duration < 80%, or 409 if settings were already captured.

    2. Apply Variant Settings

    Applies the previously captured settings to the control index.

    • Required ACLs: analytics, editSettings
    • Note: To revert settings, apply the settings captured for the control variant (variant 1).
    • Constraints: Can be applied up to 14 days after the test ends; reverted up to 15 days after. Returns 422 if the control index is currently in an active A/B test.
    # 1. Save settings from variant 2
    await client.save_variant_settings(
        id=12345,
        variant_id=2,
        save_settings_request=my_settings_dict
    )
    
    # 2. Apply those settings to the control index
    await client.apply_variant_settings(
        id=12345,
        variant_id=2
    )
  5. Initialize the CompositionClientSync

    main

    The CompositionClientSync class is used to manage Algolia compositions synchronously. You can initialize it by providing your app_id and api_key. For advanced configurations, use the create_with_config class method to pass a custom CompositionConfig and TransporterSync.

    _client = CompositionClientSync("YOUR_ALGOLIA_APP_ID", "YOUR_ALGOLIA_API_KEY")
    # Or using named arguments
    _client_with_named_args = CompositionClientSync(app_id="YOUR_ALGOLIA_APP_ID", api_key="YOUR_ALGOLIA_API_KEY")
  6. Initialize the AgentStudioClientSync client

    main

    The AgentStudioClientSync class is used to interact with Algolia Agent Studio synchronously. You can initialize it by providing your app_id and api_key. For advanced configurations, use the create_with_config class method to pass a custom AgentStudioConfig and TransporterSync.

    _client = AgentStudioClientSync("YOUR_ALGOLIA_APP_ID", "YOUR_ALGOLIA_API_KEY")
    # Or using named arguments
    _client_with_named_args = AgentStudioClientSync(app_id="YOUR_ALGOLIA_APP_ID", api_key="YOUR_ALGOLIA_API_KEY")
  7. Initialize the AbtestingV3Client

    main

    To use the Algolia A/B Testing V3 API, instantiate the AbtestingV3Client class. You can initialize it using positional arguments or named arguments. You must provide your app_id, api_key, and the region (either 'de' or 'us').

    You can also use create_with_config to initialize the client with a custom AbtestingV3Config and Transporter for advanced configuration.

    from algoliasearch.abtesting_v3.client import AbtestingV3Client
    
    # Using positional arguments
    client = AbtestingV3Client("YOUR_ALGOLIA_APP_ID", "YOUR_ALGOLIA_API_KEY", "us")
    
    # Using named arguments
    client = AbtestingV3Client(app_id="YOUR_ALGOLIA_APP_ID", api_key="YOUR_ALGOLIA_API_KEY", region="us")
  8. Initialize the AbtestingClient

    main

    The AbtestingClient is used to manage A/B testing experiments. You can initialize it by providing your Algolia application ID, API key, and the region of your application ('de' or 'us').

    For advanced configuration, use AbtestingClient.create_with_config to pass a custom AbtestingConfig and Transporter.

    from algoliasearch.abtesting.client import AbtestingClient
    
    # Basic initialization
    client = AbtestingClient(
        app_id="YOUR_ALGOLIA_APP_ID", 
        api_key="YOUR_ALGOLIA_API_KEY", 
        region="us"
    )
  9. Initialize the CompositionClient

    main

    The CompositionClient is the primary entry point for managing Algolia search compositions. You can initialize it by providing your app_id and api_key. It also supports advanced configuration using a CompositionConfig object and a custom Transporter.

    from algoliasearch.composition.client import CompositionClient
    
    # Basic initialization
    client = CompositionClient(
        app_id="YOUR_ALGOLIA_APP_ID",
        api_key="YOUR_ALGOLIA_API_KEY"
    )
    
    # Using a context manager (recommended for automatic cleanup)
    async with CompositionClient(app_id="ID", api_key="KEY") as client:
        # perform operations
        pass
  10. Initialize the AbtestingV3ClientSync

    main

    To use the A/B Testing V3 API synchronously, instantiate the AbtestingV3ClientSync class. You must provide your Algolia app_id, api_key, and the region (either 'de' or 'us').

    You can also provide a custom transporter or config for advanced setup. The client supports the context manager pattern (with statement) to ensure the underlying transporter is closed properly.

    from algoliasearch.abtesting_v3.client import AbtestingV3ClientSync
    
    # Basic initialization
    client = AbtestingV3ClientSync(
        app_id="YOUR_ALGOLIA_APP_ID",
        api_key="YOUR_ALGOLIA_API_KEY",
        region="us"
    )
    
    # Using as a context manager to ensure connection closure
    with AbtestingV3ClientSync(app_id="ID", api_key="KEY", region="us") as client:
        # perform operations
        pass