Back to Documentation

Java SDK

Java SDK Integration and Best Practices

Code Examples

// Maven dependency
<dependency>
    <groupId>com.openai</groupId>
    <artifactId>openai-java</artifactId>
    <version>1.0.0</version>
</dependency>

// usingExample
import com.openai.OpenAI;
import com.openai.models.*;

public class Main {
    public static void main(String[] args) {
        OpenAI openai = new OpenAI.Builder()
            .apiKey(System.getenv("API_KEY"))
            .baseUrl("https://api.example.com/v1")
            .build();
        
        ChatCompletionRequest request = ChatCompletionRequest.builder()
            .model("gpt-4o")
            .messages(List.of(
                new Message("user", "Hello!")
            ))
            .build();
        
        ChatCompletionResponse response = openai.chatCompletions().create(request);
        System.out.println(response.getChoices().get(0).getMessage().getContent());
    }
}

Getting Started

Complete integration in 5 minutes

Best Practices

Follow recommended development patterns

Technical Support

Get professional help