Glean offers official API clients for several popular programming languages to help developers integrate Glean’s powerful search and knowledge capabilities into their applications. These clients provide language-specific interfaces to Glean’s APIs, making it easier to build custom solutions without having to handle the low-level HTTP requests.
This page provides information about our official API clients and instructions for installing and using them in your preferred programming language.
Official API Clients
Glean provides official API clients for the following languages:
Installing the API Clients
Install the Python client
pip install glean-api-client
Or using Poetry:
poetry add glean-api-client
Initialize the client
from glean.api_client import Glean
import os
# Initialize with API token
with Glean(
api_token=os.getenv("GLEAN_API_TOKEN", ""),
instance=os.getenv("GLEAN_INSTANCE", ""),
) as client:
# Use the client...
pass
Example usage
from glean.api_client import Glean
import os
with Glean(
api_token=os.getenv("GLEAN_API_TOKEN", ""),
instance=os.getenv("GLEAN_INSTANCE", ""),
) as client:
# Send a chat message to Glean
res = client.client.chat.create(messages=[
{
"fragments": [
models.ChatMessageFragment(
text="What are the company holidays this year?",
),
],
},
], timeout_millis=30000)
# Handle response
print(res)
Install the Python client
pip install glean-api-client
Or using Poetry:
poetry add glean-api-client
Initialize the client
from glean.api_client import Glean
import os
# Initialize with API token
with Glean(
api_token=os.getenv("GLEAN_API_TOKEN", ""),
instance=os.getenv("GLEAN_INSTANCE", ""),
) as client:
# Use the client...
pass
Example usage
from glean.api_client import Glean
import os
with Glean(
api_token=os.getenv("GLEAN_API_TOKEN", ""),
instance=os.getenv("GLEAN_INSTANCE", ""),
) as client:
# Send a chat message to Glean
res = client.client.chat.create(messages=[
{
"fragments": [
models.ChatMessageFragment(
text="What are the company holidays this year?",
),
],
},
], timeout_millis=30000)
# Handle response
print(res)
Install the TypeScript client
npm install @gleanwork/api-client
Or using Yarn:
yarn add @gleanwork/api-client
Initialize the client
import { Glean } from "@gleanwork/api-client";
// Initialize with API token
const client = new Glean({
apiToken: process.env.GLEAN_API_TOKEN,
instance: process.env.GLEAN_INSTANCE,
});
Example usage
import { Glean } from "@gleanwork/api-client";
async function run() {
const glean = new Glean({
apiToken: process.env.GLEAN_API_TOKEN,
instance: process.env.GLEAN_INSTANCE,
});
const result = await glean.client.chat.create({
messages: [
{
fragments: [
{
text: "What are the company holidays this year?",
},
],
},
],
});
// Handle the result
console.log(result);
}
run();
Install the Go client
go get github.com/gleanwork/api-client-go
Initialize the client
import (
"context"
glean "github.com/gleanwork/api-client-go"
"os"
)
func main() {
ctx := context.Background()
// Initialize with API token
client := glean.New(
glean.WithAPIToken(os.Getenv("GLEAN_API_TOKEN")),
glean.WithInstance(os.Getenv("GLEAN_INSTANCE")),
)
}
Example usage
import (
"context"
"fmt"
glean "github.com/gleanwork/api-client-go"
"os"
)
func main() {
ctx := context.Background()
client := glean.New(
glean.WithAPIToken(os.Getenv("GLEAN_API_TOKEN")),
glean.WithInstance(os.Getenv("GLEAN_INSTANCE")),
)
// Send a chat message to Glean
message := glean.ChatMessageFragment{
Text: "What are the company holidays this year?",
}
chatMessage := glean.ChatMessage{
Fragments: []glean.ChatMessageFragment{message},
}
res, err := client.Client.Chat.Create(ctx, &glean.ChatRequest{
Messages: []glean.ChatMessage{chatMessage},
TimeoutMillis: glean.Int64(30000),
})
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
// Handle response
fmt.Println(res)
}
Add dependency to your project
Maven:
<dependency>
<groupId>com.glean.api-client</groupId>
<artifactId>glean-api-client</artifactId>
<version>0.x.x</version> <!-- Use latest version -->
</dependency>
Gradle:
implementation 'com.glean.api-client:glean-api-client:0.x.x' // Use latest version
Initialize the client
import com.glean.api_client.glean_api_client.Glean;
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) {
// Initialize with API token
Glean client = Glean.builder()
.apiToken(System.getenv("GLEAN_API_TOKEN"))
.instance(System.getenv("GLEAN_INSTANCE"))
.build();
}
}
Example usage
import com.glean.api_client.glean_api_client.Glean;
import com.glean.api_client.glean_api_client.models.components.*;
import com.glean.api_client.glean_api_client.models.operations.ChatResponse;
import java.util.List;
public class Application {
public static void main(String[] args) {
Glean apiClient = Glean.builder()
.apiToken(System.getenv("GLEAN_API_TOKEN"))
.instance(System.getenv("GLEAN_INSTANCE"))
.build();
ChatResponse res = apiClient.client().chat().create()
.chatRequest(ChatRequest.builder()
.messages(List.of(
ChatMessage.builder()
.fragments(List.of(
ChatMessageFragment.builder()
.text("What are the company holidays this year?")
.build()))
.build()))
.build())
.call();
if (res.chatResponse().isPresent()) {
// Handle response
System.out.println(res.chatResponse().get());
}
}
}
Additional Resources
For more detailed information about using these API clients, please refer to the documentation in their respective GitHub repositories: