TypeScript API Client
Glean's TypeScript API client provides full type safety for integrating Glean's search and AI capabilities into web applications and Node.js services.
@gleanwork/api-client
Official TypeScript/JavaScript client for Glean's Client API
Installation
- npm
- yarn
- pnpm
npm install @gleanwork/api-client
yarn add @gleanwork/api-client
pnpm add @gleanwork/api-client
Quick Start
import { Glean } from "@gleanwork/api-client";
const client = new Glean({
apiToken: process.env.GLEAN_API_TOKEN,
instance: process.env.GLEAN_INSTANCE,
});
const result = await client.client.chat.create({
messages: [{
fragments: [{ text: "What are our company values?" }]
}]
});
Core Features
Chat API
// Simple chat
const response = await client.client.chat.create({
messages: [{ fragments: [{ text: "Explain our Q4 strategy" }] }]
});
// Streaming responses
const stream = client.client.chat.stream({
messages: [{ fragments: [{ text: "What are our priorities?" }] }]
});
for await (const chunk of stream) {
console.log(chunk.text);
}
Search API
const results = await client.client.search.search({
query: "quarterly business review",
pageSize: 10
});
results.results?.forEach(result => {
console.log(`Title: ${result.title}`);
console.log(`URL: ${result.url}`);
});
Framework Integrations
Next.js API Route
// app/api/chat/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { Glean } from '@gleanwork/api-client';
export async function POST(request: NextRequest) {
const { message } = await request.json();
const client = new Glean({
apiToken: process.env.GLEAN_API_TOKEN!,
instance: process.env.GLEAN_INSTANCE!,
});
const response = await client.client.chat.create({
messages: [{ fragments: [{ text: message }] }]
});
return NextResponse.json({ response: response.text });
}
React Component
import React, { useState } from 'react';
import { Glean } from '@gleanwork/api-client';
export function ChatComponent({ apiToken, instance }) {
const [input, setInput] = useState('');
const [response, setResponse] = useState('');
const client = new Glean({ apiToken, instance });
const handleSubmit = async (e) => {
e.preventDefault();
const result = await client.client.chat.create({
messages: [{ fragments: [{ text: input }] }]
});
setResponse(result.text || '');
};
return (
<form onSubmit={handleSubmit}>
<input
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder="Ask a question..."
/>
<button type="submit">Send</button>
{response && <div>{response}</div>}
</form>
);
}
Express.js
import express from 'express';
import { Glean } from '@gleanwork/api-client';
const app = express();
app.use(express.json());
const client = new Glean({
apiToken: process.env.GLEAN_API_TOKEN!,
instance: process.env.GLEAN_INSTANCE!,
});
app.post('/api/chat', async (req, res) => {
const { message } = req.body;
const response = await client.client.chat.create({
messages: [{ fragments: [{ text: message }] }]
});
res.json({ response: response.text });
});
Authentication
User-Scoped Tokens (Recommended)
const client = new Glean({
apiToken: "your-user-token",
instance: "your-company"
});
Global Tokens with ActAs
const response = await client.client.chat.create({
messages: [{ fragments: [{ text: "Hello" }] }]
}, {
headers: { "X-Glean-ActAs": "user@company.com" }
});
Error Handling
try {
const response = await client.client.chat.create({
messages: [{ fragments: [{ text: "Hello" }] }]
});
} catch (error) {
console.error('API error:', error);
}
Testing
import { jest } from '@jest/globals';
import { Glean } from '@gleanwork/api-client';
jest.mock('@gleanwork/api-client');
const MockedGlean = Glean as jest.MockedClass<typeof Glean>;
test('chat service', async () => {
const mockCreate = jest.fn().mockResolvedValue({
text: 'Test response'
});
MockedGlean.mockImplementation(() => ({
client: { chat: { create: mockCreate } }
} as any));
// Test your code here
});