Java API Client
Glean's Java API client provides enterprise-ready integration for Java applications, with support for Spring Boot and traditional enterprise patterns.
glean-api-client
Official Java client for Glean's Client API
Installation
- Maven
- Gradle
<dependency>
<groupId>com.glean.api-client</groupId>
<artifactId>glean-api-client</artifactId>
<version>0.x.x</version>
</dependency>
implementation 'com.glean.api-client:glean-api-client:0.x.x'
Quick Start
import com.glean.api_client.glean_api_client.Glean;
import com.glean.api_client.glean_api_client.models.components.*;
import java.util.List;
public class GleanExample {
public static void main(String[] args) {
Glean client = Glean.builder()
.apiToken(System.getenv("GLEAN_API_TOKEN"))
.instance(System.getenv("GLEAN_INSTANCE"))
.build();
var response = client.client().chat().create()
.chatRequest(ChatRequest.builder()
.messages(List.of(
ChatMessage.builder()
.fragments(List.of(
ChatMessageFragment.builder()
.text("What are our company values?")
.build()))
.build()))
.build())
.call();
if (response.chatResponse().isPresent()) {
System.out.println(response.chatResponse().get().text());
}
}
}
Core Features
Chat API
public class ChatService {
private final Glean client;
public ChatService(String apiToken, String instance) {
this.client = Glean.builder()
.apiToken(apiToken)
.instance(instance)
.build();
}
public String sendMessage(String message) {
var response = client.client().chat().create()
.chatRequest(ChatRequest.builder()
.messages(List.of(
ChatMessage.builder()
.fragments(List.of(
ChatMessageFragment.builder()
.text(message)
.build()))
.build()))
.build())
.call();
return response.chatResponse()
.map(ChatResponse::text)
.orElse("No response");
}
}
Search API
public class SearchService {
private final Glean client;
public SearchService(String apiToken, String instance) {
this.client = Glean.builder()
.apiToken(apiToken)
.instance(instance)
.build();
}
public List<SearchResult> search(String query) {
var response = client.client().search().search()
.searchRequest(SearchRequest.builder()
.query(query)
.pageSize(10)
.build())
.call();
return response.searchResponse()
.map(SearchResponse::results)
.orElse(List.of());
}
}
Spring Boot Integration
Configuration
@Configuration
@ConfigurationProperties(prefix = "glean")
public class GleanConfig {
private String apiToken;
private String instance;
// Getters and setters
public String getApiToken() { return apiToken; }
public void setApiToken(String apiToken) { this.apiToken = apiToken; }
public String getInstance() { return instance; }
public void setInstance(String instance) { this.instance = instance; }
}
@Configuration
@EnableConfigurationProperties(GleanConfig.class)
public class GleanAutoConfiguration {
@Bean
public Glean gleanClient(GleanConfig config) {
return Glean.builder()
.apiToken(config.getApiToken())
.instance(config.getInstance())
.build();
}
}
REST Controller
@RestController
@RequestMapping("/api")
public class GleanController {
private final Glean gleanClient;
public GleanController(Glean gleanClient) {
this.gleanClient = gleanClient;
}
@PostMapping("/chat")
public ResponseEntity<String> chat(@RequestBody Map<String, String> request) {
try {
String message = request.get("message");
var response = gleanClient.client().chat().create()
.chatRequest(ChatRequest.builder()
.messages(List.of(
ChatMessage.builder()
.fragments(List.of(
ChatMessageFragment.builder()
.text(message)
.build()))
.build()))
.build())
.call();
String responseText = response.chatResponse()
.map(ChatResponse::text)
.orElse("No response");
return ResponseEntity.ok(responseText);
} catch (Exception e) {
return ResponseEntity.status(500).body("Error: " + e.getMessage());
}
}
}
Authentication
User-Scoped Tokens (Recommended)
Glean client = Glean.builder()
.apiToken("your-user-token")
.instance("your-company")
.build();
Configuration Properties
# application.yml
glean:
api-token: ${GLEAN_API_TOKEN}
instance: ${GLEAN_INSTANCE}
OAuth Authentication
OAuth allows you to use access tokens from your identity provider (Google, Azure, Okta, etc.) instead of Glean-issued tokens.
Prerequisites
- OAuth enabled in Glean Admin > Third-Party OAuth
- Your OAuth Client ID registered with Glean
- See OAuth Setup Guide for admin configuration
OAuth requests require these headers:
| Header | Value |
|---|---|
Authorization | Bearer <oauth_access_token> |
X-Glean-Auth-Type | OAUTH |
Example: Authorization Code Flow with Spring Security
This example uses Spring Security OAuth2 Client:
# application.yml
spring:
security:
oauth2:
client:
registration:
glean:
provider: glean
client-id: ${OAUTH_CLIENT_ID}
client-secret: ${OAUTH_CLIENT_SECRET}
scope: openid,email
authorization-grant-type: authorization_code
provider:
glean:
issuer-uri: ${OAUTH_ISSUER}
glean:
instance: ${GLEAN_INSTANCE}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.web.bind.annotation.*;
import com.glean.api_client.glean_api_client.Glean;
import com.glean.api_client.glean_api_client.models.components.*;
import java.util.List;
import java.util.Map;
@RestController
public class GleanOAuthController {
@Value("${glean.instance}")
private String gleanInstance;
@GetMapping("/search")
public Map<String, Object> search(
@RegisteredOAuth2AuthorizedClient("glean") OAuth2AuthorizedClient oauthClient,
@RequestParam String query) throws Exception {
String accessToken = oauthClient.getAccessToken().getTokenValue();
// Create Glean client and pass OAuth headers per-request
Glean glean = Glean.builder()
.instance(gleanInstance)
.build();
var response = glean.client().search().query()
.query(query)
.pageSize(10)
.call(new com.glean.api_client.glean_api_client.utils.Options() {{
headers(Map.of(
"Authorization", "Bearer " + accessToken,
"X-Glean-Auth-Type", "OAUTH"
));
}});
return Map.of("results", response.searchResponse()
.map(SearchResponse::results)
.orElse(List.of()));
}
}
tip
Access tokens typically expire after ~1 hour. Spring Security OAuth2 Client handles token refresh automatically when configured with a refresh token.
Error Handling
public String safeChat(Glean client, String message) {
try {
var response = client.client().chat().create()
.chatRequest(ChatRequest.builder()
.messages(List.of(
ChatMessage.builder()
.fragments(List.of(
ChatMessageFragment.builder()
.text(message)
.build()))
.build()))
.build())
.call();
return response.chatResponse()
.map(ChatResponse::text)
.orElse("No response");
} catch (Exception e) {
System.err.println("API error: " + e.getMessage());
return "Sorry, I couldn't process your request.";
}
}
Testing
JUnit 5 with Mockito
@ExtendWith(MockitoExtension.class)
class ChatServiceTest {
@Mock
private Glean gleanClient;
@InjectMocks
private ChatService chatService;
@Test
void shouldSendMessageSuccessfully() {
String message = "Test message";
String expectedResponse = "Test response";
// Mock setup and test implementation
assertNotNull(expectedResponse);
}
}