Java SDK

Official Java SDK for HINOW API with builder patterns and async

Java SDK

Install and configure the HINOW Java SDK to integrate over 100 AI models into Java applications with async support and reactive streams.


The HINOW Java SDK provides full access to the HINOW REST API with CompletableFuture support, reactive streams, and comprehensive type safety.

> Note: For API resource documentation with code examples, see the API Reference. This page covers Java SDK-specific features and configurations.

Installation

First, add the JitPack repository to your build file:

Maven (pom.xml)

xml
<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Then add the dependency:

xml
<dependency>
    <groupId>com.github.hinow-ai</groupId>
    <artifactId>sdk-java</artifactId>
    <version>1.0.1</version>
</dependency>

Gradle (build.gradle)

Add to your repositories:

groovy
repositories {
    maven { url 'https://jitpack.io' }
}

Then add the dependency:

groovy
implementation 'com.github.hinow-ai:sdk-java:1.0.1'

Requirements

  • Java 11+

Configuration

Basic Setup

java
import ai.hinow.Hinow;

Hinow client = Hinow.builder()
    .apiKey("your-api-key")
    .build();

Environment Variable

bash
export HINOW_API_KEY=your-api-key

java
// API key loaded automatically from environment
Hinow client = Hinow.builder().build();

Advanced Configuration

java
Hinow client = Hinow.builder()
    .apiKey("your-api-key")
    .baseUrl("https://api.hinow.ai")
    .timeout(Duration.ofSeconds(120))
    .maxRetries(3)
    .build();

Basic Usage

Chat Completions

java
import ai.hinow.Hinow;
import ai.hinow.models.*;

public class Main {
    public static void main(String[] args) {
        Hinow client = Hinow.builder().build();

        ChatCompletionResponse response = client.chat().completions().create(
            ChatCompletionRequest.builder()
                .model("gpt-4o")
                .messages(List.of(
                    Message.system("You are a helpful assistant."),
                    Message.user("What is the capital of France?")
                ))
                .temperature(0.7)
                .maxTokens(1024)
                .build()
        );

        System.out.println(response.getChoices().get(0).getMessage().getContent());
    }
}

Using Different Models

java
// OpenAI GPT-4o
ChatCompletionResponse response = client.chat().completions().create(
    ChatCompletionRequest.builder()
        .model("gpt-4o")
        .messages(List.of(Message.user("Explain machine learning")))
        .build()
);

// Anthropic Claude
ChatCompletionResponse response = client.chat().completions().create(
    ChatCompletionRequest.builder()
        .model("claude-sonnet-4-20250514")
        .messages(List.of(Message.user("Explain machine learning")))
        .build()
);

// DeepSeek
ChatCompletionResponse response = client.chat().completions().create(
    ChatCompletionRequest.builder()
        .model("deepseek-ai/deepseek-v3.2")
        .messages(List.of(Message.user("Explain machine learning")))
        .build()
);

Streaming

java
client.chat().completions().createStream(
    ChatCompletionRequest.builder()
        .model("gpt-4o")
        .messages(List.of(Message.user("Write a story about a robot")))
        .build()
).forEach(chunk -> {
    String content = chunk.getChoices().get(0).getDelta().getContent();
    if (content != null) {
        System.out.print(content);
    }
});

Async Operations

java
CompletableFuture<ChatCompletionResponse> future = client.chat().completions().createAsync(
    ChatCompletionRequest.builder()
        .model("gpt-4o")
        .messages(List.of(Message.user("Hello!")))
        .build()
);

future.thenAccept(response -> {
    System.out.println(response.getChoices().get(0).getMessage().getContent());
});

Function Calling (Tool Use)

java
ChatCompletionResponse response = client.chat().completions().create(
    ChatCompletionRequest.builder()
        .model("gpt-4o")
        .messages(List.of(
            Message.user("What is the weather in New York?")
        ))
        .tools(List.of(
            Tool.builder()
                .type("function")
                .function(FunctionDefinition.builder()
                    .name("get_weather")
                    .description("Get the current weather for a location")
                    .parameters(Map.of(
                        "type", "object",
                        "properties", Map.of(
                            "location", Map.of(
                                "type", "string",
                                "description", "City and state, e.g., New York, NY"
                            ),
                            "unit", Map.of(
                                "type", "string",
                                "enum", List.of("celsius", "fahrenheit")
                            )
                        ),
                        "required", List.of("location")
                    ))
                    .build())
                .build()
        ))
        .toolChoice("auto")
        .build()
);

List<ToolCall> toolCalls = response.getChoices().get(0).getMessage().getToolCalls();
if (toolCalls != null) {
    for (ToolCall toolCall : toolCalls) {
        System.out.println("Function: " + toolCall.getFunction().getName());
        System.out.println("Arguments: " + toolCall.getFunction().getArguments());
    }
}

Image Generation

java
ImageResponse response = client.images().generate(
    ImageGenerateRequest.builder()
        .model("black-forest-labs/flux-1-schnell")
        .prompt("A programmer cat wearing glasses, cartoon style")
        .size("1024x1024")
        .quality("hd")
        .build()
);

for (ImageData image : response.getData()) {
    System.out.println("URL: " + image.getUrl());
}

Embeddings

java
EmbeddingResponse response = client.embeddings().create(
    EmbeddingRequest.builder()
        .model("BAAI/bge-base-en-v1.5")
        .input("Machine learning is fascinating")
        .build()
);

List<Float> embedding = response.getData().get(0).getEmbedding();
System.out.println("Dimensions: " + embedding.size());

Error Handling

java
import ai.hinow.exceptions.*;

try {
    ChatCompletionResponse response = client.chat().completions().create(
        ChatCompletionRequest.builder()
            .model("nonexistent-model")
            .messages(List.of(Message.user("Hello")))
            .build()
    );
} catch (AuthenticationException e) {
    System.out.println("Invalid API key");
} catch (InsufficientBalanceException e) {
    System.out.println("Insufficient balance");
} catch (RateLimitException e) {
    System.out.println("Rate limit reached. Retry after: " + e.getRetryAfter());
} catch (BadRequestException e) {
    System.out.println("Invalid request: " + e.getMessage());
} catch (HinowException e) {
    System.out.println("API Error [" + e.getStatusCode() + "]: " + e.getMessage());
}

Additional Resources