Java API throws "All hosts are unreachable"

I’m using the Java Maven library version 3.16.5 with following code:

public class AlgoliaHelper {

    private static final String ALGOLIA_ADMIN_KEY = "***";
    private static final String ALGOLIA_APPLICATION_ID = "***";
    private static final String ALGOLIA_INDEX = "***";

    public static void upload(List<ContentSection> sections) {
        System.out.println("Upload index to Algolia: start");
        try (SearchClient client = DefaultSearchClient.create(ALGOLIA_APPLICATION_ID, ALGOLIA_ADMIN_KEY)) {
            client.initIndex(ALGOLIA_INDEX, ContentSection.class).replaceAllObjects(sections);
            System.out.println("Upload index to Algolia: finished");
        } catch (IOException ex) {
            System.err.println("Can't connect with Algolia: " + ex.getMessage());
        } catch (Exception ex) {
            System.err.println("Algolia error: " + ex.getMessage());

Initializing the client seems to work but after that an error is thrown:

Upload index to Algolia: start
Algolia error: All hosts are unreachable

The error messages seems to point to a problem with the Algolia API or is my code incorrect?


Sorry, found my mistake, typo in the key…
Still a bit a misleading error message…

1 Like

Thanks for following up. Was the typo in the API key or the App ID? The hostname of the API endpoint is derived from the App ID, so a bad ID can surface as a network error like this. Definitely weird if we’re throwing that error for a bad API key.

someone swapped ID and key … that someone being me :wink:
so yes, indeed, the hostname/endpoint was not found.

throwing a better error would indeed be very valuable for (human) errors like this…

Thanks. At the very least it would probably help if we showed the endpoint we’re trying to hit.